Sub topic 7
Pulling values across relationships with RELATED
This chapter assumes you can navigate Power BI Desktop's Report, Table and Model views. It also assumes you know what a star-schema model is and can read basic DAX such as SUM and IF. You should already know that a calculated column is evaluated one row at a time, in row context.
The problem RELATED solves
In a star schema, descriptive attributes sit in dimension tables. The Sales table holds quantities and prices. The Product table holds Category, Color and List Price. Each sale row stores a product key, not the category name.
Row context gives a calculated column access to the current row of its own table. It does not automatically give access to columns in other tables. Writing Product[Category] inside a calculated column on Sales fails, because DAX cannot choose a single Product row from a whole column.
RELATED follows a relationship from the current row to the matching row in another table and returns one value from it.
Product Category = RELATED(Product[Category])Add this as a calculated column on the Sales table. For each Sales row, DAX takes the product key, finds the one matching Product row, and returns its Category.
How it works
RELATED needs two things to work:
- A row context on the table where you write the formula. A calculated column provides one. A measure does not, unless an iterator such as SUMX supplies it.
- A relationship that leads from the current table to the table you read from, on the "one" side. The direction matters. From Sales (many) to Product (one), each Sales row matches at most one Product row, so RELATED can return a single value.
Using RELATED inside an iterator
A measure has no row context of its own. You can create one with SUMX and use RELATED inside it.
Red Quantity =
SUMX(
Sales,
IF(RELATED(Product[Color]) = "Red", Sales[Quantity], 0)
)SUMX walks through Sales one row at a time. For each row, RELATED looks up the product color. No column is stored, and the work happens at query time. The simpler CALCULATE(SUM(Sales[Quantity]), Product[Color] = "Red") gives the same answer and is usually preferred.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Error that a column "cannot be determined" or a single value is expected | You referenced `Product[Category]` directly from Sales without RELATED | Wrap the reference in RELATED |
| RELATED fails because no relationship exists | No active relationship connects the two tables | Create or activate the relationship in Model view |
| You need a value from the "many" side | RELATED only goes from many to one | Use RELATEDTABLE to get the related rows, then aggregate them, for example with COUNTROWS |
| Wrong result with an inactive relationship | RELATED uses only the active relationship | Use USERELATIONSHIP in a measure, or fix the model |
Exercise
You add Category = Product[Category] as a calculated column on Sales and get an error. What is wrong, and how do you fix it?
Show answer
Row context on Sales gives the current Sales row, but not a single row of Product. Wrap the reference with RELATED, so the formula becomes Category = RELATED(Product[Category]). This requires an active many-to-one relationship from Sales to Product.
Exercise
You are on the Product table and want a column that counts how many Sales rows each product has. Is RELATED the right function?
Show answer
No. RELATED goes from the many side to the one side. From Product, use RELATEDTABLE(Sales) inside COUNTROWS: Sales Rows = COUNTROWS(RELATEDTABLE(Sales)).