Sub topic 12
Handling order, ship, and due dates
This chapter assumes you have a marked date table related to a fact table, and that you can write basic DAX measures. Many fact tables hold more than one date. A sales table might have OrderDate, ShipDate, and DueDate. You still want one date table, not three.
One date table, several relationships
Power BI allows only one active relationship between two tables. You can create more relationships from the same fact table to the date table, but the extra ones are inactive. Inactive relationships appear as dashed lines in Model view.
The usual setup:
Sales[OrderDate]to'Date'[Date]is active. Visuals and time intelligence use it by default.Sales[ShipDate]to'Date'[Date]is inactive.Sales[DueDate]to'Date'[Date]is inactive.
A single date table in this role is called a role-playing dimension. The same table plays the part of order date, ship date, or due date, depending on which relationship a measure uses.
Activating a relationship in a measure
USERELATIONSHIP turns on an inactive relationship for the duration of one CALCULATE. The other relationships are unchanged.
Total Sales = SUM ( Sales[Amount] )
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)
Sales by Due Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[DueDate], 'Date'[Date] )
)Put 'Date'[Month] on a matrix rows and add all three measures. Each column groups the same sales by a different date. An order placed on December 28 and shipped on January 3 counts in December for Total Sales and in January for Sales by Ship Date.
Combining with time intelligence
Time intelligence functions work on whichever relationship is active inside the calculation. To get year-to-date by ship date, wrap the time intelligence measure in USERELATIONSHIP:
Sales YTD by Ship Date =
CALCULATE (
TOTALYTD ( [Total Sales], 'Date'[Date] ),
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)The year-to-date filter is still built from 'Date'[Date]. Only the link between the date table and the fact table changes.
Choosing between this and separate date tables
You can also import the date table several times, for example Order Date, Ship Date, and Due Date, each with its own active relationship. The trade-offs:
| Approach | Strength | Cost |
|---|---|---|
| One table, inactive relationships | Small model. One set of columns to maintain. | Each alternate date needs its own measures. |
| Several date tables | Slicers and visuals can use any date directly. No extra measures. | Larger model. Each copy must be built and marked separately. |
Use one table when you have many measures that rarely need an alternate date. Use separate tables when users need to slice by ship date or due date in the same way they slice by order date.
Common mistakes
- Expecting a visual to use the ship date because you placed
ShipDatein it. The visual follows the active relationship unless the measure overrides it. - Making two relationships active. Power BI turns the second one off automatically, so check Model view for the dashed line.
- Using
USERELATIONSHIPon a column that is not the many side of the relationship. - Leaving blank dates in
ShipDatefor unshipped orders. Those rows have no matching date and are not counted inSales by Ship Date. This is often correct, but confirm it is what you want.
Exercise
A Sales table has an active relationship on OrderDate and an inactive one on DueDate. Write a measure named Sales Due that returns total sales grouped by due date, using the existing [Total Sales] measure.
Show answer
Sales Due = CALCULATE ( [Total Sales], USERELATIONSHIP ( Sales[DueDate], 'Date'[Date] ) )
Exercise
A matrix shows Month on rows. Total Sales for January is 100, and Sales by Ship Date for January is 80. What is a likely reason for the difference?
Show answer
Some orders placed in January shipped in a later month, and some January shipments may belong to orders placed earlier. The two measures group the same rows by different dates, so monthly totals differ. Unshipped orders with a blank ShipDate are also left out of the ship-date measure.