Stop 09 · Advanced DAX PatternsTopics · 1/10

RELATED and RELATEDTABLE

Part of the Power BI Roadmap

In short

Your Sales table knows the ProductID, but the unit cost sits in the Product table. RELATED and RELATEDTABLE let a row reach across a relationship and grab what it needs, without merging tables in Power Query.

RELATED fetches a value from the 'one' side of a relationship into a row on the 'many' side, and RELATEDTABLE returns the matching rows from the 'many' side.

The jaadu holds a single parcel in one hand and follows a string from it to a fat address book, while a pile of parcels hangs from another string on the book's open page.

Think of it like this

The parcel and the address book

A courier slip in Jaipur only says "Customer 1042". The delivery boy flips open the address book to find the full address. That's RELATED: one row looking up one matching row. Now flip it around: the address book page for Customer 1042 asks "show me every parcel ever sent to me". That's RELATEDTABLE: one row pulling back many.

Watch it happen

A Sales row asks Product for its cost

Sales row
Relationship
Product row

RELATED ( Product[Unit Cost] )

1/5A Sales row for ProductID P07 needs the unit cost. It sits on the many side, so it can only follow the relationship to one product.

The big ideas

What you need to know

RELATED goes many to one

Use it on the many side (Sales) to fetch a single value from the one side (Product, Customer, Date).

RELATEDTABLE goes one to many

Use it on the one side to get every matching row from the many side. It returns a table, never a single value.

They need a row context

They work in calculated columns and inside iterators like SUMX or FILTER. In a plain measure there is no current row to start from.

No relationship, no result

Both follow an existing active relationship. If the tables aren't related, RELATED throws an error and RELATEDTABLE returns nothing useful.

See it in code

Walk through the code

dax
1-- Calculated column in Sales (many side)2Line Cost = Sales[Qty] * RELATED ( Product[Unit Cost] )3 4-- Calculated column in Product (one side)5Units Sold = SUMX ( RELATEDTABLE ( Sales ), Sales[Qty] )6 7-- Calculated column in Customer8Order Lines = COUNTROWS ( RELATEDTABLE ( Sales ) )9 10-- Measure: RELATED inside an iterator, no extra column needed11Total Margin =12SUMX (13    Sales,14    Sales[Amount] - Sales[Qty] * RELATED ( Product[Unit Cost] )15)

1/4Each Sales row looks up its product's unit cost and multiplies by quantity.

Both directions of a Sales to Product relationship, as columns and as a measure.

Save yourself the pain

Common mistakes

Oops

Writing a measure like Total Cost = SUM ( Sales[Qty] ) * RELATED ( Product[Unit Cost] ).

Do this instead

A measure has no current row. Put RELATED inside an iterator: SUMX ( Sales, Sales[Qty] * RELATED ( Product[Unit Cost] ) ).

Oops

Using RELATED in the Product table to fetch a Sales value.

Do this instead

From the one side there are many matches, so use RELATEDTABLE and aggregate it.

Oops

Adding lots of RELATED columns to copy every product field into Sales.

Do this instead

Keep descriptions in the dimension table and let relationships filter. Only bring across what a calculation truly needs.

Your mission

Two directions, one relationship

~20 min

Quick check

You are writing a calculated column in the Customer table and want the number of sales rows for each customer. Which works?

Go deeper

  1. 1Bring product cost into a SUMX over Sales with RELATED.
  2. 2Count each customer's orders with COUNTROWS ( RELATEDTABLE ( Sales ) ).
  3. 3Note that both need an existing relationship to work.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge