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.

Advanced 20 min read

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:

SubmenuCommandsExample for 2026-03-15
AgeTime between the date and now, as a durationdepends on today
Date OnlyDrops the time part of a Date/Time2026-03-15
YearYear, Start of Year, End of Year2026
MonthMonth, Start of Month, End of Month, Days in Month, Name of Month3, 2026-03-01, March
QuarterQuarter of Year, Start of Quarter, End of Quarter1
WeekWeek of Year, Week of Month, Start of Week, End of Week11 (Monday start)
DayDay, Day of Week, Day of Year, Start of Day, End of Day, Name of Day15, 6 (Monday start), 74, Sunday
Subtract DaysDays between two selected date columnswith two columns selected
Combine Date and TimeMerges a date and a time columnwith two columns selected
Earliest / LatestThe minimum or maximum date of the columna single value
Watch out. Day of Week numbers days from 0, and which day is 0 depends on the system's first day of the week. Write the start explicitly in the formula bar: 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:

ABCD
1InvoiceInvoice dateDue datePaid date
2INV-33012026-02-022026-03-042026-03-01
3INV-33022026-02-102026-03-122026-04-02
4INV-33032026-02-152026-03-17null

Before: invoices with due and payment dates. INV-3303 is still unpaid.

Power Query M
= 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)
Duration.Days turns the duration into a whole number of days; negative values (paid early) become 0. In a live report replace the fixed report date with Date.From(DateTime.LocalNow()).
ABCD
1InvoiceDue datePaid dateDays late
2INV-33012026-03-042026-03-010
3INV-33022026-03-122026-04-0221
4INV-33032026-03-17null44

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:

Power Query M
= 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)
Subtract the years, then one more if the birthday has not yet occurred in the reference year.

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:

Power Query M
= 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)
Every ISO week is identified by its Thursday; the week number is how many whole weeks that Thursday is into its year, plus one.

Fiscal years

If your financial year starts in July, 1 July 2026 belongs to fiscal year 2027 (or 2026/27). Add a custom column:

Power Query M
= Table.AddColumn(#"Added ISO week", "Fiscal year", each if Date.Month([Date]) >= 7 then Date.Year([Date]) + 1 else Date.Year([Date]), Int64.Type)
Change 7 to the first month of your fiscal year. Fiscal month is Number.Mod(Date.Month([Date]) + 5, 12) + 1.

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:

Power Query M
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 dynamic calendar: the date range follows the Sales query, so it grows automatically when new years appear.
ABCDEF
1DateYearMonthQuarterWeekdayFiscal year
22026-01-012026JanQ1Thu2026
32026-01-022026JanQ1Fri2026
42026-07-012026JulQ3Wed2027
52026-12-312026DecQ4Thu2027
62027-01-012027JanQ1Fri2027

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.

Tip. Add a 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.

Watch out. A calendar based on 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.

Note. Date functions behave the same on Windows, Mac and the web. The only real differences come from regional settings: always pass a culture to name functions and a locale to type conversions, and the query gives identical results everywhere.

Check yourself

1. What does [Paid date] - [Due date] return in M?

2. Which call returns 0 for Monday on every computer?

3. 2027-01-01 is a Friday. Which ISO week does it belong to?

4. With a fiscal year starting in July, which fiscal year is 2026-07-01?

5. Why should a calendar table have no missing days?