Stop 09 · Advanced DAX PatternsTopics · 2/10

USERELATIONSHIP

Part of the Power BI Roadmap

In short

An order placed on 28 March might ship on 3 April. Finance wants sales by order date, the warehouse wants them by ship date. USERELATIONSHIP lets one date table serve both, one measure at a time.

USERELATIONSHIP, used inside CALCULATE, activates an inactive relationship for one calculation, such as analysing sales by ship date instead of order date.

The jaadu pulls a big lever at a railway junction so a small toy train glides onto a dotted side track while the main track stays empty.

Think of it like this

Switching the railway points

Two tracks leave the station, but only one switch is set at a time. Trains roll down the main track by default. When a special train needs the side track, the pointsman flips the lever just for that train, then flips it back. USERELATIONSHIP is that lever, flipped inside one CALCULATE.

Watch it happen

Same date table, two relationships

Active: OrderDate

  • Solid line in the model
  • Used by every measure automatically
  • Answers: what did we sell in March?
  • Only one active path allowed between two tables

Inactive: ShipDate

  • Dotted line in the model
  • Ignored unless a measure asks for it
  • Answers: what did we ship in March?
  • Turned on with USERELATIONSHIP inside CALCULATE

Keep the most common path active and switch to the other one only in the measures that need it.

The big ideas

What you need to know

One active path

Power BI allows only one active relationship between two tables. Extra ones are created as inactive, shown as dotted lines.

Activate per measure

USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) works only as a CALCULATE filter argument and only for that calculation.

Relationship must exist

You can't invent a relationship with it. Draw the inactive relationship in Model view first.

No duplicate date tables

One date table plus inactive relationships is cleaner than copying the date table for every date column.

How it works

Setting it up

  1. 1

    Relate OrderDate

    Drag Sales[OrderDate] to 'Date'[Date]. This becomes the active relationship.

  2. 2

    Relate ShipDate

    Drag Sales[ShipDate] to 'Date'[Date]. Power BI makes it inactive because a path already exists.

  3. 3

    Write the measure

    Wrap your base measure in CALCULATE with USERELATIONSHIP naming both columns.

  4. 4

    Compare side by side

    Put both measures in one table by month and watch March orders spill into April shipments.

See it in code

Walk through the code

dax
1Total Sales = SUM ( Sales[Amount] )2 3Sales by Ship Date =4CALCULATE (5    [Total Sales],6    USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )7)8 9Orders Shipped =10CALCULATE (11    COUNTROWS ( Sales ),12    USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),13    NOT ISBLANK ( Sales[ShipDate] )14)

1/3The base measure. On its own it follows the active OrderDate relationship.

Order-date sales use the active relationship; ship-date measures switch to the inactive one.

Save yourself the pain

Common mistakes

Oops

Making both relationships active by deleting and redrawing them.

Do this instead

Power BI won't allow two active paths. Keep one inactive and call it with USERELATIONSHIP.

Oops

Using USERELATIONSHIP on its own in a measure.

Do this instead

It is only valid as a filter argument of CALCULATE or CALCULATETABLE.

Oops

Creating a second date table called Ship Date just to avoid this function.

Do this instead

That works, but doubles maintenance. One date table plus USERELATIONSHIP is usually simpler.

Your mission

Ordered vs shipped

~20 min

Quick check

Where can USERELATIONSHIP be used?

Go deeper

  1. 1Create Sales by Ship Date using your inactive date relationship.
  2. 2Show order-date and ship-date sales side by side in one line chart.
  3. 3Keep the most common date relationship as the active one.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge