0
0
Power-biHow-ToBeginner ยท 3 min read

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: RELATEDTABLE requires 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 RELATEDTABLE outside 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

FunctionDescriptionUsage Tip
RELATEDTABLE(TableName)Returns all rows from a related table matching current rowUse inside aggregation functions like SUMX
Requires relationshipWorks only if a relationship exists between tablesCheck model relationships before use
Row context neededBest used in calculated columns or iteratorsUse with functions like SUMX or FILTER
DirectionWorks from one-side to many-side in one-to-manyEnsure 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.