Calculate ISO Week from Date in Power Query

Skip2MiLu

New Member
Joined
Aug 11, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Does anyone know how to calculate ISO Week from a date in Power Query

It seems that the following function = Date.WeekOfYear([Column1],Day.Monday) - Is not calculating the same ISO Week expression as the equivalent WEEKNUM (Date,21) or the ISOWEEKNUM function in excel.

See below

 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I’m not saying it can’t be done, but it’s pretty hard. I have always resorted to using Excel. On of the problems with ISO week, and 445 calendars, is there are sooo many variations. I’ve never been motivated to find a solution for this reason.
 
Upvote 0
I’m not saying it can’t be done, but it’s pretty hard. I have always resorted to using Excel. On of the problems with ISO week, and 445 calendars, is there are sooo many variations. I’ve never been motivated to find a solution for this reason.
That is really pity. I was hoping someone has figured out a work around expression that I could make use of. At this point I do not mind if not 100% accurate. It is unusual the Microsoft does not have this expression option.
I suppose your suggestion would be to use an excel look up table?
 
Upvote 0
The short answer is, if you can write a formula in Excel that always works, you can do it in Power Query. I'm interested in doing it if you want to write the Excel version first. The rules are, you must have a single Excel formula (or multiple formulas) that can point to any date and give the ISO week number. The Excel formula is not allowed to change to cater for different years. If you can write that, it is more than half way solved.
 
Upvote 0
Here's a custom M function I named Date_ISOWeekNum. If you want to call it another name be sure to change the recursive call in the last line to match the function name you choose.

Power Query:
(dt as any) as number =>
let 
    Wk1 = (yr as number) as date => let d = #date(yr,1,1) in Date.AddDays(d, {1,0,-1,-2,-3,3,2}{Date.DayOfWeek(d,Day.Sunday)}),
    df = Date.From(dt),
    yr = Date.Year(df),
    wk1yr = Wk1(yr)
in 
    if df >= wk1yr and df < Wk1(yr+1) then Number.RoundDown((Duration.Days(df - wk1yr)/7)) +1 else 
                if df < wk1yr then Date_ISOWeekNum(#date(yr-1,12,31)) else 1
 
Upvote 0
Here's a custom M function I named Date_ISOWeekNum. If you want to call it another name be sure to change the recursive call in the last line to match the function name you choose.

Power Query:
(dt as any) as number =>
let
    Wk1 = (yr as number) as date => let d = #date(yr,1,1) in Date.AddDays(d, {1,0,-1,-2,-3,3,2}{Date.DayOfWeek(d,Day.Sunday)}),
    df = Date.From(dt),
    yr = Date.Year(df),
    wk1yr = Wk1(yr)
in
    if df >= wk1yr and df < Wk1(yr+1) then Number.RoundDown((Duration.Days(df - wk1yr)/7)) +1 else
                if df < wk1yr then Date_ISOWeekNum(#date(yr-1,12,31)) else 1
Thanks. With my limited power query experience....How do go about using this function to replace my current Customer Column function that does not calculate ISO week from my [#"Requested#(lf)Enddate"] date column?

1657574776878.png
 
Upvote 0
Open a blank query, go to advanced editor and replace the code there with the code for Date_ISOWeekNum. Name that query Date_ISOWeekNum.

Then change your custom column formula to :

= Date_ISOWeekNum([#"Requested#(lf)Enddate"])
 
Upvote 0
Open a blank query, go to advanced editor and replace the code there with the code for Date_ISOWeekNum. Name that query Date_ISOWeekNum.

Then change your custom column formula to :

= Date_ISOWeekNum([#"Requested#(lf)Enddate"])
@JGordon11 I followed those instructions it worked perfect.
However, I have noticed thta the Week Number increases on a Monday, how would I go about in changing it to start on a Sunday instead.

Edit
Figured it out
Power Query:
(dt as any) as number =>
let 
    Wk1 = (yr as number) as date => let d = #date(yr,1,1) in Date.AddDays(d, {0,-1,-2,-3,3,2,1}{Date.DayOfWeek(d,Day.Sunday)}),
    df = Date.From(dt),
    yr = Date.Year(df),
    wk1yr = Wk1(yr)
in 
    if df >= wk1yr and df < Wk1(yr+1) then Number.RoundDown((Duration.Days(df - wk1yr)/7)) +1 else 
                if df < wk1yr then Date_ISOWeekNum(#date(yr-1,12,31)) else 1
For anyone who needs it starting Sunday
The Date.AddDays(d, {0,-1,-2,-3,3,2,1} part
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top