Short answer: relationships carry filters between tables, and four functions control them in DAX. RELATED reads a value from the one side for the current row; RELATEDTABLE returns the matching rows on the many side. USERELATIONSHIP makes an inactive relationship active inside one CALCULATE, for example sales by ship date instead of order date. CROSSFILTER changes the direction of a relationship (one way, both, none) for one calculation. For tables with no relationship at all, TREATAS applies values as a filter.
A model can have only one active path between two tables. A Sales table with an order date, a ship date and a due date has three relationships to the Date table, two of them inactive. These functions are how a measure picks the one it needs.
What does each function do?
| Function | Use in | Does |
|---|---|---|
| RELATED ( Table[Column] ) | row context on the many side | returns the related value from the one side |
| RELATEDTABLE ( Table ) | row context on the one side | returns the related rows from the many side |
| USERELATIONSHIP ( Col1, Col2 ) | CALCULATE filter argument | uses an existing relationship (active or not) for this calculation |
| CROSSFILTER ( Col1, Col2, Direction ) | CALCULATE filter argument | sets the filter direction: None, OneWay, Both, OneWay_LeftFiltersRight, OneWay_RightFiltersLeft |
| TREATAS ( Table, Col ) | CALCULATE filter argument | applies values to columns of a table with no relationship |
How do you use RELATED and RELATEDTABLE?
-- calculated column on Sales: the product's category
Category = RELATED ( 'Product'[Category] )
-- measure: revenue at list price
Revenue at List = SUMX ( Sales, Sales[Quantity] * RELATED ( 'Product'[List Price] ) )
-- calculated column on Customer: number of orders
Order Count = COUNTROWS ( RELATEDTABLE ( Sales ) )Both need a row context and an existing relationship. RELATED can follow several relationships in a chain (Sales to Product to Product Category), as long as each step goes to the one side.
How do you use an inactive relationship?
Sales has Order Date (active to 'Date'[Date]) and Ship Date (inactive, the dashed line in the model).
Revenue by Ship Date =
CALCULATE (
[Revenue],
USERELATIONSHIP ( Sales[Ship Date], 'Date'[Date] )
)
Orders Shipped =
CALCULATE (
COUNTROWS ( Sales ),
USERELATIONSHIP ( Sales[Ship Date], 'Date'[Date] )
)- By order date
- By ship date
Inside the CALCULATE, the ship date relationship is used and the order date relationship is ignored for that pair of tables. Microsoft notes that USERELATIONSHIP cannot be used when row-level security is defined for the table that the measure is in, and that the relationship must exist in the model.
How do you change the filter direction?
Relationships filter from the one side to the many side. To count the products a filtered set of customers bought, the filter must go from Sales back to Product:
Products Bought =
CALCULATE (
DISTINCTCOUNT ( 'Product'[Product Key] ),
CROSSFILTER ( Sales[Product Key], 'Product'[Product Key], Both )
)A simpler form that needs no direction change: DISTINCTCOUNT ( Sales[Product Key] ). Use CROSSFILTER ... Both in one measure rather than setting the relationship to both directions in the model, which affects every visual and can make filters ambiguous. None turns a relationship off for the calculation.
What if there is no relationship?
A Budget table at month and category grain cannot relate to the daily Date table directly. TREATAS applies the visible values as a filter:
Budget =
CALCULATE (
SUM ( Budget[Amount] ),
TREATAS ( VALUES ( 'Date'[Month] ), Budget[Month] ),
TREATAS ( VALUES ( 'Product'[Category] ), Budget[Category] )
)See TREATAS for more.
What goes wrong?
- RELATED from the one side. It only goes to the one side; use
RELATEDTABLE. - USERELATIONSHIP without the relationship. It must exist, inactive or active; create it in the model.
- Both-direction relationships in the model. They spread filters into unexpected tables and can create ambiguous paths; keep them single and use
CROSSFILTERin measures. - Row-level security.
USERELATIONSHIPerrors on tables with RLS; consider a second date role table instead.
Where can you check this?
- Microsoft Learn: USERELATIONSHIP function (DAX)
- Microsoft Learn: CROSSFILTER function (DAX)
- Microsoft Learn: Active vs inactive relationship guidance
Where do you start?
Add the inactive ship date relationship and a Revenue by Ship Date measure, and put both measures on one chart. The date table post shows how to build the table these relationships point at.