covariancepif() (aggregation function)

Learn how to use the covariancepif() aggregation function to calculate the population 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 population formula.

Syntax

covariancepif(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 creates a new variable y based on whether x is even or odd and then 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 covariancepif(x, y, x % 3 == 0)

Output

covariancepif_x_y
2077.09090909091