Sub topic 1
Start with a proper date table
Every time intelligence function in DAX depends on one object: a date table. If the date table has gaps, duplicates, or is not connected to your fact table, measures such as year to date and prior year return wrong numbers without raising an error. This chapter shows how to build a correct date table, connect it, and check it before you write any time intelligence measure.
Prerequisites
This chapter assumes you already know:
- How to write basic DAX measures such as
SUMandDIVIDE. - How a star schema works, with a fact table (for example
Sales) and a dimension table (for exampleDate) joined by a relationship. - How to place a measure in a Power BI visual such as a matrix or line chart.
You do not need to know CALCULATE yet. The next chapters cover it.
Why a separate date table
A fact table usually has a date column, such as Sales[Order Date]. It is tempting to use that column directly for time intelligence. It fails for three reasons:
- Missing days. If no order was placed on a Sunday, that Sunday has no row in
Sales. Time intelligence functions need every day to exist, or the periods they build have holes. - No place for attributes. Year, month number, and month name belong in a dimension table, not repeated on every sale.
- Several date columns. A fact table can have an order date and a ship date. A date table lets both relate to the same calendar.
A date table is a table with exactly one row per calendar day, covering whole years, with no gaps and no duplicates.
The requirements
A table qualifies as a date table when it meets these conditions:
| Requirement | Why it matters |
|---|---|
| One column of type Date (or Date/Time) | The model uses this column as the date key. |
| Every value is unique | Duplicates make the relationship and filtering ambiguous. |
| No blank values | A blank key breaks the "contiguous range" check. |
| No missing days between the first and last date | Functions such as `DATESYTD` and `DATEADD` step through consecutive days. |
| Covers full calendar years | A table that stops on 15 March gives an incomplete final year. |
| Starts on or before the earliest fact date and ends on or after the latest | Fact rows outside the range have no matching date and show up under a blank. |
Build the table with DAX
The simplest approach is a calculated table. CALENDAR returns a single-column table with one row for every day between two dates. ADDCOLUMNS then adds the attributes you want.
Date =
ADDCOLUMNS (
CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2025, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Year Month", FORMAT ( [Date], "yyyy-MM" )
)Create it in Power BI Desktop with Modeling > New table. The column that CALENDAR produces is named Date, which is why the formula refers to [Date] inside ADDCOLUMNS.
The fixed range of 2020 to 2025 is easy to read, but it will go stale. You can derive the range from the data instead:
Date =
VAR FirstYear = YEAR ( MIN ( Sales[Order Date] ) )
VAR LastYear = YEAR ( MAX ( Sales[Order Date] ) )
RETURN
ADDCOLUMNS (
CALENDAR ( DATE ( FirstYear, 1, 1 ), DATE ( LastYear, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Year Month", FORMAT ( [Date], "yyyy-MM" )
)Using DATE ( FirstYear, 1, 1 ) and DATE ( LastYear, 12, 31 ) guarantees whole years. Be aware that this version depends on Sales, so the date table is recalculated when the data refreshes.
CALENDARAUTO is a third option. It scans every date column in the model and builds a range of whole years. It is convenient, but any unrelated date column, such as a birth date from 1950 or a placeholder like 1900-01-01, stretches the range far beyond what you want. Prefer an explicit range unless you have checked every date column.
Make the display columns sort correctly
FORMAT ( [Date], "yyyy-MM" ) returns text, and text sorts alphabetically. For yyyy-MM this happens to match chronological order. A month name such as "April" does not, because it would sort before "January". If you add a month name column, set Sort by column to Month Number. The same applies to any text label that has a natural numeric order.
Connect the table to the fact table
Create a one-to-many relationship from Date[Date] (the one side) to Sales[Order Date] (the many side). Use a single filter direction from Date to Sales.
The relationship only matches when the values are identical. If Sales[Order Date] is a Date/Time column that holds times such as 2024-03-05 14:32, no row will match the midnight value 2024-03-05 in the date table. Fix this in the source query, or convert the column to the Date data type, before you build the relationship.
If the fact table has more than one date column, you can create one active relationship and the others inactive. Using an inactive relationship in a measure is a separate topic, covered later in this tutorial series.
Mark it as the date table
Select the Date table, then choose Table tools > Mark as date table and pick Date[Date] as the date column. Power BI validates the column, which must be unique, non-blank, and contiguous.
Marking has two effects:
- Time intelligence functions can identify the date column reliably, and they ignore any filter you have put on the table outside the date column, so the calendar behaves as a whole.
- Power BI turns off its built-in auto date/time tables for that column's visuals, so you work with your own table.
You should also turn off Auto date/time in File > Options > Current File > Data Load. Otherwise Power BI creates a hidden date table for every date column, which adds size to the model and can confuse you when a visual picks the hidden hierarchy instead of your own.
How the pieces fit
The date table sits between the report and the data. A visual filters Date, and the relationship passes the filter to Sales.
Chapters that follow rely on this flow. A time intelligence function does not read the fact table directly. It builds a different set of dates from Date, and the relationship then filters Sales to those dates.
Check the table before you rely on it
Do not assume the table is correct. Two quick checks catch most problems.
First, compare the number of rows with the number of days in the range. In DAX query view, run:
EVALUATE
ROW (
"Rows", COUNTROWS ( 'Date' ),
"Expected Days", DATEDIFF ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] ), DAY ) + 1,
"Distinct Dates", DISTINCTCOUNT ( 'Date'[Date] )
)All three numbers must be equal. If Rows is lower than Expected Days, days are missing. If Distinct Dates is lower than Rows, there are duplicates.
Second, look for fact rows that do not match any date. A blank row in a visual that is grouped by Date[Year] means some Sales[Order Date] values fall outside the range or contain time portions. Compare the earliest and latest sale with the earliest and latest date:
EVALUATE
ROW (
"First Sale", MIN ( Sales[Order Date] ),
"Last Sale", MAX ( Sales[Order Date] ),
"First Date", MIN ( 'Date'[Date] ),
"Last Date", MAX ( 'Date'[Date] )
)The first sale must not be earlier than the first date, and the last sale must not be later than the last date.
Common errors
- A year to date line runs flat after the last sale. The date table extends past the data. This is expected, and a later chapter shows how to blank those dates.
- A visual shows a blank year or month. Some fact dates fall outside the table, or they carry a time portion that prevents matching.
- Marking the table as a date table fails. The column has blanks, duplicates, or missing days.
- Months appear in alphabetical order. The text column is not sorted by its numeric partner.
- Totals differ between a date table column and the fact date column. Visuals are using the hidden auto date/time hierarchy instead of your table. Always use the
Datetable's columns in visuals and filters.
Exercise
A colleague builds a date table with CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2024, 6, 30 ) ) and relates it to a Sales table whose latest order is dated 2024-09-12. What two problems will this cause, and how would you fix the table?
Show answer
Orders after 2024-06-30 have no matching row in the date table, so they appear under a blank date, and the final year (2024) is incomplete, which breaks year-based comparisons. Extend the range to whole years that cover all sales, for example end the calendar on DATE ( 2024, 12, 31 ), or derive the end year from MAX ( Sales[Order Date] ). Then check that the row count equals the number of days between the first and last date, and mark the table as a date table.
Exercise
Sales[Order Date] contains values such as 2024-03-05 14:32. You relate it to Date[Date], but every sale is missing from date-based visuals. Why does this happen, and what is the fix?
Show answer
The relationship compares full values. The sale value 2024-03-05 14:32 is not equal to the date table's 2024-03-05 00:00, so no rows match. Remove the time portion by converting the column to the Date data type in Power Query (or in the source), then refresh and recreate the relationship if needed.