Smart Systems for Home

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

Power Query ISO Week Number: Step-by-Step Guide

· 5 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

To get the ISO week number in Power Query, use the formula — but that alone does not fully implement ISO 8601. The ISO standard also requires that week 1 is the week containing the first Thursday of the year, and that weeks start on Monday. The reliable approach is to add a custom column with a formula that checks the weekday of January 1 and adjusts the week number accordingly. This guide walks through the exact steps, the formula to paste, and the common pitfalls.

What You'll Need

  • Power BI Desktop, Excel with Power Query (Get & Transform), or Analysis Services with Power Query — the steps are the same across all three; labels may differ slightly by version or region.
  • A table with a column of dates that Power Query recognises as Date type (not Text).
  • Basic familiarity with the Power Query Editor: adding a custom column and writing a short M formula.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform Data to open the Power Query Editor. Confirm your date column is typed as Date (the icon in the column header shows a calendar, not ABC). If it is Text, right-click the header and choose Change Type > Date.
  2. Add a custom column. On the Add Column tab, click Custom Column. Name it something like ISO Week.
  3. Paste the ISO week formula. In the formula box, enter the following M expression (replace with your actual column name): let jan1 = #date(Date.Year(d), 1, 1), jan1Weekday = Date.DayOfWeek(jan1, Day.Monday), offset = if jan1Weekday <= 3 then jan1Weekday else jan1Weekday - 7, weekNum = Number.RoundDown((Date.DayOfYear(d) - offset - 1) / 7) + 1, adjusted = if weekNum < 1 then Date.WeekOfYear(#date(Date.Year(d) - 1, 12, 31), Day.Monday) else if weekNum > 52 and Date.WeekOfYear(#date(Date.Year(d), 12, 31), Day.Monday) < weekNum then 1 else weekNum in adjusted This formula calculates the ISO week by finding the Monday-based weekday of January 1, shifting the day-of-year count, and handling the year-boundary cases where a date belongs to week 52 or 53 of the previous year, or week 1 of the next year.
  4. Click OK. Power Query adds the new column. Check a few rows: for example, 2024-01-01 should return 1, 2023-01-01 should return 52 (because 2023-01-01 was a Sunday and belonged to ISO week 52 of 2022), and 2021-01-01 should return 53.
  5. Change the column type to Whole Number. Right-click the new column header and choose Change Type > Whole Number so it sorts and aggregates correctly.
  6. Rename and close. Rename the column if needed, then click Close & Load (Excel) or Close & Apply (Power BI) to load the result.

Common Mistakes

  • Using Date.WeekOfYear without the Monday argument. The default start day is Sunday, which does not match ISO. Always pass Day.Monday.
  • Assuming Date.WeekOfYear with Monday equals ISO. It does not handle the first-Thursday rule. A date like 2021-01-01 will return 1 instead of 53. Use the full formula above.
  • Forgetting year-boundary cases. Dates in late December can belong to week 1 of the next year, and early January dates can belong to week 52 or 53 of the previous year. The formula's adjusted step handles this; a simple week-number formula will not.
  • Leaving the column as Text. If the custom column returns text (for example because of a type mismatch), sorting will be alphabetical (1, 10, 11, 2) rather than numeric.
  • Using a locale-dependent week function. Some cultures use different week rules. The formula above is locale-independent because it works from the date components directly.

Tips

  • Test the formula against a known ISO week calendar for a few years, especially years that start on Thursday, Friday, Saturday, or Sunday, where the boundary cases occur.
  • If you need the ISO week-year (the year that the ISO week belongs to, which can differ from the calendar year), add a second custom column using the same logic but returning Date.Year(d) adjusted by the same boundary rules.
  • For performance on large tables, the formula is lightweight — it uses only date arithmetic and no row context lookups — so it scales well.
  • Store the formula in a Power Query function if you reuse it across queries: create a blank query, paste the expression as a function with a date parameter, and invoke it from each table.

Troubleshooting

  • The custom column shows an error. Check that your date column is truly Date type. A Text column will cause Date.Year to fail. Also confirm the column name in the formula matches exactly, including spaces and brackets.
  • Week numbers look off by one for early January. This is the year-boundary case. Verify the formula includes the adjusted step. If you removed it, re-add it.
  • Week 53 appears when you expected 1. That is correct for dates that ISO assigns to week 53 of the previous year. Cross-check with an ISO week calendar for that specific date.
  • The column will not change to Whole Number. The formula may be returning null for some rows (for example blank dates). Filter out nulls or wrap the formula in try ... otherwise null.
  • Excel shows a different week number than Power BI. Excel's WEEKNUM function defaults to a Sunday start and system 1; it is not ISO. Use the Power Query result as the authoritative ISO value.

Frequently Asked Questions

Does Power Query have a built-in ISO week function?

No. Date.WeekOfYear supports a start day but not the full ISO 8601 first-Thursday rule, so a custom formula is required.

Why does 2021-01-01 return week 53?

ISO 8601 assigns that date to week 53 of 2020 because the week containing January 1, 2021, had its Thursday in December 2020. The formula handles this via the year-boundary adjustment.

Can I use this formula in Excel Power Query and Power BI?

Yes. The M language is the same in both. The only difference is the final button label: Close & Load in Excel versus Close & Apply in Power BI.

How do I get the ISO week-year as well?

Add a second custom column that returns the year of the Thursday in the same week. A simple way is to compute and take its year.

What if my date column contains nulls?

Wrap the formula in try ... otherwise null so null dates return null instead of an error, then filter or handle the nulls downstream.

Does the formula work for dates before 1900?

Power Query's date functions support a wide range, but ISO week rules are defined for the Gregorian calendar. For historical dates, verify against an ISO calendar for that period.