Sub topic 2
Row context: evaluating one row at a time
This chapter assumes you can move between Report, Table and Model views in Power BI Desktop, and that you know basic DAX syntax such as SUM, IF and column references. You should also know what a many-to-one relationship is in a star schema.
What row context is
Row context means "the current row". When DAX evaluates an expression in row context, a bare column reference such as Sales[Quantity] returns the value of that column in one specific row. DAX then moves to the next row and evaluates the expression again.
Without a row context, a bare column reference has no single value to return. This is why Sales[Quantity] * Sales[Unit Price] works inside a calculated column but fails as a measure.
Where row context comes from
Two things create a row context:
- A calculated column. DAX evaluates the expression once for every row of the table that holds the column.
- An iterator function. Functions that end in
X, such asSUMX,AVERAGEXandFILTER, step through a table row by row and evaluate an expression for each row.
A measure has no row context by default. It runs in a filter context, which the next chapter covers.
Calculated column example
Add this calculated column to the Sales table:
Line Amount = Sales[Quantity] * Sales[Unit Price]For each row of Sales, DAX reads that row's Quantity and Unit Price, multiplies them, and stores the result. If row 1 has Quantity 3 and Unit Price 20, Line Amount for row 1 is 60. Row 2 is calculated separately.
The numbers are computed when the model refreshes, then stored in the model like any imported column.
Iterator example
An iterator gives a measure a row context for the length of one function call:
Total Sales (no column) = SUMX(Sales, Sales[Quantity] * Sales[Unit Price])SUMX takes two arguments. The first is the table to walk through. The second is the expression to evaluate in each row. SUMX then adds up the results. The multiplication happens inside the row context that SUMX creates, and no column is stored.
The following diagram shows the loop.
Row context and relationships
Row context covers only the current row of one table. It does not automatically follow relationships. To read a value from a related table, use RELATED, which follows the many-to-one relationship from the current Sales row to its matching Product row:
Product Category = RELATED(Product[Category])This works in the Sales table because each Sales row points to exactly one Product row. The reverse direction needs a different function, and a later chapter covers it.
Common errors
- A bare column in a measure. Writing
Line Amount Measure = Sales[Quantity] * Sales[Unit Price]as a measure gives an error. A measure has no current row, so DAX cannot pick one value from the column. Wrap the expression inSUMX, or aggregate the column withSUM. - Expecting row context to filter the report. A row context does not filter other tables or change what a visual shows. Filtering is the job of filter context.
- Using
RELATEDon the wrong side.RELATEDworks from the many side to the one side. Calling it from the Product table to read Sales values does not work.
Exercise
A colleague writes the measure Discounted = Sales[Quantity] * Sales[Unit Price] * 0.9 and gets an error. Explain why, and rewrite it so it returns the total discounted sales amount.
Show answer
A measure has no row context, so Sales[Quantity] and Sales[Unit Price] have no single value to return. Create a row context with an iterator: Discounted = SUMX(Sales, Sales[Quantity] * Sales[Unit Price] * 0.9). SUMX evaluates the expression for each Sales row and adds up the results.