Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Date.WeekOfYear: A Step-by-Step How-To

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Date.WeekOfYear is a Power Query M function that returns the week number of a date, from 1 to 53, as a number. You call it as Date.WeekOfYear(#date(2026, 12, 5)) and it gives you the week that date falls in. It works in Power Query Editor inside Excel, Power BI Desktop, and other tools that host Power Query. The one thing to decide before you start is which numbering rule your business uses, because the function has a second argument that switches between two systems: a simple "first day of week" count, and an ISO-style week that can cross a year boundary.

What You'll Need

  • A table that already contains a date column, or a way to create dates from year, month and day columns. Dates stored as text need to be converted first.
  • Access to Power Query Editor (Data tab in Excel, or Transform data in Power BI Desktop).
  • A clear idea of whether your reporting weeks start on Sunday or Monday, and whether you need ISO week numbers.
  • Optional but useful: a calendar table you control, rather than calculating week numbers only on a fact table.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your table and choose Data > From Table/Range. In Power BI Desktop, choose Home > Transform data, then select the query on the left. Menu labels can differ slightly by version and by whether you are in Excel or Power BI, so look for the broadly equivalent option if the wording is not identical.
  2. Check the date column type. Click the small type icon at the left of the column header and confirm it says Date, or Date/Time. If the column is Text, change it to Date first. If Power Query prompts you to change the type in one step, accept it. Week numbers calculated on text will either error or give nonsense.
  3. Add a custom column. Go to Add Column > Custom Column. This opens a dialog with a name field and a formula box. Use Add Column rather than Transform, because Transform replaces the date with the week number and you usually want both.
  4. Enter the formula for the simple week system. Name the column something like "Week of Year" and type: Date.WeekOfYear([OrderDate]) Replace [OrderDate] with your actual column name. This returns a number where the week containing January 1 is week 1, based on a first day of week that defaults to Sunday. You can add a second argument to change the first day: Date.WeekOfYear([OrderDate], Day.Monday).
  5. Or use the ISO week system. If your organisation reports on ISO weeks, add a third argument set to the rule that starts ISO numbering: Date.WeekOfYear([OrderDate], Day.Monday, WeekRule.FirstFourDayWeek) This makes a week belong to the year that contains at least four of its days, which is the standard ISO 8601 behaviour. The same call returns a valid ISO week number, but note that it still returns the week number, not the ISO week-year.
  6. Click OK and review the result. The new column appears with a number in each row. Look at a few known dates to confirm the numbers match what you expect. A date in early January is the best test, because that is where the two systems disagree most.
  7. Handle blank or null dates. If the date column contains nulls, the function returns null, which is fine, but the result column type may become any. Set the new column's type to Whole Number so sorting and filtering behave correctly.
  8. Watch the year boundary. If you group by week number across years, weeks from different years will merge. Add a year column too, using Add Column > Date > Year, and group by year and week together. For ISO weeks, the plain year column can be off by a few days at the edges, so pair the week with a year derived the same way you derive the week.
  9. Rename and close. Right-click the new column header to rename it, then choose Home > Close & Load (Excel) or Home > Close & Apply (Power BI). The week number is now available to your pivot table, chart or model.

Common Mistakes

Assuming Sunday start matches ISO. The default first day of week is not Monday, so gives different results from an ISO week number. If your finance team reports ISO weeks, you must pass Day.Monday and WeekRule.FirstFourDayWeek . Compare a couple of early-January dates in both versions and the difference becomes obvious.

Grouping by week number alone. Week 1 of one year and week 1 of the next are not the same group. Without a year column, a pivot that groups by week number will merge unrelated periods and produce a chart that looks like it has a single spiky January.

Leaving the date as text. A date stored as text like "05/12/2026" may be interpreted with the wrong day and month order depending on locale, or fail to convert. Always confirm the column type before calculating the week.

Using Transform instead of Add Column. Transform replaces the date values, and you lose the ability to drill from week down to a specific day. It is easy to do by accident because both menus look similar.

Forgetting that week numbers are numbers, not labels. Week 5 sorts numerically as 5, not as "05". If you need zero-padded labels for display, build them with Number.ToText after the fact.

Tips

Build the week number in a dedicated calendar table rather than in each fact table. One calendar table means one definition of a week, and every report stays consistent. Load dates from the earliest to the latest you need, mark it as a date table if your tool supports that, and add year, quarter, month and week columns there.

Keep a comment or a small note on the column so a colleague knows which system you used. The formula itself records it, but a renamed column like "ISO Week" or "Week (Sun start)" saves someone opening the formula six months later.

Use the built-in Date column menu for the simple cases. Add Column > Date > Week of Year provides a one-click version of the default behaviour. It is convenient, but it does not expose the ISO options, so reach for a custom column when you need control.

Test with a fixed set of dates rather than trusting a single value. Put 1 January, 2 January, 31 December and a mid-year date into a small query and check all four. It takes a minute and catches almost every configuration error.

Troubleshooting

All values come out as errors. The date column is almost certainly text, or contains text entries mixed with real dates. Filter or replace the offending rows, then set the column type to Date and refresh the custom column.

The week numbers do not match Excel's WEEKNUM formula. Excel's WEEKNUM has its own return-type argument and a separate ISO function, so it can disagree with Power Query. Decide which source governs your reporting and apply that rule consistently everywhere, rather than mixing the two in one workbook.

The result column says "Error" on one row only. Open the error and read the message. A single bad date, such as 30 February or a blank string, is the usual cause. Fix the source row or filter it out.

Numbers seem off by one near January. This is nearly always a first-day-of-week or week-rule mismatch, not a bug. Recheck the second and third arguments of your formula against the convention your business actually uses.

Nothing updates after you edit the step. Select the step in the Applied Steps pane, confirm the formula is what you think it is, then refresh the preview. If the whole query fails, remove the custom column step and re-add it, checking the column names as you type.

Frequently Asked Questions

What does Date.WeekOfYear return exactly?

A whole number from 1 to 53 representing the week of the year that the supplied date falls in. It returns null when the input is null.

How do I get ISO week numbers?

Pass Day.Monday as the first-day argument and WeekRule.FirstFourDayWeek as the rule argument, so weeks follow ISO 8601 numbering.

Why does week number 1 sometimes not include 1 January?

Under ISO rules, 1 January can belong to the final week of the previous year if it falls in a week that has fewer than four days in the new year. That is correct behaviour, and it is why you should pair week numbers with a year.

Can I use it on a Date/Time column?

Yes. The function works on dates and datetimes. If you only want the calendar week, the time portion is ignored.

Does changing the first day of week change the count?

It can. It shifts which days sit in which week, so week numbers near year boundaries and around the cutoff can move by one.