Sub topic 3
Row Context and Why It Does Not Filter
This chapter assumes you can write basic DAX measures and calculated columns. It also assumes you have a star-schema model with one-to-many relationships, and that you know how slicers, rows and columns of a report filter a measure. The previous chapters covered filter context and how filters travel along relationships. Here you meet the second kind of context, the row context. Most confusion about CALCULATE comes from mixing the two up.
Two contexts, two jobs
DAX evaluates every expression inside a context. There are two kinds, and they do different work.
- A filter context is a set of filters on columns. It decides which rows of the model are visible to an aggregation such as
SUM. Slicers, report rows and columns, and CALCULATE arguments create it. - A row context is a pointer to one row of one table. It lets an expression read the column values of that row. It does not hide or show any other rows.
The second sentence about row context is the point of this chapter. A row context tells DAX which row you are standing on. It never restricts what an aggregation sees.
| Property | Filter context | Row context |
|---|---|---|
| What it holds | A set of filters on columns | A single current row of a table |
| Effect on aggregations | Restricts the rows they scan | None |
| Propagates through relationships | Yes, along the filter direction | Not by itself |
| Created by | Slicers, visuals, CALCULATE | Calculated columns, iterators |
Where row context comes from
Row context appears in two places.
- Calculated columns. DAX evaluates the column expression once for each row of the table, with that row as the current row.
- Iterators. Functions whose names end in X, such as
SUMX,AVERAGEX,MAXXandFILTER, walk through a table row by row. They create a row context for each row while they evaluate their expression argument.
Here is a calculated column on the Sales table:
Line Amount = Sales[Quantity] * Sales[Net Price]For each row of Sales, DAX reads Quantity and Net Price from that same row and multiplies them. No filter is involved. The expression simply reads the current row.
An iterator does the same thing inside a measure:
Total Line Amount =
SUMX (
Sales,
Sales[Quantity] * Sales[Net Price]
)SUMX takes the rows of Sales that are visible in the current filter context. For each of those rows it opens a row context, evaluates the multiplication, and then adds up the results. Notice that two things cooperate here. The filter context decides which rows SUMX iterates. The row context decides which row the multiplication reads.
Why row context does not filter
Suppose you write this calculated column on the Customer table:
Customer Sales (wrong) = SUM ( Sales[Amount] )You might expect each customer row to show that customer's sales. Instead, every row shows the same number: the total of Sales[Amount] for the whole model. The reason is that the row context on Customer only identifies a Customer row. SUM is an aggregation, and aggregations read the filter context. A calculated column has no filter context by default, so SUM sees all rows of Sales.
Row context also does not travel along relationships the way filter context does. A filter on Customer flows to Sales through the one-to-many relationship. A current row on Customer does not. The current Customer row is not a filter, so Sales is not reduced.
This explains a common surprise. You can read a column from the current row of a table, but an aggregation on a related table ignores the current row.
Reading across relationships with RELATED
Row context does follow relationships in one limited way. From the many side you can read a column on the one side with RELATED:
Line Amount with Discount =
Sales[Quantity] * Sales[Net Price] * ( 1 - RELATED ( 'Product'[Discount Rate] ) )Each Sales row points to exactly one Product row, so RELATED can follow the relationship and return a single value. The reverse direction is different. From the one side, one Customer row matches many Sales rows, so there is no single value to return. RELATEDTABLE returns the whole set of matching rows as a table:
Customer Order Lines = COUNTROWS ( RELATEDTABLE ( Sales ) )This works because RELATEDTABLE is itself a CALCULATE in disguise. It applies the context transition described in the next section. That is why it can narrow Sales to the current customer.
Context transition: turning a row into a filter
DAX has one mechanism that converts a row context into a filter context. It is called context transition. When CALCULATE runs inside a row context, it takes the values of all the columns of the current row and adds them as filters. The expression passed to CALCULATE is then evaluated with those filters in place.
You can fix the wrong calculated column from earlier by wrapping the aggregation in CALCULATE:
Customer Sales = CALCULATE ( SUM ( Sales[Amount] ) )In each Customer row, CALCULATE turns the row into filters such as Customer[CustomerKey] = 1042, Customer[Name] = "Ana" and so on for every column. The filters travel through the relationship to Sales, and SUM now adds up only that customer's sales.
Measures have an implicit CALCULATE
A reference to a measure behaves as if it were wrapped in CALCULATE. So these two calculated columns return the same result:
Customer Sales A = CALCULATE ( SUM ( Sales[Amount] ) )Customer Sales B = [Total Sales]The second one works because [Total Sales] is a measure, defined as SUM ( Sales[Amount] ). Referencing it inside a row context triggers context transition automatically. Referencing the bare column expression SUM ( Sales[Amount] ) does not.
This is also what makes the following measure work:
Average Sales per Customer =
AVERAGEX ( Customer, [Total Sales] )AVERAGEX iterates the visible customers. For each one it opens a row context, evaluates [Total Sales], and the implicit CALCULATE converts that row into a filter on that customer. The result is that customer's sales. AVERAGEX then averages those values. If you replaced [Total Sales] with SUM ( Sales[Amount] ), every customer would return the same grand total, and the average would equal that total.
Edge cases and costs
Context transition is powerful, and it has several consequences you should know about.
Duplicate rows
Context transition filters on every column of the current row. If a table has no key and contains two identical rows, both rows produce the same filter. Each of them then sees the combined data of both. For example, with two identical rows in a table that has no unique column, a measure referenced in an iterator over that table returns the doubled value for each row. Tables that you iterate with a measure should have a unique key, or you should be aware of this behavior.
Existing filters on the same columns are replaced
Context transition behaves like a CALCULATE filter argument. Its column filters replace any existing filter on those same columns. In an iterator over VALUES ( Customer[Country] ), the transition filters only Country, so an existing filter on Customer[City] stays in place and combines with it.
Performance
Each context transition evaluates the expression once per row. Iterating a table with millions of rows and calling a measure on each can be slow. Iterate over a small table when you can, such as VALUES ( Customer[CustomerKey] ) or a dimension table, and keep the inner measure simple.
Row context in a measure without an iterator
A measure has no row context unless an iterator creates one. Writing Sales[Quantity] * Sales[Net Price] directly in a measure gives an error, because DAX cannot tell which row to read. Use SUMX to supply the row context.
Nested row contexts
Row contexts can stack. An iterator inside another iterator keeps both current rows available:
Sales Above Category Average =
SUMX (
VALUES ( 'Product'[Category] ),
VAR CategoryAvg =
AVERAGEX ( RELATEDTABLE ( Sales ), Sales[Amount] )
RETURN
SUMX (
RELATEDTABLE ( Sales ),
IF ( Sales[Amount] > CategoryAvg, Sales[Amount] )
)
)The outer SUMX holds the current category. The inner iterators run over the Sales rows of that category and hold the current sale. The variable CategoryAvg is computed once per category, before the inner loop. Each inner row can read both its own columns and values from the outer level. Be careful when two nested tables share a column name, because a reference such as Sales[Amount] always refers to the innermost row context of the Sales table.
Summary of the rules
- A row context identifies a current row. It never filters other tables or other rows.
- Aggregations such as
SUM,COUNTROWSandAVERAGEread the filter context only. - Iterators and calculated columns create row contexts.
RELATEDandRELATEDTABLEfollow relationships from a row context.- CALCULATE, and any measure reference, converts the current row into filters. This is context transition.
Check your understanding
Exercise
A calculated column on the Product table is defined as Product Revenue = SUM ( Sales[Amount] ). Every row shows the same value, equal to total revenue of the whole model. Explain why, and rewrite the column so each row shows revenue for that product only.
Show answer
The calculated column has a row context on Product, but SUM reads the filter context. No filter context exists on Product in a calculated column, so SUM adds up every row of Sales. A row context does not filter, and it does not propagate to Sales. Wrap the aggregation in CALCULATE to trigger context transition: Product Revenue = CALCULATE ( SUM ( Sales[Amount] ) ). Referencing a measure such as [Total Sales] also works, because a measure reference has an implicit CALCULATE.
Exercise
You write Customer Avg = AVERAGEX ( Customer, SUM ( Sales[Amount] ) ) and every customer contributes the same number. What change makes AVERAGEX compute the average of each customer's own sales, and why does it work?
Show answer
Replace SUM ( Sales[Amount] ) with the measure [Total Sales], or with CALCULATE ( SUM ( Sales[Amount] ) ). The iterator creates a row context on Customer, but the bare SUM ignores it. A measure reference or CALCULATE triggers context transition, which converts the current Customer row into filters. The filters propagate to Sales, so the SUM returns that customer's total.
Exercise
Why does RELATED ( 'Product'[Discount Rate] ) work in a calculated column on Sales, while RELATED ( Sales[Amount] ) does not work in a calculated column on Product? Which function should you use for the second case?
Show answer
RELATED follows a relationship from the many side to the one side, where each row matches exactly one row, so it can return a single value. From Product, one row matches many Sales rows, so no single value exists. Use RELATEDTABLE ( Sales ) to get the matching rows as a table, and aggregate it, for example SUMX ( RELATEDTABLE ( Sales ), Sales[Amount] ).