Sub topic 1
Start with a proper date table
Time intelligence functions such as TOTALYTD and SAMEPERIODLASTYEAR do not read dates from your sales table. They read them from a date table. If that table is wrong, every later measure in this tutorial returns wrong numbers, and usually no error message tells you why. This chapter builds the date table and connects it to your model.
Prerequisites
This chapter assumes you can:
- Write basic DAX measures such as SUM and DIVIDE.
- Describe a star schema, with a fact table (Sales) and a dimension table (Date).
- Put a measure into a Power BI matrix or line chart.
What makes a date table proper
A date table must meet these requirements:
- It has one row per day, with no gaps.
- The date column contains only dates, with no time portion, and every value is unique.
- It covers every date in your fact table, from January 1 of the first year to December 31 of the last year.
- It is related to the fact table on the date column.
- It is marked as the model's date table.
Time intelligence functions move through the date column by position. If a day is missing, a function that shifts by 30 days or one year can land on the wrong date or return blank.
Build the table
Create a calculated table with this DAX:
Date =
ADDCOLUMNS (
CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2025, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Year Month", FORMAT ( [Date], "yyyy-MM" )
)CALENDAR returns a single-column table named Date, with one row per day between the two dates. ADDCOLUMNS then adds the descriptive columns you will use as slicers and axis labels. Change the start and end dates so they cover your data. A hard-coded range is simple, but you must update it when new years of data arrive.
Relate and mark the table
The model should look like this:
Follow these steps:
- In Model view, drag Date[Date] onto Sales[Order Date]. Confirm the relationship is one-to-many, with the filter direction going from Date to Sales.
- Select the Date table, open Table tools, and choose Mark as date table. Pick the Date column.
Marking the table tells Power BI which column holds the calendar. It also validates the column: Power BI rejects it if it contains blanks, duplicates, or missing days.
Common errors
| Symptom | Likely cause |
|---|---|
| Year-to-date total never resets | The visual uses the Sales date column, not Date[Date] |
| Prior-year values are blank | The Date table does not reach back to the prior year |
| Mark as date table fails | The column has duplicates, blanks, or times |
| Some sales do not appear | The Order Date contains times, so rows do not match a midnight date |
Always put the Date table's columns on the axis, rows, or slicers of your visuals. A visual that uses the fact table's date column bypasses the Date table, and the time intelligence functions then have nothing to work with.
Exercise
Your Sales table has orders from 2021 to 2024, and you built a Date table that runs from 2022-01-01 to 2024-12-31. Which problem will you see, and how do you fix it?
Show answer
Sales from 2021 will not match any row in the Date table, so they drop out of date-based visuals, and prior-year measures for 2022 return blank. Change the CALENDAR start date to 2021-01-01 (or earlier) so the table covers every date in the fact table, then refresh.