How to Use RELATEDTABLE in DAX in Power BI
Use
RELATEDTABLE in DAX to get all rows from a related table that match the current row in the main table based on existing relationships. It returns a table of related rows, often used inside aggregation functions to summarize related data.Syntax
The syntax of RELATEDTABLE is simple:
RELATEDTABLE(TableName)
Where TableName is the name of the related table you want to retrieve rows from.
This function returns a table containing all rows from TableName that are related to the current row in the context table.
DAX
RELATEDTABLE(RelatedTableName)
Example
This example shows how to calculate the total sales amount for each product by retrieving related sales rows from the Sales table.
Assume you have two tables: Products and Sales, related by ProductID.
DAX
Total Sales = SUMX(RELATEDTABLE(Sales), Sales[Amount])
Output
For Product A: 1500
For Product B: 2300
For Product C: 0 (if no sales)
Common Pitfalls
- Missing relationship:
RELATEDTABLErequires a defined relationship between tables; otherwise, it returns an empty table. - Wrong table direction: The function works from the one-side to the many-side in a one-to-many relationship.
- Using in wrong context: Using
RELATEDTABLEoutside a row context (like in a measure without row context) may not work as expected.
DAX
/* Wrong: No relationship defined */ Total Sales Wrong = SUMX(RELATEDTABLE(Sales), Sales[Amount]) /* Correct: Relationship exists between Products and Sales */ Total Sales Correct = SUMX(RELATEDTABLE(Sales), Sales[Amount])
Quick Reference
| Function | Description | Usage Tip |
|---|---|---|
| RELATEDTABLE(TableName) | Returns all rows from a related table matching current row | Use inside aggregation functions like SUMX |
| Requires relationship | Works only if a relationship exists between tables | Check model relationships before use |
| Row context needed | Best used in calculated columns or iterators | Use with functions like SUMX or FILTER |
| Direction | Works from one-side to many-side in one-to-many | Ensure correct relationship direction |
Key Takeaways
RELATEDTABLE returns all rows from a related table based on existing relationships.
Use RELATEDTABLE inside aggregation functions like SUMX to summarize related data.
Ensure a proper relationship exists between tables for RELATEDTABLE to work.
RELATEDTABLE works best in row context, such as calculated columns or iterators.
It retrieves related rows from the many-side table in a one-to-many relationship.