Sub topic 10
The Order CALCULATE Evaluates and Applies Filters
This chapter assumes you know filter context, row context, context transition, and the filter arguments of CALCULATE from the earlier chapters. It puts those pieces in sequence, so you can predict the result when several of them appear in one expression.
The four steps
CALCULATE does not process its arguments strictly left to right. It follows a fixed sequence:
- Evaluate the filter arguments in the original context. This includes any row context and the filter context that exist where CALCULATE is called.
- Apply context transition. If a row context exists, CALCULATE turns the current row into filters on a copy of the filter context.
- Apply modifiers and removals.
USERELATIONSHIP,CROSSFILTER, and removal functions such asREMOVEFILTERS,ALL, andALLEXCEPTchange that copy. Removals also clear filters that came from the context transition. - Apply the remaining filter arguments. Boolean and table filters replace filters on the same columns, or intersect with them when wrapped in
KEEPFILTERS.
Only then does CALCULATE evaluate its first argument, the expression, in the resulting filter context.
Why step 1 matters
Because the arguments are evaluated first, in the old context, anything computed outside CALCULATE is already fixed. This is the reason a variable does not respond to a later CALCULATE.
Wrong Red Sales =
VAR t = [Total Sales]
RETURN
CALCULATE ( t, 'Product'[Color] = "Red" )The variable t is computed once, before CALCULATE runs. CALCULATE then changes a filter context that t no longer depends on, so the measure returns the same value as [Total Sales]. The fix is to put the measure reference inside CALCULATE, so it is evaluated after the filters change.
Why removals come after the transition
Step 3 happens after step 2. That lets a removal function undo the filters created from the current row:
Total Sales Per Visible Customer =
SUMX (
Customer,
CALCULATE ( [Total Sales], REMOVEFILTERS ( Customer ) )
)For each customer, the context transition filters Customer to that one row. REMOVEFILTERS ( Customer ) then clears that filter, so every row returns sales for all customers. The sum is the total sales multiplied by the number of visible customers. Filters on other tables, such as 'Product', stay in place.
ALLEXCEPT depends on the same ordering. It clears the filters from the transition except those on the columns you name.
Filter arguments among themselves
Several filter arguments in one CALCULATE are combined with AND. Each one replaces any existing filter on its own columns first, and then they intersect with each other.
Impossible Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red",
'Product'[Color] = "Blue"
)No product is both red and blue, so this measure returns blank. Each argument replaced the outer color selection, and the two arguments then intersected.
Nested CALCULATE
The inner CALCULATE runs inside the context that the outer one created, so its filters are applied last and win on conflicts.
Inner Wins =
CALCULATE (
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ),
'Product'[Color] = "Blue"
)The outer call sets color to Blue. The inner call replaces that with Red. The result is red sales.
Summary of the rules
| Situation | What happens |
|---|---|
| Variable defined before CALCULATE | Fixed in the original context. CALCULATE cannot change it. |
| Filter argument references a measure or column | Evaluated in the original context, before any new filter is applied. |
| Removal function inside an iterator | Runs after context transition, so it can clear the row's own filters. |
| Two arguments on the same column | Each replaces the old filter, then they intersect with each other. |
| Nested CALCULATE | The inner call applies its filters last and wins. |
Exercise
A calculated column on the Customer table is defined as CALCULATE ( [Total Sales], REMOVEFILTERS ( Customer ) ). What value does each customer row show, and why?
Show answer
Every row shows the same value: total sales across all customers. The row context on Customer first becomes a filter on that one customer (context transition). REMOVEFILTERS then runs after the transition and clears that filter, so no customer filter remains.
Exercise
What does CALCULATE ( [Total Sales], 'Product'[Color] = "Red", 'Product'[Color] = "Blue" ) return, and why?
Show answer
It returns blank. Each Boolean argument replaces the existing filter on 'Product'[Color], and the two filters then intersect. No product satisfies both colors.