Sub topic 13
RELATED, RELATEDTABLE, USERELATIONSHIP, and CROSSFILTER
This chapter assumes you can create relationships in the Model view and know the terms one-to-many, filter direction, and active versus inactive relationship. It also assumes you can write a basic measure with CALCULATE. The four DAX functions here let a formula use a relationship in a way the model diagram does not.
Two kinds of function
The four functions fall into two groups.
- RELATED and RELATEDTABLE read data across a relationship. They need a row context, which means a calculated column or an iterator such as SUMX.
- USERELATIONSHIP and CROSSFILTER change how a relationship behaves while a measure is evaluated. They work only inside CALCULATE or CALCULATETABLE, as filter arguments.
| Function | Returns | Direction it follows | Typical place to use it |
|---|---|---|---|
| RELATED | One value | Many side to one side | Calculated column, iterator |
| RELATEDTABLE | A table | One side to many side | Calculated column, iterator |
| USERELATIONSHIP | Nothing (a modifier) | Any existing relationship | CALCULATE argument |
| CROSSFILTER | Nothing (a modifier) | Any existing relationship | CALCULATE argument |
RELATED: fetch a value from the one side
RELATED starts at a row in the many-side table and follows the relationship to the single matching row on the one side. It returns a column value from that row.
Category = RELATED ( Product[Category] )Add this as a calculated column on Sales. Each sales row gets the Category of its product.
Things to know:
- RELATED needs a row context. In a plain measure there is no current row, so it returns an error. Wrap it in an iterator such as SUMX, which creates a row context for each Sales row.
- It can follow a chain of many-to-one relationships. From Sales you can reach Subcategory through Product, as long as every hop is many-to-one or one-to-one.
- If the key has no match, RELATED returns blank.
- It needs a relationship to follow. The relationship can be inactive only if you activate it in the same expression, which is awkward. Prefer an active one.
A measure example, assuming Sales has a Quantity column and Product has a ListPrice column:
List Price Value =
SUMX ( Sales, Sales[Quantity] * RELATED ( Product[ListPrice] ) )Should you use a calculated column?
A calculated column with RELATED copies a value into the many-side table. This uses memory and refresh time. If the goal is only to slice by Category, use Product[Category] directly in the visual. The relationship already filters Sales. Use RELATED when you need the value in a calculation, such as the price multiplication above.
RELATEDTABLE: fetch rows from the many side
RELATEDTABLE is the reverse of RELATED. From a row on the one side, it returns the table of matching rows on the many side.
Order Count = COUNTROWS ( RELATEDTABLE ( Sales ) )Add this as a calculated column on Customer. For each customer it counts the Sales rows that belong to that customer.
RELATEDTABLE is shorthand for CALCULATETABLE with no filter, because the row context turns into a filter through context transition. You can pass it to any function that takes a table, such as SUMX, MAXX, or FILTER. A customer with no sales gets 0, because COUNTROWS of an empty table is 0 in this case.
USERELATIONSHIP: use an inactive relationship for one measure
A model can hold only one active relationship between two tables. A Sales table often has OrderDate, ShipDate, and DueDate that all point to the Date table. One is active, usually OrderDate. The others are inactive. They show as dashed lines in the Model view and are ignored by default.
Total Sales = SUM ( Sales[SalesAmount] )
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)Inside this CALCULATE, the ShipDate relationship is active and the OrderDate relationship is switched off. Other measures keep using OrderDate. A visual with Date[Month] on the axis can then show both measures side by side, one by order month and one by ship month.
Rules and gotchas:
- The relationship must already exist in the model. USERELATIONSHIP selects a relationship. It does not create one.
- The two column arguments must be the exact endpoints of that relationship. The order does not matter.
- It works only as a CALCULATE or CALCULATETABLE argument. Using it elsewhere gives an error.
- It overrides the active relationship between the same two tables, so the original is ignored in that scope.
- Time-intelligence functions such as DATESYTD can be combined with it. Put USERELATIONSHIP in the same CALCULATE as the time function.
CROSSFILTER: change direction for one measure
The model's filter direction applies to every visual. CROSSFILTER changes it for one calculation.
Customers Who Bought =
CALCULATE (
DISTINCTCOUNT ( Customer[CustomerKey] ),
CROSSFILTER ( Sales[CustomerKey], Customer[CustomerKey], Both )
)Normally a Product slicer filters Product, then Sales, and stops there because the Sales-to-Customer relationship is single direction. Customer is not narrowed. With CROSSFILTER set to Both, the filtered Sales rows narrow Customer, so the count shows only customers who bought the selected products.
The third argument takes one of three values:
- One keeps or sets single-direction filtering, from the one side to the many side.
- Both filters in both directions.
- None disables the relationship for the calculation.
The None value is useful for ignoring a relationship on purpose, for example to compute a total that does not respond to a slicer on a related table.
CROSSFILTER has these limits:
- The relationship must exist, and the arguments must be its two endpoint columns.
- It works only inside CALCULATE or CALCULATETABLE.
- It changes only the relationship named, so a chain of tables needs one CROSSFILTER per hop.
- Setting Both brings back the risks of bidirectional filtering, including ambiguous paths and slower queries. The difference is that the effect is limited to one measure, so it is easier to find and test.
Choosing between them
- To read a value for each row, use RELATED.
- To summarize the related rows of each one-side row, use RELATEDTABLE.
- To use another date column, use USERELATIONSHIP.
- To let a filter travel the other way in one measure, use CROSSFILTER.
Exercise
A calculated column on the Customer table uses RELATED ( Sales[SalesAmount] ) and returns an error. Why, and which function fits better?
Show answer
RELATED follows a relationship from the many side to the one side. Customer is the one side and Sales is the many side, so there is no single sales row to return. Use RELATEDTABLE ( Sales ) inside an aggregate such as SUMX ( RELATEDTABLE ( Sales ), Sales[SalesAmount] ), which sums every matching Sales row.
Exercise
Your model has an active OrderDate relationship and an inactive ShipDate relationship to the Date table. Write a measure that returns Total Sales by ship date, and say what happens to the OrderDate relationship while it runs.
Show answer
Sales by Ship Date = CALCULATE ( [Total Sales], USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) ). While the measure is evaluated, the ShipDate relationship is used and the OrderDate relationship is ignored. Other measures still use OrderDate.
Exercise
Why might you prefer CROSSFILTER ( Sales[CustomerKey], Customer[CustomerKey], Both ) in a measure over setting the relationship to Both in the model?
Show answer
The model setting affects every visual and measure and can create ambiguous paths or slow queries. CROSSFILTER limits the bidirectional behavior to the one measure that needs it.