Sub topic 13
RELATED, RELATEDTABLE, USERELATIONSHIP, and CROSSFILTER
This chapter assumes you can create relationships in the Model view and know what cardinality, active versus inactive relationships, and filter direction mean. It covers four DAX functions that read or change relationships from a formula.
Two groups of functions
The four functions do different jobs.
- RELATED and RELATEDTABLE read data across a relationship. They work in row context, such as a calculated column.
- USERELATIONSHIP and CROSSFILTER change how a relationship behaves. They work only as filter arguments inside CALCULATE (or another function that accepts filter modifiers).
RELATED: fetch one value from the "one" side
RELATED follows a many-to-one relationship from the current row and returns a single value. Add this calculated column to the Sales table:
Category = RELATED ( Product[Category] )Each Sales row finds its product through the relationship and returns that product's Category. A relationship must exist between the tables. If no matching row exists, RELATED returns blank.
RELATEDTABLE: fetch the matching rows from the "many" side
RELATEDTABLE goes the other way. From a row on the "one" side, it returns the table of related rows. You usually wrap it in an aggregator. Add this calculated column to the Customer table:
Order Count = COUNTROWS ( RELATEDTABLE ( Sales ) )USERELATIONSHIP: use an inactive relationship in one measure
Suppose Sales has OrderDate and ShipDate, and both relate to the Date table. Only one relationship can be active, so assume OrderDate is active. This measure activates the ShipDate relationship for its own calculation:
Total Sales = SUM ( Sales[SalesAmount] )
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)While this measure evaluates, the ShipDate relationship is active and the OrderDate relationship is switched off. Other measures keep using OrderDate.
CROSSFILTER: change direction for one measure
CROSSFILTER sets the filter direction of a relationship inside a single calculation. The model stays single-direction.
Customers Who Bought =
CALCULATE (
DISTINCTCOUNT ( Customer[CustomerKey] ),
CROSSFILTER ( Sales[CustomerKey], Customer[CustomerKey], Both )
)With a Product slicer, the filter reaches Sales. CROSSFILTER then lets it continue from Sales to Customer, so the count shows only customers who bought the selected products.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| RELATED reports that no relationship exists | The tables are not related, or only through an inactive relationship | Create the relationship, or compute with USERELATIONSHIP |
| RELATED returns blank | The key value has no match on the "one" side | Check for unmatched keys |
| USERELATIONSHIP has no effect | The relationship is already active, or the columns do not match an existing relationship | Pass the exact column pair from an inactive relationship |
| RELATED fails in a measure | A measure has no row context | Use it in a calculated column, or inside an iterator such as SUMX |
Exercise
A Sales table has an active OrderDate relationship and an inactive ShipDate relationship to a Date table. Write a measure named Shipped Quantity that sums Sales[Quantity] by ship date.
Show answer
Shipped Quantity = CALCULATE ( SUM ( Sales[Quantity] ), USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) )
Exercise
Why can you use RELATED in a calculated column on Sales but not directly in a measure?
Show answer
A calculated column is evaluated once per row, so it has a row context for RELATED to follow. A measure has no row context unless an iterator such as SUMX creates one.