Sub topic 10
The order CALCULATE evaluates and applies filters
This chapter assumes you already know measures, filter context and row context, and that you have seen the basic CALCULATE argument forms: Boolean filters, REMOVEFILTERS, KEEPFILTERS, FILTER and context transition. Here you will put those pieces in sequence. When a CALCULATE has several arguments, the order they run in decides the result. Most of the order is fixed by the engine, not by where you write each argument.
Two phases: reading the arguments, then building the new context
CALCULATE does its work in two separate phases:
- It evaluates its filter arguments. Every filter argument is evaluated in the context that existed when CALCULATE was called. That is the original filter context plus any row context.
- It builds a new filter context from a copy of the original, and then evaluates the first argument (the expression) in that new context.
Phase 1 means a filter argument never sees the changes made by its siblings. Phase 2 has its own fixed sequence, shown below.
The order in which you write the arguments does not change steps 3 to 5. A removal written last still runs before a filter written first.
| Step | What happens | Why it matters |
|---|---|---|
| 1 | Filter arguments are evaluated in the original context | `FILTER ( Sales, ... )` sees the report's current filters |
| 2 | Row context becomes filters (context transition) | The current row's values start out as filters |
| 3 | Relationship modifiers take effect | Later filters propagate over the chosen relationship |
| 4 | Removal functions clear filters | They can clear filters that step 2 just added |
| 5 | Other filters replace or intersect | Your new conditions are applied to what is left |
| 6 | The expression is evaluated | A variable declared earlier is not recomputed here |
Step 1: arguments read the original context
Every filter argument that is a table expression is computed before any change to the context. Compare these two measures, which use the base measure Total Sales from earlier chapters:
Total Sales = SUM ( Sales[Amount] )High Quantity Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] > 2 )
)High Quantity Sales Ignoring Filters =
CALCULATE (
[Total Sales],
FILTER ( ALL ( Sales ), Sales[Quantity] > 2 )
)In the first measure, FILTER ( Sales, ... ) is evaluated in the original filter context. If a slicer selects the year 2025, Sales already contains only 2025 rows, so the table that comes out of FILTER contains only 2025 rows with quantity above 2. The second measure wraps Sales in ALL. That removes the report's filters from the table being scanned, so the filter table holds every row with quantity above 2. The new context then filters Sales to those rows. The year slicer still applies, because the new filter on Sales is combined with the existing filter on Date.
The same rule explains why measures inside a filter argument still react to the report. Consider this measure:
Sales of Strong Colors =
CALCULATE (
[Total Sales],
FILTER (
ALL ( 'Product'[Color] ),
[Total Sales] > 1000
)
)ALL ( 'Product'[Color] ) produces every color. For each color, the iterator creates a row context and [Total Sales] is evaluated with context transition. That evaluation still carries the original filters on other columns, such as the selected category. A color qualifies only if its sales within the current selection exceed 1000.
Step 2: context transition comes before the filter arguments are applied
When CALCULATE runs inside a row context, the row's column values are first turned into filters on the new context. Only after that are your filter arguments applied. A calculated column on 'Product' shows the effect:
Red Sales For This Product =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)Suppose the current row is a blue product. Context transition filters every column of that row: ProductKey, Color, Category and so on. The Boolean argument then replaces the filter on Color with red. The filter on ProductKey is still in place, and no row has both that key and the color red. The column returns blank for blue products. The column does not return the total red sales, which is what many people expect.
Because transition happens before the filter arguments are applied, a removal function can undo it. This measure computes each customer's share of all customers' sales:
Customer Share of All =
AVERAGEX (
Customer,
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( Customer ) )
)
)Inside the iterator, the numerator uses an implicit CALCULATE. It filters sales to the current customer. The denominator makes an explicit CALCULATE that first transitions the current customer into a filter and then removes all filters on Customer, including the one just added. The result is total sales for all customers. Removing a filter works because step 4 runs after step 2.
Step 3: relationship modifiers run before filters
USERELATIONSHIP and CROSSFILTER change how filters travel. They are applied before the other filter arguments, so the filters that follow use the relationship you chose:
Sales 2025 by Ship Date =
CALCULATE (
[Total Sales],
'Date'[Year] = 2025,
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)The year filter appears before the modifier in the text, but it is applied after the modifier. It therefore reaches Sales through the ShipDate relationship. The modifier only exists for the duration of this CALCULATE. A nested CALCULATE inside it also uses the modified relationship, because the nested call starts from this new context.
Step 4: removals run before replacements and additions
Functions that clear filters (REMOVEFILTERS, ALL and ALLEXCEPT used as filter arguments) are applied before the filters that set new values. Consider the following measure:
Red Sales Ignoring Product Slicers =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red",
REMOVEFILTERS ( 'Product' )
)You might read this top to bottom and conclude that the red filter is added and then wiped out by REMOVEFILTERS ( 'Product' ). That is wrong. The removal runs first and clears every filter on Product. Then the red filter is applied. The result is total red sales, with all other product slicers ignored. Swapping the two arguments in the text gives the same result.
This ordering is also what makes KEEPFILTERS behave in the next case:
Red Sales After Clearing Color =
CALCULATE (
[Total Sales],
REMOVEFILTERS ( 'Product'[Color] ),
KEEPFILTERS ( 'Product'[Color] = "Red" )
)KEEPFILTERS intersects the red filter with whatever is on Color at step 5. Step 4 has already cleared that column, so nothing is left to intersect with. The result is red sales, even if the slicer selected blue. Without the REMOVEFILTERS, the same measure returns blank when blue is selected.
Step 5: how remaining filters combine
Filters applied in step 5 combine in two ways:
- A normal Boolean or table filter on a column replaces the existing filter on that column.
- A
KEEPFILTERSfilter intersects with the existing filter.
Two arguments in the same CALCULATE that filter the same column are both new filters. They are combined with each other by intersection. So this expression is always blank:
Impossible Colors =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red",
'Product'[Color] = "Blue"
)The two conditions both apply, and no row can be red and blue at once. If you want either color, write a single argument such as 'Product'[Color] IN { "Red", "Blue" }.
Nested CALCULATE: the inner call starts from the outer result
When one CALCULATE is inside another, the outer one finishes building its new context first. The inner CALCULATE treats that context as its original context. Its own filters then apply on top:
Nested Colors =
CALCULATE (
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ),
'Product'[Color] = "Blue",
'Product'[Category] = "Bikes"
)The outer call sets Color to blue and Category to bikes. The inner call replaces the color with red and leaves the category alone. The result is red bikes. The inner filter wins on the column both set. The outer filter survives on the column the inner call never mentions.
Variables sit outside this order
The sequence above applies to the expression that CALCULATE evaluates. A variable is computed once, where it is declared, and its value is fixed from then on. The measure from the spine shows the trap:
Wrong Red Sales =
VAR t = [Total Sales]
RETURN
CALCULATE ( t, 'Product'[Color] = "Red" )Here t is evaluated in the original filter context, before CALCULATE starts. Step 6 only reads that stored number. The measure therefore returns the same value as [Total Sales]. To make the expression respond to the new filter, put the calculation inside CALCULATE and reference the measure or expression there, not a variable that holds its result. A variable can still be useful for a value that must stay fixed, such as capturing the selected year before you remove filters.
Sales vs Selected Year Baseline =
VAR SelectedYear = SELECTEDVALUE ( 'Date'[Year] )
RETURN
CALCULATE (
[Total Sales],
REMOVEFILTERS ( 'Date' ),
'Date'[Year] = SelectedYear - 1
)SelectedYear is read before the filters on Date are removed, so the later argument can still use it. If you wrote SELECTEDVALUE ( 'Date'[Year] ) inside the Boolean argument instead, it would be evaluated in the original context as well (step 1). The variable makes that timing visible in the code.
Debugging checklist
When a CALCULATE result surprises you, walk through the steps in order:
- Which filter arguments are table expressions, and what do they return in the original context?
- Is there a row context? If so, which columns does context transition filter, and does the row's key make a filter you did not expect?
- Is a relationship modifier changing the path filters take?
- Which removal functions run, and which filters do they clear?
- For each column, does a remaining filter replace, or does
KEEPFILTERSintersect? - Is any value in the expression coming from a variable that was computed earlier?
Exercise
A slicer selects the color Blue. What does each measure return, and why? Measure A: CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Color] = "Red" ) ). Measure B: CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Color] = "Red" ), REMOVEFILTERS ( 'Product'[Color] ) ).
Show answer
Measure A returns blank. KEEPFILTERS intersects the red filter with the existing blue selection, and no row is both. Measure B returns the total red sales. REMOVEFILTERS runs before the remaining filters, whatever the written order, so the blue selection is already cleared when KEEPFILTERS applies. The red filter then has nothing to intersect with.
Exercise
A calculated column on 'Product' is defined as CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ). Why does it return blank for blue products, and how would you change it to return total red sales on every row?
Show answer
Context transition runs before the filter arguments. It filters every column of the current row, including ProductKey. The Boolean argument replaces only the Color filter, so the ProductKey filter still limits the result to one blue product. No row matches red, so the result is blank. To get total red sales on every row, remove the transitioned filters in the same CALCULATE, for example by adding REMOVEFILTERS ( 'Product' ) as an argument. The removal runs after the transition and before the red filter is applied.
Exercise
Why does CALCULATE ( [Total Sales], FILTER ( Sales, Sales[Quantity] > 2 ) ) respect a year slicer, while using FILTER ( ALL ( Sales ), Sales[Quantity] > 2 ) still respects the slicer even though the filter table ignores it?
Show answer
Filter arguments are evaluated in the original context, so Sales inside FILTER already reflects the year slicer in the first version. In the second version, ALL removes those filters from the table being scanned, so the filter table contains every row with quantity above 2. That filter table is applied as a new filter on Sales, but the year slicer filters Date, a different table. The two filters combine, so the year slicer still limits the result.