Generating a Date table
Almost every data model needs a Date table. It holds one row per date, and its other columns hold values worked out from that date, such as the month name, the quarter and the financial year.
A report that totals invoices by month or by financial year groups them on those columns. Without a Date table, each fact table would need its own copy of every month and year column, and a month with no invoices would drop out of the report instead of showing a zero. A Date table with no gaps solves both problems, because every fact table relates to it on its date column and every date in the range has a row.
Many data sources already include a Date table. This page shows how to generate one in Power Query instead, so that it covers exactly the range of dates the model needs. The example is a small film and TV production company whose accounts team keeps an Invoices query of supplier invoices, each with an InvoiceDate.
1. Write a calendar function
Section titled “1. Write a calendar function”The calendar is written as a function, so that the same steps can build a table for any range of dates.
- On Excel’s Data tab, click Get Data ➔ From Other Sources ➔ Blank Query.
- In the Power Query Editor, click Advanced Editor on the Home tab, replace its contents with the code below, and click Done.
- Rename the query fnCalendar.
(startDate as date, endDate as date) as table =>let // Each output column with its type, in the order the columns appear ColumnTypes = { {"Year", Int64.Type}, {"Month Number", Int64.Type}, {"Month Name", type text}, {"Short Month", type text}, {"Quarter", type text}, {"Day Name", type text}, {"Day of Week", Int64.Type}, {"Week of Year", Int64.Type}, {"Is Weekend", type logical}, {"Year Month", Int64.Type}, {"Financial Year", type text}, {"Financial Month", Int64.Type} },
// One date per day, from startDate to endDate inclusive DayCount = Duration.Days(endDate - startDate) + 1, DateList = List.Dates(startDate, DayCount, #duration(1, 0, 0, 0)), DateColumn = Table.FromColumns({DateList}, type table [Date = date]),
// Work out every other column for a date in one record WithAttributes = Table.AddColumn( DateColumn, "Attributes", each let thisDate = [Date], monthNumber = Date.Month(thisDate), monthName = Date.MonthName(thisDate, "en-GB"), dayOfWeek = Date.DayOfWeek(thisDate, Day.Monday) + 1, fyStartYear = if monthNumber >= 4 then Date.Year(thisDate) else Date.Year(thisDate) - 1 in [ Year = Date.Year(thisDate), Month Number = monthNumber, Month Name = monthName, Short Month = Text.Start(monthName, 3), Quarter = "Q" & Text.From(Date.QuarterOfYear(thisDate)), Day Name = Date.DayOfWeekName(thisDate, "en-GB"), Day of Week = dayOfWeek, Week of Year = Date.WeekOfYear(thisDate, Day.Monday), Is Weekend = dayOfWeek >= 6, Year Month = Date.Year(thisDate) * 100 + monthNumber, Financial Year = "FY" & Text.From(fyStartYear) & "/" & Text.End(Text.From(fyStartYear + 1), 2), Financial Month = Number.Mod(monthNumber + 8, 12) + 1 ] ),
// Spread the record into columns, then give each column its type Expanded = Table.ExpandRecordColumn(WithAttributes, "Attributes", List.Transform(ColumnTypes, each _{0})), Typed = Table.TransformColumnTypes(Expanded, ColumnTypes)in TypedThe function works in four stages:
- ColumnTypes lists every column that the function adds, with its type. The last two steps both read it, so a new column is named in one place.
- DayCount is the number of days from startDate to endDate, counting both ends. List.Dates() returns that many dates, one day apart, and Table.FromColumns() turns the list into a one-column table whose Date column is already typed as a date.
- WithAttributes adds a column that holds a record for each row. The record’s fields become the other columns, and they are worked out together from the row’s date, so that a value such as the month number is calculated once and shared.
- Expanded turns each field of the record into a column of its own, and Typed sets each column’s type from ColumnTypes.
2. What each column holds
Section titled “2. What each column holds”- Year, Month Number and Quarter hold the calendar year, the month from 1 to 12, and the quarter from Q1 to Q4.
- Month Name holds the full name of the month, and Short Month holds its first three letters, such as Jan.
- Day Name holds the name of the weekday, and Day of Week numbers the days from Monday as 1 to Sunday as 7. Date.DayOfWeek() counts from 0, so the function adds 1.
- Week of Year is the result of Date.WeekOfYear() with weeks that start on Monday. Week 1 is the week that contains 1 January, however few of its days fall in January, and the last days of December can fall in week 53. This is not the ISO week number: ISO puts Monday 29 December 2025 in week 1 of 2026, whereas this column puts it in week 53 of 2025.
- Is Weekend is true on Saturdays and Sundays.
- Year Month is the year and month as one number, such as 202504 for April 2025. It sorts in date order, which makes it a useful key for monthly totals.
- Financial Year follows the U.K. financial year, which runs from 1 April to 31 March, and is written as FY2025/26. Financial Month numbers the months of that year, so April is 1 and March is 12.
The function asks for the month and day names in the en-GB culture, so that they come out in English whatever the computer’s own language settings are.
3. Try the function
Section titled “3. Try the function”Select fnCalendar in the Queries pane. The editor shows a form with a box for each parameter.
- Enter 01/01/2025 as startDate and 31/12/2025 as endDate.
- Click Invoke.
Power Query adds a query called Invoked Function, which holds 365 rows. These are its first week, the last day of March and the first day of April, and a day near the end of the year:
| Date | Year | Month Number | Month Name | Short Month | Quarter | Day Name | Day of Week | Week of Year | Is Weekend | Year Month | Financial Year | Financial Month |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 01/01/2025 | 2025 | 1 | January | Jan | Q1 | Wednesday | 3 | 1 | FALSE | 202501 | FY2024/25 | 10 |
| 02/01/2025 | 2025 | 1 | January | Jan | Q1 | Thursday | 4 | 1 | FALSE | 202501 | FY2024/25 | 10 |
| 03/01/2025 | 2025 | 1 | January | Jan | Q1 | Friday | 5 | 1 | FALSE | 202501 | FY2024/25 | 10 |
| 04/01/2025 | 2025 | 1 | January | Jan | Q1 | Saturday | 6 | 1 | TRUE | 202501 | FY2024/25 | 10 |
| 05/01/2025 | 2025 | 1 | January | Jan | Q1 | Sunday | 7 | 1 | TRUE | 202501 | FY2024/25 | 10 |
| 06/01/2025 | 2025 | 1 | January | Jan | Q1 | Monday | 1 | 2 | FALSE | 202501 | FY2024/25 | 10 |
| 07/01/2025 | 2025 | 1 | January | Jan | Q1 | Tuesday | 2 | 2 | FALSE | 202501 | FY2024/25 | 10 |
| 31/03/2025 | 2025 | 3 | March | Mar | Q1 | Monday | 1 | 14 | FALSE | 202503 | FY2024/25 | 12 |
| 01/04/2025 | 2025 | 4 | April | Apr | Q2 | Tuesday | 2 | 14 | FALSE | 202504 | FY2025/26 | 1 |
| 29/12/2025 | 2025 | 12 | December | Dec | Q4 | Monday | 1 | 53 | FALSE | 202512 | FY2025/26 | 9 |
1 January 2025 is a Wednesday, so week 1 runs from that Wednesday to Sunday 5 January, and week 2 starts on Monday 6 January. The financial year changes between 31 March and 1 April, where Financial Month starts again at 1.
Delete the Invoked Function query once the output looks right, because the next step calls the function from a query of its own.
4. Size the calendar from the invoices
Section titled “4. Size the calendar from the invoices”Fixed dates go out of date as soon as a new invoice arrives. The calendar can instead read its range from the Invoices query, so that it always runs from the start of the year of the earliest invoice to the end of the year of the latest one. Whole years keep the yearly and quarterly totals complete.
- Create another blank query, name it Calendar, and enter this code in the Advanced Editor:
let FirstInvoiceDate = List.Min(Invoices[InvoiceDate]), LastInvoiceDate = List.Max(Invoices[InvoiceDate]), CalendarStart = Date.StartOfYear(FirstInvoiceDate), CalendarEnd = Date.EndOfYear(LastInvoiceDate), CalendarTable = fnCalendar(CalendarStart, CalendarEnd)in CalendarTableList.Min() and List.Max() find the earliest and the latest InvoiceDate. Date.StartOfYear() moves the earliest back to 1 January of its year, and Date.EndOfYear() moves the latest on to 31 December of its year. The InvoiceDate column must be typed as a date in the Invoices query, because fnCalendar accepts only dates.
With invoices dated from 4 March to 12 March 2025, Calendar runs from 1 January to 31 December 2025. It grows by a year when the first invoice dated in 2026 is loaded.
5. Load the calendar to the Data Model
Section titled “5. Load the calendar to the Data Model”- On the Home tab, click Close & Load ➔ Close & Load To.
- Choose Only Create Connection, tick Add this data to the Data Model, and click OK.
fnCalendar needs no table of its own, because it is a function. If Excel has loaded it to a sheet, change its load setting to Only Create Connection.
In the Data Model, relate Invoices[InvoiceDate] to Calendar[Date], and mark Calendar as the date table, with Date as its date column. Then set Month Name and Short Month to sort by Month Number, so that the months appear in calendar order rather than alphabetically.
When the workbook is refreshed, Calendar reads the new range of invoice dates and rebuilds itself to cover it.