Sub topic 9
Context transition: from row context to filter context
This chapter assumes you know basic DAX measures and calculated columns, and that you work with a star schema, which has fact tables on the many side of one-to-many relationships. It also assumes you know how slicers, rows and columns filter a measure. It builds on the earlier chapters about row context and on how CALCULATE replaces filters.
Why row context needs help
A row context is an iteration state. It says "the current row of this table is row N", and it lets you read column values for that row. A row context does not filter anything. It does not reach related tables through relationships in the filtering sense, and it does not change what an aggregation such as SUM sees.
That produces a classic surprise. Suppose you add this calculated column to the Customer table:
Customer Sales (wrong) = SUM ( Sales[Amount] )Every customer row shows the same number: the total of all sales. The calculated column is evaluated once per Customer row, so a row context exists. But SUM reads the filter context, and the filter context is empty. The row context on Customer does not filter Sales.
Context transition is the mechanism that fixes this. It converts the current row of a row context into an equivalent set of filters in the filter context. CALCULATE performs that conversion.
What context transition does
When CALCULATE (or CALCULATETABLE) runs inside a row context, it does these things:
- It takes every column of the table being iterated, together with the value of each column in the current row.
- It turns those values into filters, one equality filter per column.
- It adds those filters to the filter context, then evaluates its expression.
Here is the corrected calculated column:
Customer Sales = CALCULATE ( SUM ( Sales[Amount] ) )For the customer with key 17, CALCULATE produces filters equivalent to Customer[CustomerKey] = 17, plus a filter on every other column with the value in that row. The filter on Customer then flows through the relationship to Sales, and SUM adds only that customer's rows.
You do not need to remember "one filter per column". It is enough to know that the whole row becomes a filter. If the table has a unique key column, that key alone identifies the row, so the result is the rows for exactly that one entity.
Measures are wrapped in CALCULATE automatically
Every reference to a measure is evaluated as if it were wrapped in CALCULATE. This is called the implicit CALCULATE around a measure. It is the reason the following measure works:
Average Sales per Customer =
AVERAGEX ( Customer, [Total Sales] )AVERAGEX creates a row context on Customer, one row at a time. For each row, the reference [Total Sales] behaves like CALCULATE ( [Total Sales] ). That triggers context transition, so [Total Sales] is computed for that single customer. AVERAGEX then averages those per-customer values.
Compare this with a raw column aggregation inside an iterator:
Average Sales per Customer (wrong) =
AVERAGEX ( Customer, SUM ( Sales[Amount] ) )SUM ( Sales[Amount] ) is not a measure. It is a plain expression, so there is no implicit CALCULATE. There is no context transition, and every iteration returns the same total. The result is the grand total of the current filter context, not an average per customer.
You can fix it in either of two ways:
Average Sales per Customer A =
AVERAGEX ( Customer, CALCULATE ( SUM ( Sales[Amount] ) ) )
Average Sales per Customer B =
AVERAGEX ( Customer, [Total Sales] )Defining a measure and referencing it, as in version B, is usually clearer. It also makes the transition easy to miss, so keep the rule in mind: a measure reference inside an iterator always transitions.
Context transition and filter replacement
CALCULATE with a row context both adds the transition filters and applies its own filter arguments. Transition filters are applied in the same step as the other filter arguments, after the original filter context has been established. Because they are equality filters on columns, they replace any existing filter on those same columns.
This has a useful consequence. Suppose a report visual has a slicer on Customer[Country] that selects France. You then evaluate [Total Sales] inside an iterator over Customer. For each customer row, the transition sets filters on every Customer column, including Country. For a customer from Germany that would replace the France selection with Germany. In practice the iterator only visits rows that are visible in the current filter context, so the iterated rows still come from France. The transition does not widen the selection, because the iteration itself was already constrained by it.
The replacement matters when you iterate over a table that is not filtered the same way, such as ALL ( Customer ):
Average Sales per Customer (All) =
AVERAGEX ( ALL ( Customer ), [Total Sales] )Now every customer is visited, regardless of the slicer. For each, the transition filters override the France selection on Customer, so each customer returns their own total. Then the average runs over all customers in the model.
Duplicate rows and non-unique keys
Context transition filters on all columns of the iterated table, not only the key. If two rows in the iterated table have identical values in every column, the transition filter matches both rows. Each of the two rows then reports the combined result for both.
This matters in two situations:
- A table that has no unique key, such as a staging table or a table you built with
SELECTCOLUMNS. Duplicate rows return doubled values. - An iteration over a table expression that you shaped yourself.
Consider this measure:
Sum of Customer Sales Naive =
SUMX ( Customer, [Total Sales] )If Customer has a unique key, each customer's total is counted once, and the sum equals the grand total for customers that have sales. If Customer contained two identical rows for the same person, each row would report that person's full total, and the sum would count it twice. The standard model guarantees uniqueness on the dimension key, which is one reason star schemas are recommended.
Performance cost
Context transition is not free. For each row of the iterated table, the engine builds a new filter context and evaluates the expression. If the table has 100,000 customers, a measure reference inside AVERAGEX ( Customer, ... ) runs the base expression 100,000 times.
Guidelines that follow from this:
- Iterate over the smallest table that produces the right answer, usually a dimension and not a fact table.
- Avoid iterating a large fact table and calling a measure on each row. A row of a fact table is usually unique only by combination of many columns, so the transition filter is large and slow.
- Prefer a column-level aggregation inside the iterator when no transition is needed.
Worked example
Suppose you want to count how many customers bought more than 1,000 in total in the current selection.
Big Customers =
COUNTROWS (
FILTER (
Customer,
[Total Sales] > 1000
)
)FILTER iterates Customer, which creates a row context. In each iteration, [Total Sales] transitions the current customer into a filter and returns that customer's total. The comparison keeps customers above 1,000. COUNTROWS counts them.
Now remove the measure and use a column aggregation:
Big Customers (wrong) =
COUNTROWS (
FILTER (
Customer,
SUM ( Sales[Amount] ) > 1000
)
)There is no transition here. SUM ( Sales[Amount] ) returns the same total for every customer. Either all customers pass the test or none do, so the result is either the number of visible customers or zero.
Variables and transition
A variable stores a value at the point where it is defined. Context transition cannot change it afterwards. The topic spine example shows this:
Wrong Red Sales =
VAR t = [Total Sales]
RETURN
CALCULATE ( t, 'Product'[Color] = "Red" )t is computed once in the original filter context. CALCULATE then changes the filter context, but t is already a number, so it does not change. The measure returns the same result as [Total Sales].
The same applies to variables in an iterator. If you define a variable that holds a measure value before the iteration starts, the value is fixed. To get per-row values, evaluate the measure inside the iterator, or define the variable inside the iterator body.
Average Sales per Customer Var =
AVERAGEX (
Customer,
VAR customerSales = [Total Sales]
RETURN customerSales
)Here the variable is defined inside the iterated expression, so it is evaluated once per row, with the transition applied.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| `SUM` of a fact column in a calculated column on a dimension | Every row shows the grand total | Wrap in `CALCULATE`, or use a measure |
| Column aggregation inside `AVERAGEX`/`SUMX` over a dimension | Same value on every iteration | Reference a measure, or wrap in `CALCULATE` |
| Variable defined outside the iterator | Value is frozen; no per-row result | Define the variable inside the iterator |
| Iterating a large fact table and calling a measure | Slow queries and large transition filters | Iterate a dimension, or restructure the logic |
| Iterating a table with duplicate rows | Values are counted more than once | Use a table with a unique key, or add a key column |
Debugging a transition
When a result looks wrong, ask these questions in order:
- Is there a row context at this point? Iterators (
SUMX,AVERAGEX,FILTER) and calculated columns create one. - Does the expression inside contain a measure reference or an explicit
CALCULATE? If not, there is no transition. - Which table is iterated, and does it have a unique key? If it has none, the transition may match several rows.
- Is the filter from the transition able to reach the table you aggregate? It must flow through a relationship, from the one side to the many side.
A quick test is to temporarily return COUNTROWS ( Sales ) inside the iterator and view the per-row values. If they are all equal, no transition is taking place.
Exercise
A calculated column on the Customer table is defined as = SUM ( Sales[Amount] ). Every row shows the same large number. Explain why, and give two ways to fix it.
Show answer
A calculated column creates a row context on Customer, but a row context does not filter Sales, and SUM reads only the filter context, which is empty. So each row returns the total of all sales. Fix it by wrapping the aggregation in CALCULATE, as in = CALCULATE ( SUM ( Sales[Amount] ) ), or by referencing a measure, as in = [Total Sales]. Both trigger context transition, which turns the current Customer row into a filter that flows to Sales.
Exercise
What does this measure return, and why? Test = VAR s = [Total Sales] RETURN AVERAGEX ( Customer, s )
Show answer
It returns the same value as [Total Sales] in the current filter context. The variable s is evaluated once before the iteration starts, so it holds a single number. AVERAGEX averages that same number across all visible customers, and the average of identical values is that value. To get an average per customer, reference [Total Sales] inside the iterator, or define the variable inside the iterated expression.