Sub topic 3
Row Context: Working on One Row at a Time
This chapter assumes you know tables, columns, and one-to-many relationships. It also assumes you can write simple aggregations such as SUM and COUNTROWS, and that you know a measure from a calculated column. Filter context was covered in the previous chapter. Here we focus on its counterpart.
What a row context is
A row context is a pointer to one row of one table. While it exists, a formula can read the columns of that row directly, without an aggregation function. A row context does not filter anything. It only says "the current row is this one."
Two things create a row context:
- A calculated column. DAX evaluates the formula once for each row of the table.
- An iterator function such as SUMX. It steps through a table and evaluates an expression for each row.
A measure on its own has no row context. That is why Sales[Quantity] * Sales[Unit Price] fails as a measure body: no current row exists to read from.
Row context in a calculated column
The formula below runs once per row of Sales. It reads Quantity and Unit Price of the current row.
-- Calculated column in the Sales table
Line Amount = Sales[Quantity] * Sales[Unit Price]Sales is on the many side of a relationship to Product. The row context of a Sales row can follow that relationship to the single related Product row with RELATED.
-- Calculated column in the Sales table, using the relationship
Product Category = RELATED ( Product[Category] )Row context in an iterator
An iterator takes a table and an expression. It creates a row context on each row of the table, evaluates the expression, and combines the results. SUMX adds them up.
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )The table argument is read in the current filter context. If a slicer selects the year 2025, SUMX iterates only the Sales rows for 2025. So both contexts work together: filter context decides which rows are iterated, and row context points at each of them in turn.
A row context does not filter other tables
This is the most common mistake at this stage. Consider a calculated column in the Product table:
-- Calculated column in the Product table
Sales Without Transition =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )You might expect each product to show its own sales. Instead, every row shows the same grand total. The reason is that the row context points at a Product row, but it does not filter Sales. SUMX iterates all Sales rows, because no filter context restricts them.
A row context sees its own row, plus related rows on the one side through RELATED. It does not see the matching rows on the many side. Getting those rows takes a filter, and the chapter on context transition shows how a row context can be turned into one.
Reading the many side with RELATEDTABLE
RELATEDTABLE goes the other way. From a Product row, it returns the Sales rows that belong to that product. You can wrap it in an aggregation.
-- Calculated column in the Product table
Product Line Count = COUNTROWS ( RELATEDTABLE ( Sales ) )This works because RELATEDTABLE uses the current row to filter the table it returns. A later chapter explains what happens behind that behavior.
Which context am I in?
| Place | Row context? | Filter context? |
|---|---|---|
| Measure in a visual | No | Yes, from the visual, slicers, and filters |
| Calculated column | Yes, one row at a time | No, unless the formula adds one |
| Inside an iterator's expression | Yes, on each iterated row | Yes, still the outer one |
A quick check: if you can write a bare column reference such as Sales[Quantity] without an aggregation, you are inside a row context.
Exercise
A calculated column in the Product table is defined as Total = SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] ). Why does every product show the same value, and which function reads a column from a related row on the one side?
Show answer
The row context of each Product row does not filter Sales, so SUMX iterates every Sales row and returns the grand total each time. RELATED reads a column from the one side of a relationship, starting from a many-side row. For example, Sales can use RELATED ( Product[Category] ).