Sub topic 12
Handling order, ship, and due dates
A sales table often holds several date columns, such as OrderDate, ShipDate, and DueDate. Each column answers a different question, and each one needs the same calendar. This chapter shows how to connect one date table to several date columns and how to choose which column a measure uses.
It assumes you can already create a date table, relate it to a fact table, and mark it as the date table. It also assumes you can write a basic CALCULATE measure. The examples use a Sales table with OrderDate, ShipDate, DueDate, and Amount, plus the 'Date' table from earlier chapters.
Why several dates cause a problem
A date table works as a dimension. It filters the fact table through a relationship from 'Date'[Date] to a date column in Sales. Power BI allows only one active relationship between two tables, so only one of the three date columns can filter Sales by default.
The other relationships can exist, but they are inactive. They appear as dashed lines in Model view. Visuals and slicers ignore them. A measure can switch one on temporarily, and the rest of this chapter covers how.
The pattern is called a role-playing dimension. The same table plays the role of order date, ship date, or due date, depending on which relationship is in use.
Making the date table cover every date column
Before you add relationships, check the range of the date table. A table built only from MIN and MAX of OrderDate can miss ship and due dates. Due dates usually fall later than order dates, so the last weeks of the range are the most likely gap.
Rows whose date has no match in the date table are grouped under a blank member in visuals. Time intelligence also behaves badly when the range stops early. Build the range from all the date columns you plan to relate:
Date =
VAR MinDate =
MIN (
MIN ( Sales[OrderDate], Sales[ShipDate] ),
MIN ( Sales[DueDate] )
)
VAR MaxDate =
MAX (
MAX ( Sales[OrderDate], Sales[ShipDate] ),
MAX ( Sales[DueDate] )
)
RETURN
CALENDAR (
DATE ( YEAR ( MinDate ), 1, 1 ),
DATE ( YEAR ( MaxDate ), 12, 31 )
)The two-argument form of MIN and MAX compares two scalar values. Here the first argument compares the minimum of two columns, and the second adds a third. The table still starts on January 1 and ends on December 31, so it contains whole years.
If you use CALENDARAUTO instead, it scans the date columns in the model and also returns whole years. Check the result against your data either way.
Creating the relationships
Create the relationships in Model view. Drag 'Date'[Date] onto Sales[OrderDate], then onto Sales[ShipDate], and then onto Sales[DueDate].
When you create the second relationship between the same two tables, Power BI makes it inactive. If you added the relationships in a different order, open the relationship you want as the default and select or clear Make this relationship active.
All three relationships should be many-to-one, with the date table on the one side and a single cross-filter direction from Date to Sales. This is the same setup as for a single date column.
Choose the active relationship deliberately. It decides what every visual, slicer, and unmodified measure means. Most models make the order date active, because it is the event that creates the fact row.
Ship and due dates are often blank for some rows, for example orders not yet shipped. A blank key does not match a date. Those rows do not appear in any ship-date calculation, which is usually what you want. They still appear when you use the active order-date relationship.
Measures that activate an inactive relationship
USERELATIONSHIP is a CALCULATE modifier. It names the two columns of an existing relationship and activates that relationship for the duration of the calculation. The relationship it names must already exist in the model, and it must connect those exact columns.
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 Month from the date table on a matrix. Total Sales then shows sales by order month, and Sales by Ship Date shows the same amount grouped by ship month. A sale placed in December and shipped in January appears in December for the first measure and in January for the second.
Do not use the column from the fact table to group the visual, because the measures filter through the date table. Always use columns from 'Date' as the rows, columns, and slicers.
Combining a role with time intelligence
Time intelligence functions filter 'Date'[Date], and CALCULATE applies USERELATIONSHIP to every filter in the same call. You can therefore combine them. Put the relationship switch in CALCULATE and the date function next to it:
Ship Date Sales YTD =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
DATESYTD ( 'Date'[Date] )
)
Ship Date Sales PY =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ),
SAMEPERIODLASTYEAR ( 'Date'[Date] )
)Because CALCULATE activates the relationship before it evaluates its filter arguments, both DATESYTD and SAMEPERIODLASTYEAR work on ship dates.
A shortcut is to write a base role measure and then wrap it in the ordinary time intelligence measures. Because CALCULATE evaluates inner measures in the filter context it creates, this also works:
Sales by Ship Date YTD =
TOTALYTD ( [Sales by Ship Date], 'Date'[Date] )Here TOTALYTD sets the year-to-date date filter. Each measure it calls then applies its own USERELATIONSHIP. Use this form for readability when you have many role measures.
Letting users choose the role
A report with three role measures per metric becomes hard to maintain. A common alternative is a small disconnected table that lists the roles, and a slicer that selects one. The table has no relationships, so it filters nothing by itself. A measure reads the selection and chooses the relationship.
Date Role =
DATATABLE (
"Role", STRING,
"Sort", INTEGER,
{
{ "Order", 1 },
{ "Ship", 2 },
{ "Due", 3 }
}
)
Sales by Selected Date =
VAR SelectedRole = SELECTEDVALUE ( 'Date Role'[Role], "Order" )
RETURN
SWITCH (
SelectedRole,
"Ship",
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
),
"Due",
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[DueDate], 'Date'[Date] )
),
[Total Sales]
)Set Sort by column on 'Date Role'[Role] to 'Date Role'[Sort] so the slicer lists the roles in a sensible order. The default of "Order" is used when no role or several roles are selected. Add the role name to the visual title so readers know which date they are looking at.
Alternative: one date table per role
Instead of inactive relationships, you can create a separate date table for each role. Each table has an active relationship to its own fact column.
Ship Date = 'Date'This creates a calculated table that copies every column of 'Date'. Relate 'Ship Date'[Date] to Sales[ShipDate]. If you build the date table in Power Query, you can instead create a reference query for each role. Rename the columns in each copy, for example Ship Year, so that fields from different roles are easy to tell apart in the field list.
| Approach | Strengths | Costs |
|---|---|---|
| One table, inactive relationships | One calendar to maintain. Smaller model. | Each role needs its own measures. A single visual cannot filter by two roles. |
| One table per role | Slicers work directly. A visual can filter by order year and ship year together. No `USERELATIONSHIP` needed. | More tables and fields. Changes to the calendar must be repeated. Time intelligence needs each copy to be a valid date table. |
If you choose copies, a copy that you use with time intelligence should meet the same requirements as the original. It needs a date column of type Date, unique values, no gaps, and full years. If you mark a copy, mark it in the same way.
The deciding question is whether users must filter two roles at once, such as orders placed in 2025 that shipped in 2026. With one table and USERELATIONSHIP, a single filter on the date table applies to only one role at a time. Separate tables answer that question without extra DAX.
Common problems
- A measure returns the same value as
Total Sales. The relationship named inUSERELATIONSHIPmay be the active one, or the visual may not use a column from the date table. Check Model view for the dashed line. - An error says that a relationship does not exist.
USERELATIONSHIPneeds the column pair to match an existing relationship, in either argument order. A relationship to a different column or table does not count. - A blank row appears in a visual. Some fact dates fall outside the date table, or the key is blank. Check the range of the date table against every role column.
- Totals do not add up across roles. This is expected. Each role assigns the same sale to a different day, so a ship-date total for a period and an order-date total for the same period describe different sets of orders.
- Several active paths between tables. If your model has other tables that also relate to the date table, a second active path can cause ambiguity and an error. Keep a single active filter path from the date table to each table.
Exercise
A model has an active relationship from 'Date'[Date] to Sales[OrderDate] and an inactive one to Sales[ShipDate]. Write a measure named Shipped Qty that sums Sales[Quantity] by ship date, and say why a matrix with 'Date'[Month] on the rows then shows shipments rather than orders.
Show answer
Shipped Qty = CALCULATE ( SUM ( Sales[Quantity] ), USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] ) ). Inside CALCULATE, USERELATIONSHIP makes the ship-date relationship active and turns off the order-date one for that calculation. The month on each row filters the date table, and that filter reaches Sales through ShipDate, so each month contains the rows shipped in that month.
Exercise
Your date table was built from MIN and MAX of Sales[OrderDate]. After you relate DueDate, a visual shows a blank month with sales in it. What is the likely cause, and how do you fix it?
Show answer
Some due dates fall after the last order date, so they have no matching row in the date table and are grouped under the blank member. Rebuild the table so its range covers the minimum and maximum across OrderDate, ShipDate, and DueDate, for example with nested MIN and MAX calls or CALENDARAUTO, and keep whole years.
Exercise
Users want to see orders placed in 2025 that shipped in 2026 in one visual. Which approach fits better, one date table with USERELATIONSHIP measures or a separate date table for each role, and why?
Show answer
A separate date table for each role. Each table has its own active relationship, so a slicer on Order Year and a slicer on Ship Year can filter the fact table at the same time. With one date table, a measure can use only one role per calculation, which makes this combined filter harder.