Sub topic 7
Pulling values across relationships with RELATED
This chapter assumes you can move around Power BI Desktop's Report, Table and Model views. It also assumes you know a star-schema model (fact tables joined to dimension tables) and basic DAX such as SUM, IF and column references. You will learn what RELATED does, where it works, and what goes wrong when you use it in the wrong place.
The problem RELATED solves
In a star schema, descriptive attributes live in dimension tables. In this model, Product holds Category, Color and List Price. The fact table Sales holds one row per order line, with a ProductKey that points to Product.
A row in Sales has no Category column. If you want to see the category next to each sales row, or build a calculation that depends on it, you must fetch it from Product. A row context gives DAX one row of Sales at a time, and RELATED follows the relationship from that row to the matching row in the other table.
Product Category = RELATED(Product[Category])Add this as a calculated column on Sales. For each Sales row, DAX reads that row's ProductKey, finds the single matching Product row, and returns its Category.
How RELATED works
RELATED takes one argument: a column in another table. It returns a single value. It needs two things to be true:
- A row context exists for the current table.
- A relationship leads from the current table to the table that holds the column.
The relationship is the path. The row context picks the starting row. RELATED walks along the relationship from that row and reads the value at the other end.
The diagram below shows the lookup path.
Direction matters
RELATED works from the many side to the one side. Each Sales row matches at most one Product row, so the result is a single value and there is no ambiguity.
The reverse direction does not work with RELATED. A Product row can match many Sales rows, so there is no single value to return. In that direction, you use RELATEDTABLE, which returns a table of the matching rows. You then wrap it in an aggregator such as COUNTROWS or SUMX. Here is a calculated column on Product that counts its sales rows.
Sales Line Count = COUNTROWS(RELATEDTABLE(Sales))| Function | Called from | Returns | Typical use |
|---|---|---|---|
| `RELATED` | Many side (`Sales`) | One value from the one side | Copy a dimension attribute onto fact rows |
| `RELATEDTABLE` | One side (`Product`) | A table of matching many-side rows | Aggregate fact rows per dimension row |
Chains of relationships
RELATED can follow several relationships in a row, as long as each step goes from many to one. If Sales relates to Product, and Product relates to Subcategory, then a calculated column on Sales can call RELATED(Subcategory[Name]). In a star schema you usually avoid this chain by keeping dimensions flat, but a snowflake model makes it common.
Using RELATED inside other calculations
RELATED is not limited to simple lookups. You can use it inside any expression that runs in a row context. Here is a calculated column on Sales that applies a discount only to one category.
Discounted Amount =
IF (
RELATED ( Product[Category] ) = "Accessories",
Sales[Quantity] * Sales[Unit Price] * 0.9,
Sales[Quantity] * Sales[Unit Price]
)RELATED also works inside iterator functions such as SUMX. An iterator creates a row context over the table you give it, so RELATED is allowed in its expression. This measure sums quantity times list price, where the list price comes from Product.
Sales at List Price =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[List Price] )
)This gives you the benefit of RELATED without adding a column to the model. The lookup happens at query time, once per row visible in the current filter context.
Where RELATED fails
Most RELATED errors come from a handful of causes.
- No row context. In a plain measure, there is no current row. This measure fails:
``dax Bad Category = RELATED ( Product[Category] ) ``
Power BI Desktop reports that a single value cannot be determined or that no row context exists. Wrap the call in an iterator, or use a function that works over the filter context, such as SELECTEDVALUE.
- No relationship. If no active relationship connects the two tables,
RELATEDreturns an error. An inactive relationship does not count. You can activate it, or useUSERELATIONSHIPinsideCALCULATE, or useLOOKUPVALUE.
- Wrong direction. Calling
RELATEDfrom the one side to the many side fails, as described above. UseRELATEDTABLE.
- Unmatched keys. If a
Salesrow has aProductKeythat is missing fromProduct,RELATEDreturns a blank, not an error. The unmatched rows then appear under a blank category in visuals. Check for orphan keys when blank categories show up.
- Many-to-many relationships. With a many-to-many relationship, neither side is guaranteed to be unique, so
RELATEDcannot be relied on. Prefer a bridge table or a different approach.
RELATED in a calculated column or in a measure?
The same function appears in two places, and the trade-off is the one this tutorial is about.
- In a calculated column,
RELATEDruns once per row at refresh time. The result is stored in the model and uses memory. You get a real column that you can use on a slicer, an axis or a filter. - In an iterator inside a measure,
RELATEDruns at query time and stores nothing. You cannot put the result on a slicer, because there is no column.
Consider Product Category on Sales. It copies a value that already exists in Product[Category]. In most reports it is better to put Product[Category] itself on the slicer or axis. The relationship already filters Sales by category, so the copy adds memory cost and no capability.
A copied column earns its place in a few cases:
- You need the category as part of a further row-level calculation, such as the discount example above.
- You need to use the value in a relationship key or in a sort-by-column setup on the fact table.
- You need to compare it row by row with another column on the same table.
Outside those cases, prefer a measure or a column on the dimension table.
A worked example
Suppose Sales has Quantity and Unit Price, and Product has List Price. You want to know how much revenue was given up through discounting, that is, list value minus actual value.
First, a row-level column on Sales:
List Amount = Sales[Quantity] * RELATED ( Product[List Price] )Then a measure over it:
Discount Given = SUM ( Sales[List Amount] ) - SUM ( Sales[Line Amount] )This works because both parts are additive. Summing list amounts and summing line amounts across any set of rows gives the right totals, so their difference is right at every level.
The same result without the extra stored column is a single measure:
Discount Given (no column) =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[List Price] )
)
- SUM ( Sales[Line Amount] )Both give the same numbers. The first costs memory at refresh. The second costs a little more work at query time. On a table with millions of rows, test both before choosing.
Exercise
A calculated column on Product uses =RELATED(Sales[Amount]) and returns an error. Why does it fail, and what would you use to total the amount per product row instead?
Show answer
RELATED only goes from the many side to the one side. Product is the one side and Sales is the many side, so a single Product row matches many Sales rows and no single value exists. Use RELATEDTABLE, wrapped in an aggregator: Product Sales = SUMX(RELATEDTABLE(Sales), Sales[Amount]). For reports, a measure such as SUM(Sales[Amount]) is usually better, because it responds to filters.
Exercise
A new calculated column Sales[Region] = RELATED(Geography[Region]) shows blank for some rows. Name two likely causes.
Show answer
The Sales row may have a key that does not exist in Geography (an orphan key), so there is no matching row. Or the relationship between the tables may be inactive or missing, so the lookup has no path. Check the key values and the relationship in Model view.