covarianceif() (aggregation function)

Learn how to use the covarianceif() aggregation function to calculate the sample covariance in an expression where the predicate evaluates to true.

Calculates the sample covariance of two random variables expr1 and expr2 in records for which predicate evaluates to true.

The following formula is used:

Equation showing a covariance sample formula.

Syntax

covarianceif(expr1 , *expr2 , predicate)

Parameters

NameTypeRequiredDescription
expr1real✔️First random variable expression.
expr2real✔️Second random variable expression.
predicatestring✔️If predicate evaluates to true, values of expr1 and expr2 will be added to the covariance.

Returns

Returns the covariance value of expr1 and expr2 in records for which predicate evaluates to true.

Example

The example in this section shows how to use the syntax to help you get started.

This query calculates the covariance of x and y for the subset of numbers where x is divisible by 3.

range x from 1 to 100 step 1
| extend y = iff(x % 2 == 0, x * 2, x * 3)
| summarize covarianceif(x, y, x % 3 == 0)

Output

covarianceif_x_y
2142