Sub topic 10
Building a proper date dimension
This chapter assumes you can load data in Power BI Desktop, work in Model view, and write simple DAX. It also assumes you know what a one-to-many relationship is and why a fact table holds foreign keys. Here you build the date dimension that almost every star schema needs.
Why dates deserve their own table
A fact table usually has at least one date column, such as the order date. You could group by that column directly. A dedicated date dimension does more:
- It gives every fact table the same Year, Quarter, and Month columns, so visuals stay consistent.
- It holds attributes the fact column cannot, such as month names, weekday, fiscal period, and holiday flags.
- It has one row for every day in the range, including days with no sales. DAX time intelligence functions need that continuity.
- It lets several fact tables (sales, budgets, shipments) share the same slicers.
Power BI can create hidden date tables for you with the Auto date/time option. These are created per date column, they are hard to customize, and they cannot be shared across tables. For a star schema, turn the option off in Options > Current File > Data Load and build your own table.
What a proper date table must satisfy
Power BI requires these properties before it accepts a table as a date table:
- It has a column of type Date (or Date/Time) with unique values.
- That column has no blanks.
- The dates are contiguous, with no missing days.
- The range covers every date in the fact tables, and usually whole calendar years.
The last point is a common error. If the table ends on 30 June but the facts contain July orders, those rows lose their date context. Year-to-date and prior-year calculations then return wrong or blank results. Cover whole years from the first to the last year of your facts. If you also plan forecasts or budgets, extend the end date to include them.
Creating the table with DAX
A calculated table is the quickest way to generate a date dimension. This one produces one row per day from 2022 through 2025, with a numeric key and common attributes.
Date =
ADDCOLUMNS (
CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2025, 12, 31 ) ),
"DateKey", YEAR ( [Date] ) * 10000 + MONTH ( [Date] ) * 100 + DAY ( [Date] ),
"Year", YEAR ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "MMM" )
)CALENDAR returns a single column named Date. ADDCOLUMNS then adds the others. The DateKey follows the yyyymmdd pattern, so 27 November 2026 becomes 20261127. The fact table must use the same pattern in its own DateKey column.
You can add more columns in the same expression. These are useful in most reports:
Date =
ADDCOLUMNS (
CALENDAR ( DATE ( 2022, 1, 1 ), DATE ( 2025, 12, 31 ) ),
"DateKey", YEAR ( [Date] ) * 10000 + MONTH ( [Date] ) * 100 + DAY ( [Date] ),
"Year", YEAR ( [Date] ),
"Quarter", "Q" & QUARTER ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "MMM" ),
"Year Month", FORMAT ( [Date], "yyyy-MM" ),
"Day of Week Number", WEEKDAY ( [Date], 2 ),
"Day of Week", FORMAT ( [Date], "ddd" ),
"Is Weekend", WEEKDAY ( [Date], 2 ) >= 6
)WEEKDAY ( [Date], 2 ) numbers Monday as 1 and Sunday as 7, so weekends are 6 and 7.
Alternatives to a calculated table
| Approach | Strength | Weakness |
|---|---|---|
| DAX calculated table | Fast to write, lives inside the model | Recalculates on refresh and is not reusable outside this file |
| Power Query (M) | Can be folded into shared dataflows | More code for the same columns |
| Table in the source warehouse | One date table for every report | Needs access to the data platform |
If several reports use the same calendar, build it once in the warehouse or a dataflow. For a single report, DAX is usually enough.
Sorting month names correctly
Text columns sort alphabetically. A Month column showing Jan, Feb, Mar sorts as Apr, Aug, Dec in a chart axis. Fix it with Sort by column:
- Select the
Monthcolumn in the Data view. - In Column tools, choose Sort by column and pick
Month Number. - Repeat for
Day of Week, sorted byDay of Week Number.
Each name must map to exactly one number. Sorting Month by Month Number works. Sorting by a column that gives one name several numbers causes an error.
Mark it as a date table
Select the table, then choose Table tools > Mark as date table and pick the Date column. Power BI checks the requirements listed earlier and shows an error if any fail.
Marking the table does three things:
- It tells the engine which column holds the real calendar, so time intelligence functions work on it.
- It keeps time intelligence correct when the relationship uses an integer
DateKeyinstead of a Date column. - It turns off the automatic date hierarchies for that table's date column.
Without the mark, a function such as TOTALYTD can return wrong results, because the engine does not know which table is the calendar.
Relating the date dimension to the fact table
Create a one-to-many relationship from 'Date'[DateKey] (the one side) to Sales[DateKey] (the many side), single direction, as with any other dimension. Filters then flow from the date table to the facts, the same path the other dimensions use.
The diagram below shows the shape.
Hide Sales[DateKey] from the report view. Report authors should use the columns of the Date table, not the key in the fact table. Otherwise they may slice by the raw key and bypass the calendar.
Using the date dimension in measures
Measures from the earlier chapter work unchanged. When a visual puts 'Date'[Month] on the axis, the filter reaches the fact table and Total Sales is evaluated per month.
Total Sales = SUM ( Sales[SalesAmount] )Time intelligence needs a measure and the date column:
Sales YTD =
TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales Prior Year =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )Both pass the Date column from the date table, never a column from Sales. The functions need a complete, contiguous date column to shift periods correctly. That is why the gaps rule matters.
A common mistake is to build the calendar from the fact table's own dates, for example with VALUES ( Sales[OrderDate] ). That gives only days with sales, so the table has gaps and the prior-year calculation breaks for days without sales.
Fiscal calendars and other extras
If your organization's fiscal year does not start in January, add fiscal columns to the table. For a fiscal year starting in July, the fiscal year label can be YEAR ( [Date] ) + ( MONTH ( [Date] ) >= 7 ). Put the logic in one place, the date table, and every report picks it up. Other columns worth adding are holiday flags, working-day flags, and a "Current Month" flag you can use as a filter.
Keep the table readable. A date dimension with 40 columns that nobody uses makes the field list hard to scan. Hide columns that exist only for sorting, such as Month Number, once the sort-by relationship is set.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Year-to-date shows blank or wrong values | Date table not marked, or the range misses dates | Mark the table and widen the range |
| Months appear in alphabetical order | No Sort by column set | Sort `Month` by `Month Number` |
| "Mark as date table" is rejected | Duplicate, blank, or missing dates | Rebuild the table from a contiguous range |
| Facts for some rows show under "(Blank)" | Fact dates fall outside the calendar | Extend the start or end date |
| Two date hierarchies appear on the same field | Auto date/time is still on | Turn it off in Data Load options |
Exercise
A report shows Sales YTD, but the date table was built with CALENDAR ( DATE ( 2024, 1, 1 ), DATE ( 2025, 6, 30 ) ) and the fact table contains orders through December 2025. Name two problems this causes and how you would fix the table.
Show answer
Orders after 30 June 2025 have no matching row in the date table, so they show under blank and fall outside any month, quarter, or year-to-date calculation. The table also does not cover whole calendar years, so full-year comparisons and prior-year calculations for 2025 are incomplete. Change the end date to DATE ( 2025, 12, 31 ) (or later), refresh, and mark the table as a date table again if needed.
Exercise
Why should the Month column be sorted by Month Number, and what rule must hold between the two columns?
Show answer
A text column sorts alphabetically, so months would appear as Apr, Aug, Dec and so on. Sorting Month by Month Number gives calendar order. Each month name must map to exactly one number, and each number to one name, otherwise Power BI reports an error.