Context Transition, Explained with Three Bugs I Wrote
Context transition is the rule that makes DAX feel like magic until it doesn’t. Inside a row context, such as an iterator or a calculated column, CALCULATE turns the current row into a filter. Every measure reference gets an implicit CALCULATE, so it happens there too.
The rule itself is one sentence. The bugs live in what happens around it. Here are three I wrote for this site, each caught by a review rather than a test.
Bug 1: The Filter Argument That Ran Too Early
The shared Average Inventory measure needed each month’s snapshot date, found with every filter except the date cleared, so a part with no stock that month would count as zero. The first version, inside an ADDCOLUMNS over months, looked like this:
"@Snapshot",
CALCULATE (
MAX ( fact_inventory_snapshot[snapshot_date] ),
REMOVEFILTERS ( fact_inventory_snapshot ),
VALUES ( dim_date[date] )
)
The intent: clear the fact table, then put this month’s dates back. Two things went wrong.
CALCULATEevaluates its filter arguments in the context it was called from, before context transition applies the row. SoVALUES ( dim_date[date] )returned every date in the 12-month window, not this month’s.REMOVEFILTERSon the fact table clears its expanded table, which includesdim_date. That wiped out the month filter the transition had just added.
Every month resolved to the last snapshot in the window. On the demo data the average would have read about a ninth of the real figure. The fix captures the month first, in a step where the transition has already happened:
"@Snapshot",
VAR MonthDates =
CALCULATETABLE ( VALUES ( dim_date[date] ) )
RETURN
CALCULATE (
MAX ( fact_inventory_snapshot[snapshot_date] ),
REMOVEFILTERS ( fact_inventory_snapshot ),
MonthDates
)
CALCULATETABLE with no filter arguments does nothing but context transition, so MonthDates holds this month’s dates. Passed as a filter argument, it is applied after REMOVEFILTERS, which restores exactly the month.
Bug 2: The Rule That Said to Delete a CALCULATE
I added a DAX Lint note for CALCULATE with no filter arguments, because at the top of a measure it changes nothing: a measure already runs in the filter context of the visual. The note was right for measures and wrong in two other places.
In an iterator, CALCULATE ( SUM ( Sales[Amount] ) ) is how you get a per-row total. The rule skipped iterators it knew about, but its list was missing MEDIANX, PERCENTILEX.INC and the rest of the statistical X functions. In a calculated column it is the textbook use:
Customer Sales = CALCULATE ( SUM ( Sales[Amount] ) )
Without CALCULATE, that column returns the grand total on every row, because a calculated column has a row context but no filter context. With it, the row’s customer becomes a filter. The Model Documenter was running the rule on calculated columns as if they were measures. Both are fixed: the iterator list is complete and calculated columns are linted in column mode.
The lesson is that “a CALCULATE with nothing in it” is only redundant where there is no row to transition.
Bug 3: Two Ways to Filter, Two Different Answers
The changeover template counted closed changeovers like this:
Changeover Count =
CALCULATE (
COUNTROWS ( fact_changeover_events ),
NOT ISBLANK ( fact_changeover_events[first_good_part_time] )
)
and summed their minutes with SUMX ( FILTER ( fact_changeover_events, NOT ISBLANK ( ... ) ), ... ). They look like the same filter. They are not.
A boolean filter argument in CALCULATE is shorthand for FILTER ( ALL ( column ), condition ). The ALL replaces any existing filter on that column. So with a report filter on first_good_part_time, such as “last 7 days”, the count covered every closed changeover ever, while the FILTER inside SUMX kept the report filter and summed only the last week. The average divided a week of minutes by all of history.
The fix is to filter both with the same FILTER, or wrap the boolean in KEEPFILTERS, which intersects with the existing filter instead of replacing it:
Changeover Count =
COUNTROWS (
FILTER (
fact_changeover_events,
NOT ISBLANK ( fact_changeover_events[first_good_part_time] )
&& NOT ISBLANK ( fact_changeover_events[changeover_start] )
)
)
What the Three Have in Common
None of them is a syntax error, and none breaks on the happy path. Each depends on when something is evaluated or which filter wins, and that is invisible in the text unless you know to look. That is why the tests missed all three and a reader who knew the rules caught them.
If you remember one thing: filter arguments are evaluated before the row arrives, boolean filters replace, and KEEPFILTERS and CALCULATETABLE are how you take control of both. The Field Note on how this site tests its DAX covers what checks it runs, and what it took to catch these.