Quantcast
Channel: Tabular – SQLBI
Viewing all articles
Browse latest Browse all 227

Using USERELATIONSHIP in DAX

$
0
0

This article shows how to use the USERELATIONSHIP function in DAX to change the active relationship in a CALCULATE function.

If two tables are linked by more than one relationship, you can decide which relationship to activate by using USERELATIONSHIP. Indeed, you can only have one active relationship between any two tables. For example, you start with a model where Sales is related to Date through the Date column, like in the following picture.

If you create a second relationship, based on the Delivery Date columns, the second relationship is an inactive relationship.

Inactive relationships are – by nature – non-active. Consequently, if you filter by Year the report shows the sales whose Order Date belongs to the filtered year. Delivery Date is not used as part of the default filtering. In order to filter Sales based on Delivery Date, you can temporarily activate a relationship using USERELATIONSHIP.

USERELATIONSHIP is a CALCULATE modifier, which instructs CALCULATE to temporarily activate the relationship. When CALCULATE has computed its result, the default relationship becomes active again.

The Delivery Amount measure below computes the amount delivered within a certain time period, in contrast with the ordered amount returned by the Sales Amount measure:

Delivery Amount :=
CALCULATE (
    [Sales Amount],
    USERELATIONSHIP ( Sales[Delivery Date], 'Date'[Date] )
)

You can compare Delivery Amount with Sales Amount in the following figure.

The arguments of USERELATIONSHIPS are the two columns that form the relationship. The order of the columns is not relevant, even though it is common practice to use the column of the many-side of the relationship (Sales in our example) first, and the one-side (Date in the example) second. With that said, it is just a standard convention; the opposite order does not alter the result.

The relationship must already be in the model. You cannot use USERELATIONSHIP to create a relationship on the fly. USERELATIONSHIP can only activate an existing relationship.

 


Viewing all articles
Browse latest Browse all 227

Trending Articles