Sub topic 10
Context transition: from row context to filter context
This chapter assumes you know the difference between a measure and a calculated column, and that you can read basic SUMX and CALCULATE expressions. It also assumes you have seen how filter context and row context work separately. Here we connect the two.
The problem: a row context does not filter
A row context points at one row, but it does not filter any table. Relationships do not carry it to other tables either. Look at this calculated column in the Product table.
-- Calculated column in the Product table
Sales Without Transition =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )SUMX iterates all of Sales. The row context on the current Product row does not restrict Sales, so every product row shows the same grand total.
The fix: context transition
Context transition is the step where CALCULATE takes the columns of the current row and turns them into filters. The row context becomes an equivalent filter context. That filter then follows the relationship from Product to Sales.
The measure reference below is wrapped in an implicit CALCULATE, so the transition happens.
-- Calculated column in the Product table
Product Sales = [Sales Amount]Each product now shows its own sales. The current Product row becomes a filter, and Sales is filtered through the relationship.
Where transition happens
Transition happens whenever CALCULATE runs while a row context exists. Three situations cover most cases:
- A measure reference inside a row context. A measure is an implicit
CALCULATE, so[Sales Amount]in a calculated column or inside an iterator triggers it. - An explicit
CALCULATEinside a row context. The transition applies before the filter arguments are evaluated. - A measure reference inside an iterator. The iterator opens one row context per row, and each row gets its own transition.
Here are two iterator examples.
Average Sales per Customer =
AVERAGEX ( Customer, [Sales Amount] )AVERAGEX opens a row context on each Customer row. The measure reference turns that row into a filter, so each customer's sales are computed separately and then averaged.
Products Over 1000 =
COUNTROWS (
FILTER ( Product, [Sales Amount] > 1000 )
)FILTER iterates Product. For each product, transition makes [Sales Amount] evaluate for that product only. The measure counts the products whose sales exceed 1000.
What the transition filters on
The new filter contains every column of the current row, not only the key. The filter keeps the rows of the table whose values match all of those columns.
This has a consequence. If a table has two rows with identical values in every column, both rows produce the same filter. Each of those rows then sees the combined result of both rows. Rows with identical values are why a table without a unique key can give surprising totals. A key column makes each row's filter unique.
Cost
Transition is not free. For each row in the iteration, DAX evaluates the measure in a new filter context. Iterating a table with millions of rows and calling a measure on each row can be slow. Iterate over a small table, such as Product or Customer, when you can, and avoid iterating a large fact table with a measure inside.
Keeping outer rows apart
When a row context is nested, transition filters only on the current row of the context that CALCULATE sees. If you need a value from an outer row inside an inner iteration, capture it in a variable first. A variable keeps the two row contexts apart and avoids EARLIER.
-- Calculated column in the Product table
Price Rank =
VAR CurrentPrice = Product[Unit Price]
RETURN
COUNTROWS ( FILTER ( Product, Product[Unit Price] > CurrentPrice ) ) + 1CurrentPrice holds the price of the outer product row. Inside FILTER, Product[Unit Price] refers to the inner row, and the comparison reads clearly.
Exercise
A calculated column in the Product table is defined as Total = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ). Every product shows the same number. Change the formula so that each product shows only its own sales, and say why the change works.
Show answer
Replace the formula with a measure reference, for example Total = [Sales Amount], or wrap the expression in CALCULATE, as in CALCULATE ( SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ) ). Both run CALCULATE inside the row context of the Product row. Context transition turns that row into a filter on Product, and the relationship passes the filter to Sales, so only that product's sales rows are iterated.