Sub topic 5
Pulling Values from Related Tables with RELATED
This chapter assumes you can write basic DAX measures with SUM and CALCULATE, and that you know the star-schema vocabulary: fact tables, dimension tables, and one-to-many relationships. It also assumes you have read the earlier chapters on row context, filter context, and how SUMX works.
The problem: the column you need is in another table
In a star schema, the fact table holds events, such as one row per sales line. Descriptive attributes sit in dimension tables. A Sales table might store Quantity and Net Price, while Product stores Unit Cost.
Suppose you want total cost. Each sales row needs its quantity multiplied by the unit cost of the product on that row. Unit Cost is not a column of Sales, so Sales[Quantity] * Product[Unit Cost] is not valid inside an iterator over Sales.
RELATED solves this. It reads a column from another table by following a relationship from the current row.
How RELATED works
RELATED ( Table[Column] ) takes the row you are currently on, follows the relationship to the table that owns the column, and returns the value from the single matching row.
Two conditions must hold:
- A row context must exist on the table you start from. Iterators such as SUMX create one for each row they visit. A plain measure has no row context, so
RELATEDfails there. - The relationship must lead from the many side to the one side. From a
Salesrow, there is exactly oneProductrow, so the result is a single value.
Here is the standard example.
Total Cost =
SUMX (
Sales,
Sales[Quantity] * RELATED ( Product[Unit Cost] )
)SUMX visits each row of Sales. For each row, DAX takes Sales[Quantity] from the current row. It then follows the relationship from Sales[ProductKey] to Product[ProductKey] and reads Unit Cost from the matching product. It multiplies the two values and moves to the next row. At the end, SUMX adds up all the products.
The diagram shows one iteration.
A worked example
Take a small Sales table and a Product table.
| Sales row | ProductKey | Quantity |
|---|---|---|
| 1 | 101 | 2 |
| 2 | 102 | 5 |
| 3 | 101 | 1 |
| ProductKey | Product Name | Unit Cost |
|---|---|---|
| 101 | Desk Lamp | 10 |
| 102 | Notebook | 3 |
Total Cost evaluates like this:
- Row 1: 2 × 10 = 20
- Row 2: 5 × 3 = 15
- Row 3: 1 × 10 = 10
SUMX adds these to get 45. Notice that product 101 is looked up twice. RELATED is evaluated per sales row, not per product.
Why not store the cost in Sales?
You could add a calculated column to Sales, such as Sales[Line Cost] = Sales[Quantity] * RELATED ( Product[Unit Cost] ), and then use SUM ( Sales[Line Cost] ). That works, and the column even uses the same RELATED syntax, because a calculated column also has a row context.
The trade-offs:
| Approach | Memory | Refresh cost | Query speed | Flexibility |
|---|---|---|---|---|
| SUMX with RELATED in a measure | No extra column | None | Calculated at query time | Logic changes without a refresh |
| Calculated column plus SUM | Stores one value per fact row | Recalculated at each refresh | Often faster, since it only scans a column | Fixed at refresh time |
For a large fact table, a calculated column can increase model size noticeably. Many modelers start with the measure and move to a column only when query speed demands it. If the model is built from a database or Power Query, computing the column upstream is another option.
RELATED in the other direction: RELATEDTABLE
RELATED only works from the many side toward the one side. The reverse, from one row of Product to the many Sales rows, returns a set of rows rather than a single value. For that, DAX has RELATEDTABLE, which returns the related rows as a table.
Product Sales Rows =
COUNTROWS ( RELATEDTABLE ( Sales ) )As a calculated column on Product, this counts the sales rows for each product. RELATEDTABLE is the table-returning counterpart of RELATED. In measures you more often use CALCULATE for this job, because it works without an explicit row context. The details of RELATEDTABLE are outside this chapter, but remember the rule: one value comes from RELATED, and many rows come from RELATEDTABLE.
Iterating over a dimension instead
You do not have to iterate over the fact table. If you iterate over the dimension, the columns of that table are available directly, and you reach the fact data through context transition (covered in a later chapter). For example, this measure gives the same total cost for sales within the current filter context only when the cost is evaluated against each product's sales:
Total Cost by Product =
SUMX (
VALUES ( Product[ProductKey] ),
CALCULATE ( SUM ( Sales[Quantity] ) ) * RELATED ( Product[Unit Cost] )
)Here VALUES ( Product[ProductKey] ) is a one-column table of product keys, so the row context is on a column of Product. RELATED is not strictly required to read another column of the same table, but it also works when the row context comes from VALUES of a column in a table that owns Unit Cost... and this is a point where many people get confused. Row context from VALUES ( Product[ProductKey] ) does not carry the other columns of Product as a row, so reading Product[Unit Cost] directly can fail or give an unintended result.
That makes this pattern fragile. For a weighted sum like cost, iterating over Sales with RELATED is simpler and safer. Use the dimension-iterating form when you genuinely want one calculation per member, as in an average per customer.
Common errors
Using RELATED where there is no row context
Bad Cost =
RELATED ( Product[Unit Cost] ) * SUM ( Sales[Quantity] )A measure on its own has no row context. DAX reports that RELATED needs a row context. Wrap the logic in an iterator, as in the Total Cost measure.
Going the wrong way along the relationship
If you iterate over Product and write RELATED ( Sales[Net Price] ), DAX reports an error or cannot resolve a single value. The relationship goes from many to one, and Product is on the one side. Either iterate over Sales, or use an aggregation inside CALCULATE.
Missing or inactive relationships
RELATED follows the active relationship. If the relationship between the two tables is inactive, RELATED cannot use it. For example, a Sales table might have both OrderDate and ShipDate related to Date, with only one active. RELATED ( 'Date'[Month] ) uses the active one. To use the inactive one, use USERELATIONSHIP inside CALCULATE, or choose a different design. RELATED itself does not take a relationship argument.
Blank results from unmatched keys
If a Sales row has a ProductKey with no match in Product, RELATED returns blank. Multiplying by blank gives zero in DAX arithmetic for *, so those rows contribute zero cost without any error. This can hide data problems. In many models, DAX adds a blank row to the one side to absorb unmatched keys, and that row has a blank Unit Cost. Check for orphan keys when totals look low.
Chains of relationships
RELATED can travel through a chain of many-to-one relationships. If Sales relates to Product, and Product relates to Subcategory, then RELATED ( Subcategory[Name] ) from a Sales row works, provided each step goes from many to one and the relationships are active. A snowflake design makes this possible, but each hop costs a little time.
Filtering rows with a related column
A common use of RELATED is inside FILTER, which is also an iterator. It lets you keep only rows where a dimension column meets a condition.
Cost of Expensive Items =
SUMX (
FILTER ( Sales, RELATED ( Product[Unit Cost] ) > 100 ),
Sales[Quantity] * RELATED ( Product[Unit Cost] )
)FILTER walks Sales and keeps rows whose product costs more than 100. SUMX then iterates the remaining rows. RELATED appears twice because each of the two iterators has its own row context.
When you only need a filter on a dimension column, a simpler form is often better:
Cost of Expensive Items v2 =
CALCULATE (
[Total Cost],
Product[Unit Cost] > 100
)This version applies the condition as a filter on Product, which the engine handles efficiently. Prefer it when it expresses the same logic.
Performance notes
RELATED is cheap per call, since it is a lookup through a relationship. The cost scales with the number of rows iterated. On a fact table with tens of millions of rows, a SUMX with several RELATED calls can be slower than a column aggregation. The storage engine can often push simple expressions like Quantity * RELATED ( Unit Cost ) down efficiently, but complex logic inside the iterator may fall back to slower row-by-row evaluation. Later chapters return to this.
Summary
RELATEDreads one column value from the one side of a relationship while a row context exists on the many side.- Iterators such as SUMX and FILTER supply that row context.
- It cannot run in a plain measure, cannot go from one to many, and uses only the active relationship.
- An unmatched key gives a blank, which can hide data problems.
- For simple conditions on a dimension, a
CALCULATEfilter is often clearer and faster thanFILTERwithRELATED.
Exercise
A Sales table has Quantity and ProductKey. A Product table has ProductKey and Unit Cost. Write a measure that returns the total cost of all sales in the current filter context. Then explain why SUM ( Sales[Quantity] ) * RELATED ( Product[Unit Cost] ) fails.
Show answer
Total Cost = SUMX ( Sales, Sales[Quantity] * RELATED ( Product[Unit Cost] ) ). The alternative fails because a measure has no row context, so RELATED has no current row from which to follow the relationship. Even with a row context, multiplying the total quantity by one unit cost would give the wrong answer, since each row has its own product.
Exercise
You iterate over Product with SUMX and write RELATED ( Sales[Net Price] ) inside it. Why does this not work, and how would you restructure it?
Show answer
RELATED goes from the many side to the one side. Product is the one side, and each product has many sales rows, so there is no single value to return. Iterate over Sales instead, so each row has one product, or use CALCULATE ( SUM ( Sales[Net Price] ) ) inside an iterator over Product, which uses context transition.
Exercise
Some Sales rows contain a ProductKey that does not exist in Product. What does RELATED return for them, and how does this affect Total Cost?
Show answer
RELATED returns blank. In the multiplication, blank is treated as zero, so those rows add nothing to the total and no error appears. The total can be understated, so check for orphan keys when numbers look low.