Date.WeekOfYear Function (Power Query M)

If you want to find out which week of the year a date falls in, the Date.WeekOfYear function gives you that number. In this article, I’ll show you how to use it, including how to set which day starts the week.

Syntax of Date.WeekOfYear Function

Date.WeekOfYear(dateTime as any, optional firstDayOfWeek as nullable number) as nullable number

where

  • dateTime (required, any). The date value you want the week number for.
  • firstDayOfWeek (optional, nullable number). A Day.Type value such as Day.Sunday or Day.Monday that sets which day starts the week. Leave it out and Power Query uses a culture-dependent default.

Returns: a number from 1 to 54 showing which week of the year the date falls in. Returns null if dateTime is null.

In plain terms, you hand it a date and it tells you the week number for that date within its year.

Example 1: Get the week number for a date

Find out which week of the year January 15, 2024 falls in.

Date.WeekOfYear(#date(2024,1,15))

Result: 3

With no firstDayOfWeek set, Power Query falls back to its default, and that date lands in week 3.

Example 2: Set Monday as the start of the week

The week number can change depending on which day you treat as the start of the week.

Pass Day.Monday as the second argument to count weeks from Monday.

Date.WeekOfYear(#date(2024,1,7),Day.Monday)

Result: 1

January 7, 2024 was a Sunday, so with Monday-start weeks it still belongs to week 1.

Example 3: Add a week-number column to a table

A common job is tagging each row with its week number so you can group or filter by week later on.

This works the same way as adding a weekday-number column, just with the week of the year instead.

Say you have a Tickets query with a TicketID and an OpenedDate column.

Here is the starting data:

TicketIDOpenedDate
T-20412024-02-06
T-20422024-03-21
T-20432024-05-09
T-20442024-08-15

Set the column to the date type, then add a Week column with Date.WeekOfYear:

let
Source = Excel.CurrentWorkbook(){[Name="Tickets"]}[Content],
Typed = Table.TransformColumnTypes(Source,{{"OpenedDate",type date}}),
AddWeek = Table.AddColumn(Typed,"Week",each Date.WeekOfYear([OpenedDate],Day.Monday),Int64.Type),
Result = Table.SelectColumns(AddWeek,{"TicketID","Week"})
in
Result

Each ticket now carries the week its date falls in, counting from Monday.

The result keeps:

TicketIDWeek
T-20416
T-204212
T-204319
T-204433

The Int64.Type on Table.AddColumn types the new column as a whole number straight away, so you do not have to convert it later.

Example 4: Watch the end-of-year boundary

Late-December dates can land in a high week number even when they are close to the next year.

Check December 29, 2024 with Monday as the week start.

Date.WeekOfYear(#date(2024,12,29),Day.Monday)

Result: 52

The function counts weeks within the same calendar year, so this date stays in week 52 of 2024 rather than rolling into the next year. If you need the last day of that period instead, Date.EndOfMonth handles month boundaries.

Things to keep in mind with Date.WeekOfYear

  • The week number is not the ISO week number. Date.WeekOfYear counts simple calendar weeks from the start of the year, so it can run up to 54 and does not follow the ISO 8601 rules. For ISO weeks you need a custom calculation.
  • The default firstDayOfWeek depends on the locale. Omit the argument and the same date can return a different week number on a machine with a different culture. Pass Day.Monday or Day.Sunday explicitly when you need a stable result.
  • It expects a date or datetime, not text. A text value throws Expression.Error: We cannot convert a value of type Text to type Date. Set the column type first, as in Example 3, or convert the text to a date before passing it in.
  • A null date returns null, not an error. When you add the column over a table with blank dates, those rows get null in the Week column rather than failing the refresh.

Common questions about Date.WeekOfYear

How is Date.WeekOfYear different from Date.WeekOfMonth?

Date.WeekOfYear returns the week position within the whole year, while Date.WeekOfMonth returns the week position within that date’s month.

Can I get an ISO 8601 week number from it?

Not directly. Date.WeekOfYear uses simple calendar weeks, so an ISO week number needs its own formula rather than this function.

List of All Power Query Functions

Related Power Query Functions / Articles:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.