OpenBI Docs Buy

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>] … )
ParameterTypeDescription
expressionExpressionThe value to work out, usually a measure, in the changed filter context.
filter1Filter
optional
A condition on columns of one table, a table, or a modifier such as ALL, KEEPFILTERS or USERELATIONSHIP.
filter2Filter
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 in KEEPFILTERS to 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 in FILTER ( VALUES ( Products[Product] ), [Liters sold] > 300000 ).
  • ALL and REMOVEFILTERS among 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]LitersBeerBeer, kept with the row
Beer150538915053891505389
Cider3010741505389(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 aloneIn CALCULATEThe measure
Cider1806463301074301074
IPA1806463331884331884
Lettøl1806463169179169179
Pils180646310043261004326

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 FrognerPils or IPA
1485411336210

CALCULATETABLE, FILTER, KEEPFILTERS, ALL, REMOVEFILTERS, USERELATIONSHIP.

Last updated · OpenBI Studio 0.3.74