Sub topic 5
Creating the table with Power Query
This chapter assumes you can open Power Query Editor in Power BI Desktop, you know basic M syntax (let ... in, steps that refer to earlier steps), and you know why a model needs a dedicated date table. It covers the Power Query route. The DAX route is in the previous chapter.
When Power Query is the better choice
A DAX calculated table is quick to write. A Power Query table is worth the extra step in these cases:
- The date table is loaded in a dataflow or semantic model shared by several reports.
- You want the table to be part of the refresh step with the rest of your queries, not calculated afterward in the model.
- You want to add calendar columns in M, or reuse the same query in Excel or in a dataflow.
Either route produces a table that meets the requirements: one row per day, no gaps, unique dates, and a column of type Date.
Build the list of dates
Open Home > Transform data, then New Source > Blank Query. Open the Advanced Editor and paste this query.
let
StartDate = #date(2020, 1, 1),
EndDate = #date(2026, 12, 31),
Dates = List.Dates(StartDate, Duration.Days(EndDate - StartDate) + 1, #duration(1, 0, 0, 0)),
AsTable = Table.FromList(Dates, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
Typed = Table.TransformColumnTypes(AsTable, {{"Date", type date}})
in
TypedEach step does one job:
StartDateandEndDateare fixed dates. Subtracting them gives a duration, andDuration.Daysturns that into a number of days.List.Datestakes a start date, a count, and a step. The count is the number of days plus one, so the end date is included. The step of one day (#duration(1, 0, 0, 0)) is what keeps the list free of gaps.Table.FromListconverts the list into a table with one column namedDate.Table.TransformColumnTypessets the column type to Date. Marking a table as the date table requires a Date column, so do not skip this step.
Rename the query to Date. Then choose Close & Apply.
Avoid hard-coded years
Fixed dates are easy to read, but you must edit them when the data grows past 2026. You can derive the range from the fact table instead. Add this to the top of the query, and make sure the Sales query is loaded and has a Date column named OrderDate.
let
FirstYear = Date.Year(List.Min(Sales[OrderDate])),
LastYear = Date.Year(List.Max(Sales[OrderDate])),
StartDate = #date(FirstYear, 1, 1),
EndDate = #date(LastYear, 12, 31),
Dates = List.Dates(StartDate, Duration.Days(EndDate - StartDate) + 1, #duration(1, 0, 0, 0)),
AsTable = Table.FromList(Dates, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
Typed = Table.TransformColumnTypes(AsTable, {{"Date", type date}})
in
TypedUsing whole years keeps the range complete, which is also a requirement for time intelligence functions.
Add calendar columns in M
You can add the calendar columns in Power Query too. Add steps after Typed, and change the in line to return the last step.
let
StartDate = #date(2020, 1, 1),
EndDate = #date(2026, 12, 31),
Dates = List.Dates(StartDate, Duration.Days(EndDate - StartDate) + 1, #duration(1, 0, 0, 0)),
AsTable = Table.FromList(Dates, Splitter.SplitByNothing(), {"Date"}, null, ExtraValues.Error),
Typed = Table.TransformColumnTypes(AsTable, {{"Date", type date}}),
WithYear = Table.AddColumn(Typed, "Year", each Date.Year([Date]), Int64.Type),
WithMonthNumber = Table.AddColumn(WithYear, "Month Number", each Date.Month([Date]), Int64.Type),
WithMonth = Table.AddColumn(WithMonthNumber, "Month", each Date.ToText([Date], "MMM"), type text)
in
WithMonthSet the type of each new column explicitly, as the last argument does here. Without it, the column has type Any, and later steps and sorting can behave unexpectedly. The Month Number column is there so that you can sort Month by it, which a later chapter covers.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Mark as date table is unavailable or reports an error | The column type is Date/Time, text, or Any | Set the column to Date in the query |
| The query fails with a reference error on `Sales` | The fact query has a different name, or the column has a different name | Check the query and column names in the Queries pane |
| The last day of the range is missing | The count passed to `List.Dates` omits the `+ 1` | Add one to the day count |
| Dates show a time such as 00:00:00 | The type is Date/Time | Change the type to Date |
Exercise
A query uses List.Dates(#date(2024, 1, 1), Duration.Days(#date(2024, 12, 31) - #date(2024, 1, 1)), #duration(1, 0, 0, 0)). How many rows does it return, and what is the last date?
Show answer
It returns 365 rows, and the last date is 30 December 2024. The difference between the two dates is 365 days (2024 is a leap year, so the span from January 1 to December 31 is 365 days), and without + 1 the list stops one day short. Adding 1 gives 366 rows that end on 31 December.
Exercise
Why must the Date column in a Power Query date table have type Date and not Date/Time?
Show answer
Marking a table as the date table requires a column of type Date, and a Date/Time type, or a column with Any type, can leave time values or fail the check.