Power BI Practice: Using DAX USERELATIONSHIP for Multiple Date Keys
Scenario:
You have a Power Bl report that imports a date table and a sales table from an Azure SQL database data source.
The sales table has the following date foreign keys: 1. Due Date 2. Order Date 3. Delivery Date
You need to support the analysis of sales over time based on all three dates at the same time.
Solution:
1. From the Fields pane, you rename the date table as Due Date.
2. You use a DAX expression to create Order Date and Delivery Date as calculated tables.
3. You create active relationships between the sales table and each date table.
Does this meet the goal?
Answer: No, this solution does not meet the goal of supporting the analysis of sales over time based on all three dates at the same time. In Power BI, you can only have one active relationship between two tables at a time. Creating active relationships between the sales table and each date table simultaneously is not possible.
Instead, you can use the following approach.
Step-by-step guide
[1] Rename the Date Table: Rename the date table to something generic like Date.
[2] Create Inactive Relationships: Create inactive relationships between the sales table and the Date table for Order Date and Delivery Date.
[3] Use USERELATIONSHIP in DAX: Use the USERELATIONSHIP function in your DAX measures to activate the appropriate relationship when needed. For example:
Total Sales by Due Date =
SUM ( Sales[Amount] )
.
Total Sales by Order Date =
CALCULATE (
SUM ( Sales[Amount] ),
USERELATIONSHIP ( Sales[Order Date], Date[Date] )
)
.
Total Sales by Delivery Date =
CALCULATE (
SUM ( Sales[Amount] ),
USERELATIONSHIP ( Sales[Delivery Date], Date[Date] )
)
This way, you can analyze sales over time based on all three dates without needing multiple active relationships.