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:

Syntax
covarianceif(expr1 , *expr2 , predicate)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| expr1 | real | ✔️ | First random variable expression. |
| expr2 | real | ✔️ | Second random variable expression. |
| predicate | string | ✔️ | 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 |
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.