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

 
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

Hi there,

It can be a bit tricky to compute an ISO Week and ISO year number in Power Query. Especially since there is no built-in function. You can find the consolidated code to do this on my blog (Create ISO Week and ISO Year in Power Query - BI Gorilla)

But step by step this means:

  • Step 1: Find the Current Thursday
  • Power Query:
    Date.AddDays([Date], 3 - Date.DayOfWeek( [Date], Day.Monday) )
  • Step 2: Create the Year of Current Thursday
  • Power Query:
    Date.Year( [CurrentThursday] )
  • Step 3: Find the First Thursday of Year
  • Power Query:
    Date.AddDays(
    #date( [YearCurrentThursday],1 ,7 ),
    - Date.DayOfWeek( #date( [YearCurrentThursday],1 , 1), Day.Friday )
    )
  • Step 4: Compute the ISO Week Number based on these values
  • Power Query:
    ( Duration.Days( [CurrentThursday] - [FirstThursdayOfYear] )
    / 7 )
    + 1

In the article you can also find a way to compute the ISO Year number.

Hope that helps!

Rick
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Great info. Here it is a a function which I named fxIsoWeek:
Power Query:
( IsoDate as date ) =>
let
  CurrentThursday =  Date.AddDays( IsoDate, 3 - Date.DayOfWeek( IsoDate, Day.Monday ) ),
  YearCurrThursday = Date.Year( CurrentThursday ),
  FirstThursdayOfYear = Date.AddDays( #date(YearCurrThursday, 1, 7) , - Date.DayOfWeek( #date( YearCurrThursday, 1, 1 ), Day.Friday ) ),
  ISO_Week = Duration.Days( CurrentThursday - FirstThursdayOfYear ) / 7 + 1
in
  ISO_Week
This can be used with Add Column -> Invoke Custom Function in the PQ Editor to add a column using a Date column. Confirmed using the step by step above.
 
Upvote 0
I use this formula:
if Date.DayOfWeek(Date.StartOfYear([Date]), Day.Monday) < 3 then Date.WeekOfYear([Date]) else Date.WeekOfYear([Date]) -1

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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