Sub topic 3
Row Context and Why It Does Not Filter
This chapter assumes you can write basic DAX measures and calculated columns, that you know a star schema with one-to-many relationships, and that you understand how slicers and report rows filter a measure. It covers row context, the second kind of context in DAX, and shows why it does not filter other tables.
Two contexts, two jobs
DAX evaluates every expression inside a context. You have seen filter context: a set of filters, from slicers, rows, columns and CALCULATE, that decides which rows of each table an aggregation can see.
Row context is different. It means "the current row". It exists while DAX walks through a table one row at a time, and it lets you refer to column values in that row.
| Row | Filter context | Row context |
|---|---|---|
| Question it answers | Which rows are visible? | Which row am I on? |
| Created by | Slicers, report rows and columns, CALCULATE | Calculated columns, iterators such as SUMX and AVERAGEX |
| Effect on other tables | Propagates through relationships | None |
| Lets you read a column directly | No, you need an aggregation | Yes |
Where row context comes from
A calculated column has a row context automatically. DAX evaluates the formula once for each row of the table.
Line Total = Sales[Quantity] * Sales[Net Price]For each row of Sales, Sales[Quantity] returns the quantity in that row. No aggregation is needed, because the row context supplies a single value.
Iterator functions create a row context too. They take a table, visit each row, evaluate an expression, and then combine the results.
Total Line Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Net Price] )While SUMX runs, each row of Sales is the current row. The expression sees that row's quantity and price.
Row context does not filter
Row context identifies a row, but it does not restrict any table. Consider this calculated column on the Customer table:
Customer Sales (Wrong) = SUM ( Sales[Amount] )Every customer shows the same number: the total of the whole Sales table. The row context points at one customer, but SUM aggregates Sales under the current filter context. That filter context is empty, so every Sales row is visible.
Row context also does not follow relationships. The relationship from Customer to Sales propagates filters, not rows. Being "on" a customer row tells Sales nothing.
The diagram shows the difference.
Reading related values
Row context does let you read columns from the current row, and RELATED follows a many-to-one relationship from that row to the "one" side. In a calculated column on Sales:
Product Cost Total = Sales[Quantity] * RELATED ( 'Product'[Unit Cost] )This works because each Sales row points to exactly one product. RELATED fetches a single value. It does not filter anything. In the other direction, from one customer to many sales rows, RELATED has nothing to return, because the row would match many rows.
Row context with measures
Now use an iterator over Customer and reference a column from another table:
Sum of Amounts per Customer =
SUMX ( Customer, SUM ( Sales[Amount] ) )A matrix with years on rows shows the same value for every customer within a year. For each customer, SUM ( Sales[Amount] ) sees only the outer filter context, the selected year, because row context on Customer does not filter Sales. The result is the yearly total multiplied by the number of customers, which is almost never what you want.
Replace the inner SUM with a measure reference and the result changes:
Average Sales per Customer =
AVERAGEX ( Customer, [Total Sales] )A measure reference is wrapped in an implicit CALCULATE, and CALCULATE converts the current row into filters. This is called context transition. It is the only mechanism that turns row context into filtering, and a later chapter covers it in full. For now, remember that the row context alone did nothing. The CALCULATE did the filtering.
Common errors
- Same value on every row of a calculated column. An aggregation such as
SUMignores the row context. Wrap it inCALCULATEor useRELATEDandRELATEDTABLEwhere the relationship allows it. - Using a bare column in a measure.
Sales[Amount]in a measure has no row context, so DAX returns an error. Aggregate it withSUM, or iterate withSUMX. - Expecting an iterator to filter the table it reads from. Row context on one table does not restrict a different table.
Exercise
A calculated column on the Customer table is defined as Total = SUM ( Sales[Amount] ). Every customer shows the same value, equal to the grand total of Sales. Why does this happen, and how would you change the formula so each customer shows only their own sales?
Show answer
A calculated column provides a row context on Customer, but row context does not filter Sales. SUM runs in an empty filter context, so it sees every Sales row. Wrapping the aggregation in CALCULATE fixes it: Total = CALCULATE ( SUM ( Sales[Amount] ) ). CALCULATE performs context transition, which turns the current Customer row into a filter on Customer. That filter propagates through the relationship to Sales.