Sub topic 5
Reading related rows with RELATED and RELATEDTABLE
A row context points at one row of one table. Often the value you need is in another table: the product category sits in Product, but you are working on a Sales row. RELATED and RELATEDTABLE let a row context cross a relationship. This chapter explains how each function works, what it returns, and where it fails.
This chapter assumes you know the following:
- One-to-many relationships, and which side holds the key.
- The difference between a measure and a calculated column.
- What a row context is, from the previous chapter.
The examples use a small star schema. Sales is on the many side. It has Quantity, Unit Price, and a product key. Product is on the one side. It has Category, Color, and Unit Cost. Customer and Geography follow the same pattern: each customer belongs to one geography, and Customer is on the many side of that relationship.
The problem: a row context sees only its own table
Inside a calculated column on Sales, the row context gives you every column of the current Sales row. It does not give you columns of Product. This formula fails:
-- Calculated column in the Sales table
Product Category = Product[Category]DAX cannot tell which Product row you mean. It would need a row context on Product, and none exists. The relationship supplies the missing link. The current Sales row holds a product key, and that key identifies exactly one Product row. RELATED follows the key.
RELATED: from the many side to the one side
RELATED takes one column and returns that column's value from the single related row. It needs a row context on a table that sits on the many side of a relationship.
-- 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] )For each Sales row, RELATED reads the product key, finds the matching Product row, and returns its Category. The key is unique on the one side, so the result is always a single value.
RELATED inside an iterator
RELATED also works in measures, as long as a row context exists. An iterator creates that row context. Here SUMX creates a row context on Sales, so RELATED can read Product for each row.
Gross Margin =
SUMX (
Sales,
Sales[Quantity] * ( Sales[Unit Price] - RELATED ( Product[Unit Cost] ) )
)The filter context still decides which Sales rows the iterator visits. A slicer on Product[Color] filters Sales through the relationship, so SUMX visits only matching rows. Then RELATED runs once per visited row.
A measure with no iterator has no row context, and RELATED cannot work there:
-- Fails: a measure has no row context
Category Lookup = RELATED ( Product[Category] )What RELATED does not do
- It does not use the filter context. It follows the key of the current row. A slicer on
Product[Category]has no direct effect on the valueRELATEDreturns. The slicer only changes which rows the iterator visits. - It does not depend on cross-filter direction. It always follows the relationship from the many side to the one side. Single or both-direction filtering makes no difference.
- It follows the active relationship. If two relationships connect the same tables,
RELATEDuses the active one. To use an inactive one, wrap the call inCALCULATEwithUSERELATIONSHIP. - It returns a value, not a table. To read several columns, call
RELATEDonce per column.
Chains of relationships
RELATED can cross several many-to-one relationships. If Customer points to Geography, you can read Geography from Sales as long as the chain goes from many to one at each step:
-- Calculated column in the Customer table
Customer Country = RELATED ( Geography[Country] )If Sales also relates to Customer, then RELATED ( Customer[Customer Country] ) or RELATED ( Geography[Country] ) works from a Sales row. DAX follows the chain. Avoid hops that pass through a many-to-many step, because the result would no longer be one row.
Missing matches return BLANK
If a Sales row has a product key that does not exist in Product, RELATED returns BLANK (). This is a referential integrity violation. Power BI keeps such rows by adding a blank row to the one-side table. In a visual, these sales appear under a blank category. If your report shows a (Blank) group you did not expect, check the keys in the many-side table first.
RELATEDTABLE: from the one side to the many side
RELATEDTABLE runs in the opposite direction. It takes a table name and returns the rows of that table that relate to the current row. Because one row can have many related rows, the result is a table, not a value.
-- Calculated column in the Product table
Order Lines = COUNTROWS ( RELATEDTABLE ( Sales ) )
-- Calculated column in the Product table
Product Revenue =
SUMX ( RELATEDTABLE ( Sales ), Sales[Quantity] * Sales[Unit Price] )For each Product row, RELATEDTABLE ( Sales ) returns the Sales rows that share that product's key. COUNTROWS counts them. SUMX opens a new row context on that small table and adds up the product of quantity and price.
This is the explicit counterpart of a pattern from the spine. Compare it with a column that iterates Sales directly:
-- Calculated column in the Product table
Sales Without Transition =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )The Product row context does not filter Sales, so every product shows the grand total. RELATEDTABLE ( Sales ) fixes that by naming the relationship explicitly. A measure reference such as [Sales Amount] also fixes it, but through a different mechanism, context transition, covered in a later chapter.
RELATEDTABLE in measures
RELATEDTABLE is not limited to calculated columns. Any iterator over Product provides the row context it needs:
Products With Sales =
COUNTROWS (
FILTER ( Product, COUNTROWS ( RELATEDTABLE ( Sales ) ) > 0 )
)FILTER visits each product in the current filter context. For each one, RELATEDTABLE ( Sales ) returns that product's sales rows. The measure counts the products where that count is above zero.
RELATEDTABLE performs context transition
RELATEDTABLE ( Sales ) behaves like CALCULATETABLE ( Sales ). Internally, the current row of Product becomes a filter on Product, and that filter reaches Sales through the relationship. That is why it returns only that product's rows.
This has two consequences:
- It is a filter, so it combines with the existing filter context. If a slicer already restricts
Salesto 2025,RELATEDTABLE ( Sales )inside a measure returns only that product's 2025 rows. In a calculated column, the filter context is empty, so you get all of that product's rows. - The same duplicate-row concern applies as for any context transition. The row-to-filter conversion uses every column of the table, so a table whose rows are not unique can return more rows than you expect. The pitfalls chapter returns to this.
Choosing between RELATED and RELATEDTABLE
| Row | RELATED | RELATEDTABLE |
|---|---|---|
| Starting row context | Many side (for example `Sales`) | One side (for example `Product`) |
| Returns | One scalar value | A table of rows |
| Argument | A column on the one side | A table on the many side |
| Typical wrapper | None, or inside an iterator | An aggregator or iterator such as `COUNTROWS` or `SUMX` |
| No match | `BLANK ()` | Empty table |
| Uses context transition | No | Yes |
A quick rule: if you can name the answer as a single cell, use RELATED. If the answer is "all the matching rows", use RELATEDTABLE and aggregate.
Common errors
- Using RELATED with no row context. A bare
RELATEDin a measure fails. Wrap the logic in an iterator over the many-side table. - Using RELATED in the wrong direction.
RELATED ( Sales[Quantity] )from aProductrow asks for many values and fails. UseRELATEDTABLE ( Sales )with an aggregator. - Passing an unaggregated table to a scalar context.
RELATEDTABLE ( Sales )returns a table. A calculated column needs a value, so wrap it inCOUNTROWS,SUMX, or similar. - Expecting a relationship that does not exist. If there is no relationship path between the two tables, neither function works. Check the model view.
- Expecting RELATED across a many-to-many relationship.
RELATEDneeds the target side to be unique. For many-to-many links, aggregate over the related rows instead, or useLOOKUPVALUEwhen you know the match is unique.
RELATED in a calculated column or a measure?
Computing Product Category as a calculated column on Sales stores a value for every row. It uses memory, and it is recomputed at data refresh. Doing the same lookup at query time costs CPU instead. For descriptive attributes such as category, the better design is usually to use Product[Category] directly in the visual. The filter then flows through the relationship and no copy is needed. Use RELATED in a calculated column when you need the value to build another column. Use it in an iterator when the formula combines columns from both tables row by row, as in Gross Margin.
Exercise
A calculated column on the Product table is defined as Total Qty = SUMX ( Sales, Sales[Quantity] ). Every product shows the same number. Rewrite the formula so each product shows only its own quantity, without using a measure reference.
Show answer
Replace the table argument with RELATEDTABLE: Total Qty = SUMX ( RELATEDTABLE ( Sales ), Sales[Quantity] ). The Product row context does not filter Sales, so SUMX over Sales sums all rows. RELATEDTABLE ( Sales ) returns only the Sales rows linked to the current Product row.
Exercise
You write the measure Margin Per Line = Sales[Quantity] * ( Sales[Unit Price] - RELATED ( Product[Unit Cost] ) ) and get an error. Why, and how do you fix it so the measure returns total margin?
Show answer
A measure has no row context, so neither the Sales columns nor RELATED can be evaluated. Wrap the expression in an iterator over Sales: SUMX ( Sales, Sales[Quantity] * ( Sales[Unit Price] - RELATED ( Product[Unit Cost] ) ) ). SUMX creates the row context on Sales that RELATED needs.
Exercise
A report shows a (Blank) category with sales under it. The Product table has no blank categories. What is the most likely cause, and what does RELATED return for those rows?
Show answer
Some Sales rows have a product key that does not exist in Product, which violates referential integrity. RELATED returns BLANK for those rows, and the visual groups them under (Blank). Fix the keys in Sales or add the missing products to Product.