Sub topic 11
Active and Inactive Relationships
This chapter assumes you can load tables into Power BI Desktop, open the Model view, and write a basic measure such as a sum of a column. It also assumes you know what a one-to-many relationship is and that a filter on a dimension table narrows the fact table. Here you will learn why a model can hold more than one relationship between the same two tables, how Power BI chooses which one to use, and how to switch to another one inside a measure.
Why a pair of tables can have several relationships
A fact table often holds more than one column that points to the same dimension. A Sales table might have OrderDate and ShipDate. Both columns contain dates, and both can be matched to the Date column of a Date table. Each match is a valid relationship.
Power BI allows you to create all of them. It does not allow all of them to filter at once. Between any two tables, only one relationship can be active. The others are inactive.
- An active relationship is the one the model uses by default. Filters travel across it automatically. It is drawn as a solid line in the Model view.
- An inactive relationship exists in the model but does nothing until a measure asks for it. It is drawn as a dashed line.
The reason is predictability. If both date relationships were active, a slicer on the Date table would have to filter OrderDate and ShipDate at the same time. The result would show only orders that were placed and shipped inside the selected period. That is rarely what anyone wants, and it would make results hard to explain.
How Power BI decides which relationship is active
When you add relationships, Power BI makes the first one active. If you add a second relationship between the same two tables, it is created as inactive. Power BI also sets a relationship to inactive when making it active would create an ambiguous path through the model. Chapter 10 covers ambiguous paths in detail.
You can change the setting yourself:
- Open the Model view and double-click the relationship line, or choose Manage relationships on the Home tab and select Edit.
- Find the Make this relationship active check box.
- Select or clear it, then choose Save.
If you select the box on an inactive relationship while another relationship between the same tables is active, Power BI makes the other one inactive. There is never more than one active relationship between a pair of tables.
Changing which relationship is active changes the numbers in every measure and visual that depends on it. Treat it as a model-wide change, not a local one.
Using an inactive relationship in a measure
An inactive relationship is not wasted. A measure can turn it on for the duration of one calculation by using USERELATIONSHIP inside CALCULATE.
Total Sales = SUM ( Sales[SalesAmount] )
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)USERELATIONSHIP takes the two columns that define the relationship. Inside this measure, the ShipDate relationship is active and the OrderDate relationship is switched off. Every other measure in the model still uses OrderDate. The change lasts only for the evaluation of the expression inside CALCULATE.
Put a month from the Date table on the rows of a matrix, with Total Sales and Sales by Ship Date as values. Each row shows the sales ordered in that month next to the sales shipped in that month. The two columns differ, and that is the point of the technique.
Rules and limits
- Both columns named in
USERELATIONSHIPmust belong to an existing relationship. If no relationship exists between them, the measure returns an error. Create the relationship first and leave it inactive. USERELATIONSHIPcan only be used as a filter argument of a function that accepts one, such asCALCULATEorCALCULATETABLE.- It enables a relationship. It does not create one. It also does not apply any filter by itself, because the filter still comes from the visual, the slicer, or other arguments of
CALCULATE. - If a measure activates one relationship, the active relationship that connects the same two tables is turned off inside that calculation. You cannot have both on at once.
- You can pass several
USERELATIONSHIParguments to oneCALCULATEwhen they connect different table pairs.
Active and inactive relationships and filter direction
Active status and filter direction are separate settings. An inactive relationship still has a cardinality and a cross-filter direction. When a measure activates it, it behaves according to those settings. A one-to-many inactive relationship with a single direction filters from the dimension to the fact table, just like an active one.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| The ship-date measure equals the order-date measure | The measure uses `[Total Sales]` without `USERELATIONSHIP`, or the relationship you wanted is the active one | Wrap the calculation in `CALCULATE` with `USERELATIONSHIP` |
| `USERELATIONSHIP` returns an error | No relationship exists between the two columns | Create the relationship in the Model view and leave it inactive |
| A new relationship appears dashed | An active relationship already exists between the tables, or an active one would be ambiguous | Keep it dashed and use `USERELATIONSHIP`, or change the active one on purpose |
| Visuals changed after editing a relationship | The active relationship was swapped | Restore the original active relationship and move the alternative into a measure |
| The Date slicer has no effect on a ship-date visual | The visual uses a measure that does not activate the `ShipDate` relationship, and no active relationship covers that column | Use a measure with `USERELATIONSHIP` |
Design choices
There are two ways to analyze by a second date.
- Inactive relationship plus
USERELATIONSHIP. The model has oneDatetable and one extra relationship. Each alternative date needs its own measure. This is compact, but you must write a version of each measure for each date. - Separate date tables. Each date role gets its own copy, such as
Order DateandShip Date, with its own active relationship. Users can place both on the same visual or slicer. The model is larger and the copies must be kept consistent. The next chapter covers this pattern as role-playing dimensions.
Choose the first approach when one date is clearly the main one and the others are used in a few measures. Choose the second approach when users need to slice by several dates in the same report.
Exercise
A Sales table has OrderDate and ShipDate, each related to Date[Date]. The OrderDate relationship is active. A report shows a column named Sales by Ship Date, defined as CALCULATE ( [Total Sales], USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) ). A colleague then makes the ShipDate relationship active in the Manage relationships dialog. What happens to Total Sales and to Sales by Ship Date?
Show answer
Total Sales now uses ShipDate, because ShipDate is the only active relationship and OrderDate becomes inactive. Sales by Ship Date still returns ship-date results, because USERELATIONSHIP activates ShipDate inside the calculation, and that relationship is already active. So the two columns now show the same numbers. To get order-date results again, the colleague would need to restore OrderDate as the active relationship, or the measures would need to be rewritten.
Exercise
Why does Power BI allow only one active relationship between two tables, and what must you create before USERELATIONSHIP can use a second one?
Show answer
With two active relationships, a Date slicer would filter OrderDate and ShipDate at once, producing results that are hard to interpret and ambiguous in meaning. Power BI therefore keeps one active and marks the others inactive. Before USERELATIONSHIP can use the second one, the relationship must exist in the model, created in the Model view and left inactive. USERELATIONSHIP only switches an existing relationship on and cannot create one.