Sub topic 5
Reading related rows with RELATED and RELATEDTABLE
This chapter assumes you know one-to-many relationships, the difference between a calculated column and a measure, and what a row context is. A row context is the "current row" that a calculated column or an iterator such as SUMX sets up. The examples use a Sales table (the many side) related to a Product table (the one side).
The problem a row context leaves open
A row context gives you the columns of one row. It does not reach into other tables. In a Sales row you can read Sales[Quantity] directly, but Product[Category] lives in another table. DAX needs a way to follow the relationship from the current row.
RELATED and RELATEDTABLE do that. Both need a row context, and both follow an existing relationship.
| Function | Direction | Returns |
|---|---|---|
| `RELATED ( Table[Column] )` | Many side to one side | One value |
| `RELATEDTABLE ( Table )` | One side to many side | A table of matching rows |
RELATED: one value from the one side
In a calculated column on Sales, the row context points at one sales row. That row holds a product key, so exactly one product matches it.
-- Calculated column in the Sales table
Line Amount = Sales[Quantity] * Sales[Unit Price]
-- Calculated column in the Sales table, using the relationship
Product Category = RELATED ( Product[Category] )The first column reads only the current row. The second follows the relationship and returns the category of that row's product.
RELATED also works in any iterator over Sales, because the iterator creates a row context. This measure sums only the red products' sales without adding a column to the model:
Red Line Amount =
SUMX (
FILTER ( Sales, RELATED ( Product[Color] ) = "Red" ),
Sales[Quantity] * Sales[Unit Price]
)FILTER opens a row context on each Sales row, so RELATED has a current row to start from.
RELATEDTABLE: many rows from the many side
Start from a Product row and go the other way. Many Sales rows can match one product, so the result is a table, not a value. Wrap it in a function that accepts a table.
-- Calculated column in the Product table
Sales Row Count = COUNTROWS ( RELATEDTABLE ( Sales ) )For each product row, RELATEDTABLE ( Sales ) returns the sales rows that belong to that product. COUNTROWS counts them. Any aggregation over a table works here, such as SUMX ( RELATEDTABLE ( Sales ), Sales[Quantity] ).
-- Calculated column in the Product table
Units Sold = SUMX ( RELATEDTABLE ( Sales ), Sales[Quantity] )Common errors
- No row context. Using
RELATEDin a measure outside an iterator fails, because there is no current row to follow. Wrap the expression in an iterator over the many-side table. - Wrong direction.
RELATEDon a one-to-many path from the one side fails. UseRELATEDTABLEfrom the one side, andRELATEDfrom the many side. - Passing a column, not a table.
RELATEDTABLE ( Sales[Quantity] )is invalid. It takes a table name. Pick the column in the aggregator. - Missing relationship. Both functions need an active relationship between the tables. Without one, the formula returns an error. A table with no related row on the one side returns blank for
RELATED. - Chains.
RELATEDcan follow a chain of many-to-one relationships, for example Sales to Product to Subcategory. It cannot cross a one-to-many step in the middle of the chain.
When to prefer a column or a measure
A calculated column such as Product Category stores its values in the model and uses memory. The Red Line Amount measure computes at query time and uses no storage. Use a column when you need to slice or filter by the value. Use a measure when you only need a number in a visual.
Exercise
You are writing a calculated column on the Product table that shows how many Sales rows each product has. Which function do you use to reach the Sales rows, and why can you not use RELATED?
Show answer
Use COUNTROWS ( RELATEDTABLE ( Sales ) ). From the one side (Product), the relationship leads to many Sales rows, so the result is a table. RELATED returns a single value and only works from the many side toward the one side.
Exercise
A measure contains RELATED ( Product[Color] ) written directly in its body, outside any iterator. Why does it fail, and how do you fix it?
Show answer
A measure runs in a filter context and has no row context, so RELATED has no current row to follow. Put the expression inside an iterator over Sales, such as SUMX ( FILTER ( Sales, RELATED ( Product[Color] ) = "Red" ), Sales[Quantity] * Sales[Unit Price] ).