Sub topic 2
Row context: evaluating one row at a time
This chapter assumes you can move around Power BI Desktop (Report, Table and Model views), know what a star-schema relationship is, and can read basic DAX such as SUM, IF and column references. It explains row context, the first of the two evaluation contexts that decide what any DAX expression returns.
What row context is
A DAX expression that refers to a column, such as Sales[Quantity], needs to know which row to read. Row context supplies that answer. It means "the expression is being evaluated for one specific row of one specific table, and unqualified column references resolve to that row's values."
Row context does not filter anything. It does not hide other rows or change what SUM sees. It only points at a current row so that a bare column reference has a single value.
Two things create a row context:
- A calculated column, which is evaluated once for every row of its table.
- An iterator function such as
SUMX,AVERAGEX,MAXX,COUNTXorFILTER. These walk through a table row by row and evaluate an expression for each row.
A measure does not create a row context by itself. This is the cause of one of the most common DAX errors, covered below.
Row context in a calculated column
Consider this calculated column on the Sales table:
Line Amount = Sales[Quantity] * Sales[Unit Price]Power BI evaluates the expression once per row. For row 1 it reads that row's Quantity and Unit Price and multiplies them. For row 2 it does the same with row 2's values. The results are stored in the model.
| Row | Quantity | Unit Price | Line Amount |
|---|---|---|---|
| 1 | 2 | 10.00 | 20.00 |
| 2 | 5 | 4.00 | 20.00 |
| 3 | 1 | 99.00 | 99.00 |
You never had to write a loop. The row context is created automatically for every calculated column, which is why Sales[Quantity] works here without any aggregation.
Row context in an iterator
You can get the same per-row behavior inside a measure by using an iterator. SUMX takes a table and an expression. It creates a row context over that table, evaluates the expression for each row, and adds up the results.
Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price])Here the row context exists only while the measure runs. Nothing is written to the model. The expression Sales[Quantity] * Sales[Unit Price] is the same as in the calculated column, but it runs at query time and the per-row products are never stored.
The table you pass as the first argument decides which rows are visited. If a report visual filters Sales down to one year, SUMX iterates only the rows that survive that filter. That filter belongs to the second context, filter context, which the next chapter covers. For now, remember the split: filter context decides which rows are in play, and row context walks through them one by one.
Total Sales = SUM(Sales[Line Amount])SUM works differently. It does not need a row context because it is an aggregator. It takes a whole column and totals every visible value in it.
A bare column reference in a measure fails
Try to write this measure:
Bad Measure = Sales[Quantity] * Sales[Unit Price]DAX reports an error saying that a single value for the column cannot be determined. The reason is that a measure has no row context. When the measure is evaluated in a visual, many Sales rows may be visible at once, so Sales[Quantity] could mean any of them. DAX will not guess.
There are two correct fixes, depending on what you want:
- Aggregate the column:
SUM(Sales[Quantity]). - Create a row context with an iterator:
SUMX(Sales, Sales[Quantity] * Sales[Unit Price]).
Row context and relationships
Row context follows a relationship in one direction only. When you stand on a row of a "many" side table, DAX can find the single matching row on the "one" side. It cannot do the reverse automatically, because one row on the "one" side matches many rows on the "many" side.
RELATED uses this. On the Sales table, each row has one product, so this works:
Product Category = RELATED(Product[Category])For each Sales row, RELATED follows the many-to-one relationship to the matching Product row and returns its Category. The function requires a row context, so it runs in a calculated column or inside an iterator over Sales.
The same expression placed on the Product table would fail. A Product row matches many Sales rows, so there is no single Category to fetch from Sales. RELATED is for moving toward the "one" side. Moving the other way needs a different function, such as RELATEDTABLE, which returns a table, not a single value.
Nested row contexts
Row contexts can nest. If an iterator runs inside another iterator, or inside a calculated column, there are several current rows at once. The inner expression can refer to columns from both tables, and the innermost row context takes priority when the same table appears twice.
Sales Value With Category Total =
SUMX(
Product,
SUMX(
RELATEDTABLE(Sales),
Sales[Quantity] * Sales[Unit Price]
)
)The outer SUMX walks the Product table. For each product, RELATEDTABLE(Sales) returns that product's Sales rows, and the inner SUMX walks them. Inside the inner expression, both Sales[...] columns (the current Sales row) and Product[...] columns (the current product) are available.
Row context does not filter
It is easy to assume that being "on a row" restricts what other functions see. It does not. Consider a calculated column that tries to total all sales for the current product:
Product Sales Wrong = SUM(Sales[Line Amount])In a calculated column on Product, this returns the same grand total for every row. The row context points at one Product row, but SUM ignores row context and aggregates all visible Sales rows. No filter has been applied to Sales.
To make the row context act as a filter, you wrap the expression in CALCULATE:
Product Sales = CALCULATE(SUM(Sales[Line Amount]))This conversion is called context transition. It turns the current row into a filter. The chapter on context transition covers it in detail, so here it is enough to know that a row context alone never filters other tables.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| "A single value for column cannot be determined" in a measure | Bare column reference with no row context | Wrap in an aggregator or use an iterator |
| `RELATED` returns an error or blank on the "one" side table | Relationship direction runs the wrong way for a single value | Use `RELATEDTABLE` with an aggregator, or move the column to the "many" side |
| Every row of a calculated column shows the same total | `SUM` ignores row context | Use `CALCULATE` to apply context transition |
| Calculated column is slow to refresh | Iterating a large table inside a column for each row | Move logic to Power Query or the source, or use a measure |
Choosing between a column and an iterator
Both a calculated column and an iterator use row context to run the same per-row logic. The difference is when the work happens and where the result lives.
- A calculated column runs at refresh time and stores one value per row in the model.
- An iterator inside a measure runs at query time and stores nothing.
If you need the per-row value as something to slice by, place on an axis, or filter on, you need a column. If you only need the aggregated total, an iterator usually avoids the storage cost. The following chapters on evaluation timing and storage cost expand on this trade-off.
Exercise
A colleague writes the measure Avg Price = Sales[Unit Price] and gets an error. Explain why, and give two corrected versions that return different but valid results.
Show answer
A measure has no row context, and many Sales rows can be visible at once, so DAX cannot choose a single value for Sales[Unit Price]. One fix is to aggregate with AVERAGE(Sales[Unit Price]), which returns the simple average of the visible unit prices. Another is to create a row context with an iterator, for example AVERAGEX(Sales, Sales[Unit Price]), which gives the same result here, or a weighted version such as DIVIDE(SUMX(Sales, Sales[Quantity] * Sales[Unit Price]), SUM(Sales[Quantity])), which returns the average price per unit sold.
Exercise
You add the calculated column Customer Country = RELATED(Customer[Country]) to the Customer table, and it fails. Where should the column go, and why?
Show answer
RELATED follows a relationship from the "many" side to the "one" side. Customer is the "one" side, so a Customer row has many matching Sales rows and no relationship to follow toward a single value. Put the column on a "many" side table such as Sales, where each row has exactly one customer. If Country is already on the Customer table, you do not need RELATED there at all, because you can reference Customer[Country] directly.