Sub topic 12
Context transition: bridging row context and filter context
This chapter assumes you can write basic DAX, know the difference between row context and filter context, and have a star-schema model with a Sales table and a Product table related one-to-many. Earlier chapters covered the two contexts separately. Here you will see how DAX moves from one to the other, and why that move is the source of many surprising results.
The problem: a row context does not filter
Row context means "the current row of a table." It lets an expression read columns of that row. It does not filter any table. Related tables are not narrowed by it, and neither are aggregations.
Consider a calculated column on the Product table that tries to count that product's sales rows:
Sales Rows (wrong) = COUNTROWS(Sales)In a calculated column, DAX evaluates this expression once per Product row. But COUNTROWS(Sales) reads the Sales table through the filter context, and a calculated column starts with an empty filter context. Every product therefore shows the total number of rows in Sales. The row context on Product has no effect on Sales.
A row context only gives you the column values of the current row. A filter context is what restricts the rows an aggregation sees. To get the per-product count, you need to turn the row context into a filter context.
What context transition is
Context transition is the conversion of the current row context into an equivalent filter context. DAX does this when CALCULATE or CALCULATETABLE is evaluated inside a row context. The values of every column in the current row become filters, and the expression is then evaluated under those filters.
The fix for the column above is to wrap the expression in CALCULATE:
Sales Rows = CALCULATE(COUNTROWS(Sales))For each Product row, DAX takes the row's column values, applies them as filters on the Product table, and lets the relationship carry those filters to Sales. Each product now shows its own sales row count.
Measures get context transition for free
A measure reference behaves as if it were wrapped in CALCULATE. DAX adds the wrapper automatically. This is the key link between the two objects.
Take the measure from earlier chapters:
Total Sales = SUM(Sales[Line Amount])Now use it in a calculated column on Product:
Product Sales = [Total Sales]Written this way, the column returns each product's own sales total. The reference [Total Sales] is evaluated as CALCULATE(SUM(Sales[Line Amount])), so the current Product row becomes a filter. If you instead wrote SUM(Sales[Line Amount]) directly, with no measure and no CALCULATE, every product would show the grand total.
This also explains a common pattern. Iterators such as SUMX create a row context, and a measure called inside them is evaluated once per row, with each row transitioned to a filter:
Average Sales per Product =
AVERAGEX(Product, [Total Sales])AVERAGEX steps through the Product rows. For each row, [Total Sales] sees that product's filter and returns that product's total. AVERAGEX then averages the totals. Without the measure, each iteration would return the same grand total.
What the transition filters on
The transition does not filter on only the key column. It filters on all columns of the current row. This has two consequences.
First, the filter is as specific as the row. For a Product table with columns ProductKey, Name, Color and Category, the transition applies a filter for every one of them at once. If the table has a unique key, the combination identifies one row, which is what you want.
Second, if the table has no unique key, duplicate rows share the same column values and therefore the same filter. Each duplicate then sees the combined data of all its duplicates. Here is a small example. A Sales table has two identical rows with no key column:
| Product | Quantity |
|---|---|
| Hat | 2 |
| Hat | 2 |
A calculated column CALCULATE(SUM(Sales[Quantity])) on this table returns 4 for both rows, not 2. Both rows produce the filter Product = "Hat" and Quantity = 2, and both rows match both filters. Keep a unique key on tables where you rely on context transition.
Comparing the three versions
The table below shows how the same Product-level question behaves with and without the transition.
| Expression in a Product calculated column | Result for each product |
|---|---|
| `SUM(Sales[Line Amount])` | Grand total of all sales, the same on every row |
| `CALCULATE(SUM(Sales[Line Amount]))` | That product's sales total |
| `[Total Sales]` | That product's sales total |
The second and third rows are equivalent. Prefer the measure reference when you already have a measure. It keeps the logic in one place and makes the transition visible by name.
Transition inside a measure
Context transition also occurs in measures, whenever an iterator supplies the row context. The spine's earlier measure Total Sales (no column) uses SUMX over Sales without a transition, because it only reads columns of the current row. A transition is needed only when the expression inside the iterator reads a table that the row context does not filter.
Here is a measure that counts customers whose total sales exceed 1,000. It assumes a Customer table related to Sales:
Big Customers =
COUNTROWS(
FILTER(
Customer,
CALCULATE(SUM(Sales[Line Amount])) > 1000
)
)FILTER iterates over Customer and creates a row context for each customer. The CALCULATE inside converts that row into a filter, so the SUM returns that customer's own total. The comparison is then true or false per customer, and COUNTROWS counts the true ones. You could replace the CALCULATE(SUM(...)) with a measure reference such as [Total Sales] with the same result.
Cost and common errors
Context transition is not free. For every row of the iterated table, DAX builds a filter and runs a separate evaluation of the expression. Over a table with millions of rows, this multiplies the work. Three habits keep this under control:
- Iterate over a small table, such as Customer or Product, rather than over a large fact table, when you want per-entity results.
- Avoid wrapping a transition around a table that has many columns, since the transition filters on every column.
- Avoid calling an expensive measure inside an iterator on a large table unless you need the per-row result.
Common errors to look for:
- A calculated column that aggregates a related table and returns the same number on every row. The cause is a missing
CALCULATE. - A per-row result that is too large on a table without a unique key. The cause is duplicate rows sharing a filter.
- A calculated column that uses a measure and is slow to refresh. The cause is a transition per row, evaluated at refresh time. If the value does not need to be a column, move the logic into a measure.
- Expecting a transition when the table is only reached through
RELATED.RELATEDfollows a relationship from the many side to the one side and reads a value. It does not need a transition and does not use one.
Putting it together
A calculated column evaluates in a row context and starts with an empty filter context. A measure evaluates in the filter context of a visual cell. CALCULATE, and measure references, let a row context hand over its values as filters. This is why a measure can be used inside a calculated column or an iterator and still return a per-row answer.
Exercise
A calculated column on the Product table is defined as Units Sold = SUM(Sales[Quantity]). Every product shows the same large number. Name the cause and write the corrected column.
Show answer
The column is evaluated in a row context on Product, but SUM reads Sales through the filter context, which is empty in a calculated column, so every row returns the total for all of Sales. Wrapping the expression in CALCULATE triggers context transition, which turns the current Product row into a filter that reaches Sales through the relationship: Units Sold = CALCULATE(SUM(Sales[Quantity])).
Exercise
A Sales table has no key column and contains two identical rows: Product "Hat", Quantity 2. A calculated column Row Qty = CALCULATE(SUM(Sales[Quantity])) is added. What value does each of the two rows show, and why?
Show answer
Each row shows 4. The transition filters on all columns of the current row, so each row produces the filter Product = "Hat" and Quantity = 2. Both rows match that filter, so SUM adds both quantities. A unique key column would separate the rows.