Power Query course · Lesson 15 of 17
Dates, durations and a calendar table
Extract years, months and weeks, calculate durations and ages, handle ISO weeks and fiscal years, and build a dynamic calendar table in M.
Almost every business report is organised by time: sales by month, headcount by quarter, overdue invoices by days late. Power Query has a full toolbox for dates, and it is the ideal place to build a calendar table (also called a date dimension): one row per day with year, month, quarter, week and fiscal period columns, which you relate to your fact tables in the Data Model. This lesson covers both, starting with the buttons and ending with a reusable M query.
Dates must be dates
All date commands require the column type Date, Date/Time or Date/Time/Timezone. If a column still has the ABC icon, convert it first, using Change Type › Using Locale… when the source writes dates in another country's order (The Power Query Editor: steps, formula bar and data types). Text like 20260315 from older systems has no separators, so a locale does not help; add a custom column Date.FromText([Posting], [Format = "yyyyMMdd"]) instead. Excel serial numbers that arrive as numbers (46096) become dates with Date.From([Serial]).
The Date menu
Select a date column and open Add Column › Date (or Transform › Date to replace the column). The menu offers:
| Submenu | Commands | Example for 2026-03-15 |
|---|---|---|
| Age | Time between the date and now, as a duration | depends on today |
| Date Only | Drops the time part of a Date/Time | 2026-03-15 |
| Year | Year, Start of Year, End of Year | 2026 |
| Month | Month, Start of Month, End of Month, Days in Month, Name of Month | 3, 2026-03-01, March |
| Quarter | Quarter of Year, Start of Quarter, End of Quarter | 1 |
| Week | Week of Year, Week of Month, Start of Week, End of Week | 11 (Monday start) |
| Day | Day, Day of Week, Day of Year, Start of Day, End of Day, Name of Day | 15, 6 (Monday start), 74, Sunday |
| Subtract Days | Days between two selected date columns | with two columns selected |
| Combine Date and Time | Merges a date and a time column | with two columns selected |
| Earliest / Latest | The minimum or maximum date of the column | a single value |
Date.DayOfWeek([Date], Day.Monday) returns 0 for Monday and 6 for Sunday on every computer. Likewise Name of Day and Name of Month use the language of the computer that refreshes unless you pass a culture, as in Date.DayOfWeekName([Date], "en-US").Durations: days between dates
Subtracting two dates in M gives a duration, not a number. You can select two date columns and choose Add Column › Date › Subtract Days, but the sign of the result depends on the order in which you selected them. Writing the formula yourself makes the direction explicit:
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Invoice | Invoice date | Due date | Paid date |
| 2 | INV-3301 | 2026-02-02 | 2026-03-04 | 2026-03-01 |
| 3 | INV-3302 | 2026-02-10 | 2026-03-12 | 2026-04-02 |
| 4 | INV-3303 | 2026-02-15 | 2026-03-17 | null |
Before: invoices with due and payment dates. INV-3303 is still unpaid.
= Table.AddColumn(#"Changed Type", "Days late", each
let paid = [Paid date] ?? #date(2026, 4, 30) // report date for unpaid invoices
in Number.Max({0, Duration.Days(paid - [Due date])}),
Int64.Type)| A | B | C | D | |
|---|---|---|---|---|
| 1 | Invoice | Due date | Paid date | Days late |
| 2 | INV-3301 | 2026-03-04 | 2026-03-01 | 0 |
| 3 | INV-3302 | 2026-03-12 | 2026-04-02 | 21 |
| 4 | INV-3303 | 2026-03-17 | null | 44 |
After: days late per invoice, counting unpaid invoices up to the report date of 2026-04-30.
Other useful date arithmetic: Date.AddDays([Date], 30), Date.AddMonths([Date], 1) (which handles month ends: 31 January plus one month is 28 February in 2026), Date.EndOfMonth([Date]) like EOMONTH, and Date.IsInCurrentMonth([Date]) for rolling filters.
Age in whole years
The Age command returns a duration in days, which is awkward for HR reports. A precise age in years from a Birth date on a reference date:
= Table.AddColumn(#"Changed Type", "Age", each
let ref = #date(2026, 12, 31), b = [Birth date]
in Date.Year(ref) - Date.Year(b) - (if Date.Month(ref) * 100 + Date.Day(ref) < Date.Month(b) * 100 + Date.Day(b) then 1 else 0),
Int64.Type)Weeks: Week of Year versus ISO weeks
Week of Year (Date.WeekOfYear) counts week 1 as the week containing 1 January. Much of Europe uses ISO 8601 weeks instead, where week 1 is the week containing the first Thursday, weeks start on Monday, and the last days of December can belong to week 1 of the next year (or the first days of January to week 52 or 53). Power Query has no built-in ISO week function, so use this formula, which matches Excel's ISOWEEKNUM:
= Table.AddColumn(#"Changed Type", "ISO week", each
let
d = [Date],
thursday = Date.AddDays(d, 3 - Date.DayOfWeek(d, Day.Monday)),
jan1 = #date(Date.Year(thursday), 1, 1)
in
Number.IntegerDivide(Duration.Days(thursday - jan1), 7) + 1,
Int64.Type)Fiscal years
If your financial year starts in July, 1 July 2026 belongs to fiscal year 2027 (or 2026/27). Add a custom column:
= Table.AddColumn(#"Added ISO week", "Fiscal year", each if Date.Month([Date]) >= 7 then Date.Year([Date]) + 1 else Date.Year([Date]), Int64.Type)Building a calendar table
A calendar table has one row for every day between a start and an end date, with no gaps, even on days without sales. It is the backbone of time intelligence in the Data Model and in DAX (Power Pivot and the Data Model: relationships and DAX measures). Build it in a blank query: Data › Get Data › From Other Sources › Blank Query, then Home › Advanced Editor, and paste:
let
// Range: from the first day of the earliest sales year to the end of the latest year
StartDate = Date.StartOfYear(List.Min(Sales[Date])),
EndDate = Date.EndOfYear(List.Max(Sales[Date])),
DayCount = Duration.Days(EndDate - StartDate) + 1,
Dates = List.Dates(StartDate, DayCount, #duration(1, 0, 0, 0)),
Calendar = Table.FromList(Dates, Splitter.SplitByNothing(), type table [Date = date]),
AddYear = Table.AddColumn(Calendar, "Year", each Date.Year([Date]), Int64.Type),
AddMonthNo = Table.AddColumn(AddYear, "Month no", each Date.Month([Date]), Int64.Type),
AddMonth = Table.AddColumn(AddMonthNo, "Month", each Date.ToText([Date], [Format = "MMM", Culture = "en-US"]), type text),
AddYearMonth = Table.AddColumn(AddMonth, "Year-Month", each Date.ToText([Date], [Format = "yyyy-MM"]), type text),
AddQuarter = Table.AddColumn(AddYearMonth, "Quarter", each "Q" & Text.From(Date.QuarterOfYear([Date])), type text),
AddWeekday = Table.AddColumn(AddQuarter, "Weekday", each Date.ToText([Date], [Format = "ddd", Culture = "en-US"]), type text),
AddWeekdayNo = Table.AddColumn(AddWeekday, "Weekday no", each Date.DayOfWeek([Date], Day.Monday) + 1, Int64.Type),
AddWorkday = Table.AddColumn(AddWeekdayNo, "Is workday", each Date.DayOfWeek([Date], Day.Monday) < 5, type logical),
AddFiscal = Table.AddColumn(AddWorkday, "Fiscal year", each if Date.Month([Date]) >= 7 then Date.Year([Date]) + 1 else Date.Year([Date]), Int64.Type)
in
AddFiscal| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Date | Year | Month | Quarter | Weekday | Fiscal year |
| 2 | 2026-01-01 | 2026 | Jan | Q1 | Thu | 2026 |
| 3 | 2026-01-02 | 2026 | Jan | Q1 | Fri | 2026 |
| 4 | 2026-07-01 | 2026 | Jul | Q3 | Wed | 2027 |
| 5 | 2026-12-31 | 2026 | Dec | Q4 | Thu | 2027 |
| 6 | 2027-01-01 | 2027 | Jan | Q1 | Fri | 2027 |
After: selected rows of the calendar (some columns hidden). Note that 2027-01-01 is ISO week 53 of 2026, but Week of Year 1.
Month no column next to Month and, in the Data Model, use Sort by Column so Month sorts Jan, Feb, Mar rather than alphabetically. In a PivotTable without the Data Model, the Year-Month text 2026-03 sorts correctly on its own.Holidays and working days
Keep public holidays in an Excel table Holidays with a Date column. Merge it into the calendar with a Left Outer join on Date (Merge queries: every join kind explained), then set Is workday to false where a holiday matched. Counting working days between two dates then becomes a simple filter on the calendar, which is the Power Query equivalent of NETWORKDAYS.
List.Min(Sales[Date]) reads the whole Sales query again on refresh. With very large sources, prefer fixed boundaries from parameters (for example 2020-01-01 to the end of next year), as shown in Parameters and custom functions.Loading the calendar
Choose Home › Close & Load To…, select Only Create Connection and tick Add this data to the Data Model. In the Power Pivot window, relate Calendar[Date] to Sales[Date] and use Design › Mark as Date Table so DAX time-intelligence functions work. Make sure the Sales date column contains dates without times, or the relationship will not match.
Check yourself
1. What does [Paid date] - [Due date] return in M?
Date subtraction returns a duration; Duration.Days converts it to a number.
2. Which call returns 0 for Monday on every computer?
Passing Day.Monday fixes the first day of the week.
3. 2027-01-01 is a Friday. Which ISO week does it belong to?
Its Thursday is 2026-12-31, so it belongs to the last ISO week of 2026, week 53.
4. With a fiscal year starting in July, which fiscal year is 2026-07-01?
Months from July onwards belong to the next calendar year's fiscal year.
5. Why should a calendar table have no missing days?
Gaps break running totals and period comparisons in the Data Model.