Sub topic 10
Building a proper date dimension
This chapter assumes you can load tables in Power BI Desktop, use the Model view, and read simple DAX. You should also know what a primary key and a foreign key are. By the end you will be able to build a date table, mark it as a date table, and relate it to a fact table.
Why a separate date table
A fact table such as Sales holds one row per order line, and each row carries a date. You could group by that date column directly, but you would get only the dates that have sales. Gaps appear for days with no orders. You also have no month names, no sort order for them, and no fiscal periods.
A date dimension fixes this. It has one row for every day in a continuous range. Each row carries the attributes you slice by, such as year, month, and quarter. Time intelligence functions in DAX (for example, year-to-date or same period last year) need this continuous range to work correctly.
Power BI can create hidden automatic date tables for each date column. These are hard to control and add size to the model. Build one shared date dimension instead, and turn off auto date/time in the Power BI Desktop options.
Creating the table in DAX
This calculated table makes one row per day from 2022 through 2025. It adds a numeric key and a few 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. DateKey turns each date into an integer such as 20240315. It matches the DateKey column in the fact table from the earlier chapters. Integer keys are small and compress well.
Choose a range that covers every date in your fact data. Dates outside the range will not match any row in the date table. Many modelers start the range at the first year of data and end it at the end of the current year or later.
Sorting month names
The Month column holds text such as "Jan" and "Feb". Text sorts alphabetically, so April would appear first. Fix this with Sort by column.
- Select the
Monthcolumn in the Data pane. - On the Column tools ribbon, choose Sort by column.
- Pick
Month Number.
The same pattern applies to any text attribute with a natural order, such as weekday names.
Marking it as a date table
Tell Power BI that this table is the official calendar.
- Select the
Datetable. - Choose Table tools, then Mark as date table.
- Pick the
Datecolumn as the date column.
Power BI checks that the column has no blanks, no duplicates, and no missing days. Marking the table also makes time intelligence functions use it correctly.
Relating it to the fact table
Create a one-to-many relationship from Date[DateKey] to Sales[DateKey]. Keep the filter direction single, from the date table to the fact table. Then a slicer on Year filters Sales through the relationship, and measures such as [Total Sales] respond with no extra code.
A fact table with a second date, such as a ship date, reuses this same date table. The next chapters cover that role-playing case.
Common errors
- Gaps in the range. A date table with missing days breaks time intelligence. Use
CALENDAR, which always produces every day. - Range too short. Facts dated after the last row are dropped from date-based visuals.
- Time in the key. If the fact date includes a time, the values will not match the date table. Strip the time or use an integer
DateKey. - One date table per fact date. Use one table and role-playing relationships instead.
Exercise
A report shows months in the order Apr, Aug, Dec, Feb. The Date table has a Month text column and a Month Number column. What fix do you apply?
Show answer
Select the Month column and use Sort by column with Month Number. Text sorts alphabetically by default, so the numeric column supplies the calendar order.