Sub topic 5
Pulling Values from Related Tables with RELATED
This chapter assumes you know star-schema modelling, can write simple measures with SUM and CALCULATE, and understand that SUMX creates a row context on each row it visits. If row context is new to you, review the earlier chapters first.
The problem: a column on the other side of a relationship
The Sales fact table holds Quantity and Net Price. The Product dimension table holds Unit Cost. You want total cost, which is quantity times unit cost for every sale.
Inside an iterator over Sales, you can read any column of Sales directly, because the iterator creates a row context on that table. You can't read Product[Unit Cost] the same way. The row context covers only the Sales row, and a bare column reference to Product has no current row to point at.
RELATED solves this. It follows the relationship from the current row to the related row and returns the value of the column you name.
RELATED in a SUMX
Total Cost =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[Unit Cost] )
)For each Sales row, RELATED uses the relationship to find the matching Product row. It returns that product's Unit Cost. SUMX multiplies it by the row's Quantity and adds the results.
You can combine this with the first measure to get margin:
Margin =
SUMX (
Sales,
Sales[Quantity] * ( Sales[Net Price] - RELATED ( Product[Unit Cost] ) )
)How RELATED travels
The direction matters. Each Sales row matches at most one Product row, so RELATED can return a single value. This is the many-to-one direction, from the fact table to the dimension.
Rules to remember
RELATEDneeds a row context. Using it in a measure outside an iterator causes an error.- The current table must be on the many side of a relationship that leads to the table you name.
- It can follow a chain of relationships. For example, from
SalestoProducttoProduct Subcategory, if each link is active and many-to-one. - It returns one value. If the relationship is missing or inactive, the formula fails or returns the wrong result.
- If the Sales row has no matching Product row,
RELATEDreturns blank.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that a column can't be determined | `RELATED` used with no row context, for example directly in a measure | Wrap it in an iterator such as `SUMX` |
| Error about a missing relationship | No active relationship between the tables | Create or activate the relationship |
| Error when iterating `Product` and reading a `Sales` column | You are on the one side and need many values | Use `RELATEDTABLE`, or aggregate with a measure |
| Unexpected zero or blank cost | Sales rows with a product key that doesn't exist in `Product` | Check data quality for orphan keys |
RELATED or a calculated column?
You could add a calculated column Sales[Line Cost] that uses RELATED, then use SUM. That stores the value for every row and uses more memory. The SUMX version computes the value at query time and needs no extra column. Prefer the measure unless you reuse the per-row value in many places.
Exercise
Write a measure called Revenue Over Cost that returns the sum, over all Sales rows, of Sales[Quantity] * (Sales[Net Price] - Product[Unit Cost]). Which function brings in Unit Cost, and why can't you write Product[Unit Cost] on its own?
Show answer
Revenue Over Cost = SUMX ( Sales, Sales[Quantity] * ( Sales[Net Price] - RELATED ( Product[Unit Cost] ) ) ) RELATED brings in Unit Cost. SUMX creates a row context only on Sales, so a bare Product[Unit Cost] has no current Product row. RELATED follows the many-to-one relationship from the current Sales row to its single Product row and returns that product's Unit Cost.