Excel WEEKNUM function for Power Query

If you are looking for the Excel equivalent of the WEEKNUM function for Power Query this article is for you.

As this is also a function that has many regional options, I was lucky to find an algorithm that I could use for its main part here: M functions to convert between ISO 8601 Week & Year ⇄ dates (e.g., 2014-12-29"2015-W01-1") (github.com)

Excel WEEKNUM function for Power Query

https://gist.github.com/ImkeF/29797a686e57aebb742c7a506df632f1

Function parameters

The function takes the same parameters as its Excel equivalent. So this is just a copy for your convenient reference:

WEEKNUM(serial_number,[return_type])

The WEEKNUM function syntax has the following arguments:

  • Serial_number     Required. A date within the week. Dates should be entered by using the DATE function, or as results of other formulas or functions. For example, use DATE(2008,5,23) for the 23rd day of May, 2008. Problems can occur if dates are entered as text.
  • Return_type     Optional. A number that determines on which day the week begins. The default is 1.
Return_type Week begins on System
1 or omitted Sunday 1
2 Monday 1
11 Monday 1
12 Tuesday 1
13 Wednesday 1
14 Thursday 1
15 Friday 1
16 Saturday 1
17 Sunday 1
21 Monday 2

If you are interested in more Power Query functions that replicate Excel functions who haven’t made it into the M-language (yet?) please check out this collection.

Enjoy and stay queryious 😉

2 thoughts on “Excel WEEKNUM function for Power Query”

  1. Hello
    I want to share with you my own ISO week number function

    (d as date) =>
    let
    Week_Thursday = Date.AddDays(d,3-Date.DayOfWeek(d,Day.Monday)),

    Week_Year =Date.Year(Week_Thursday),

    Year_First_Thursday = Date.AddDays(#date(Week_Year,1,7),-Date.DayOfWeek(#date(Week_Year,1,1),Day.Friday)),

    Week_Num = Duration.Days(Week_Thursday - Year_First_Thursday)/7+1

    in

    Week_Num
    //For 2022-W04 : Text.From(Week_Year)&”-W”&Text.PadStart(Text.From(Week_Num),2,”0″)

    Reply

Leave a comment