CALCULATE
An expression evaluated in a filter context its filter arguments change; a row context becomes filters (context transition).
Group: Filter. Compatible with DAX.
Syntax
CALCULATE ( <expression> [, <filter1>] [, <filter2>] … )
| Parameter | Type | Description |
|---|---|---|
expression | Expression | The value to work out, usually a measure, in the changed filter context. |
filter1 | Filter optional | A condition on columns of one table, a table, or a modifier such as ALL, KEEPFILTERS or USERELATIONSHIP. |
filter2 | Filter optional, repeatable | More filters; all of them apply together. |
Returns
Same type as the expression. The expression's value in the filter context the filters make.
Remarks
- A condition such as
Products[Category] = "Beer"replaces whatever filter was on that column: in a row for Cider it still shows the beer. Wrap it inKEEPFILTERSto narrow the existing filter instead. - Several filters apply together. For either-or on one column, write one condition:
Products[Product] = "Pils" || Products[Product] = "IPA". - A condition may use several columns of one table. A condition over columns of two tables, or one that uses a measure, is an error: write it with
FILTER, as inFILTER ( VALUES ( Products[Product] ), [Liters sold] > 300000 ). ALLandREMOVEFILTERSamong the filters take effect before the conditions, whatever their order:CALCULATE ( [Liters sold], Products[Category] = "Beer", ALL ( Products ) )is the beer.- The filters are worked out in the filter context outside CALCULATE:
FILTER ( Products, ... )sees only the products already in view, so in a row for Cider it finds no beer. - In a row context, such as an iterator or a calculated column, CALCULATE turns the current row into filters (context transition). A measure does this by itself, as if it were wrapped in CALCULATE.
Examples
A condition replaces the filter on its column
The Cider row shows the beer, because the condition overrides the row's category; with KEEPFILTERS both must hold, and nothing is left.
EVALUATE
SUMMARIZECOLUMNS (
Products[Category],
"Liters", [Liters sold],
"Beer", CALCULATE ( [Liters sold], Products[Category] = "Beer" ),
"Beer, kept with the row", CALCULATE ( [Liters sold], KEEPFILTERS ( Products[Category] = "Beer" ) )
)
ORDER BY Products[Category]
| Products[Category] | Liters | Beer | Beer, kept with the row |
|---|---|---|---|
| Beer | 1505389 | 1505389 | 1505389 |
| Cider | 301074 | 1505389 | (blank) |
Context transition
In ADDCOLUMNS each product is a row, not a filter: SUM alone adds every liter. CALCULATE, or the measure, turns the row into a filter.
EVALUATE
ADDCOLUMNS (
VALUES ( Products[Product] ),
"SUM alone", SUM ( Sales[Liters] ),
"In CALCULATE", CALCULATE ( SUM ( Sales[Liters] ) ),
"The measure", [Liters sold]
)
ORDER BY Products[Product]
| Products[Product] | SUM alone | In CALCULATE | The measure |
|---|---|---|---|
| Cider | 1806463 | 301074 | 301074 |
| IPA | 1806463 | 331884 | 331884 |
| Lettøl | 1806463 | 169179 | 169179 |
| Pils | 1806463 | 1004326 | 1004326 |
Two filters together, and an either-or
EVALUATE
ROW (
"Beer in Frogner", CALCULATE ( [Liters sold], Products[Category] = "Beer", Places[District] = "Frogner" ),
"Pils or IPA", CALCULATE ( [Liters sold], Products[Product] = "Pils" || Products[Product] = "IPA" )
)
| Beer in Frogner | Pils or IPA |
|---|---|
| 148541 | 1336210 |
Related functions
CALCULATETABLE, FILTER, KEEPFILTERS, ALL, REMOVEFILTERS, USERELATIONSHIP.