How to calculate ISO year in Power Query

How to calculate ISO year in Power Query

ISO year in Power Query is necessary if you have multiple years and want to group data using ISO week number. Some of the dates at the end or beginning of the year might belong to another year.

Here is a list of dates that I will use in this example, and here is how to generate them in Power Query.

generate list of dates in Power Query

= List.Dates(#date(2021, 1, 1), 10, #duration(1, 0, 0, 0))

After date list generation, you need to transform it to a table, rename a column, and change the data type. You can find all M code at the end of this post.


ISO year in Power Query

To do this calculation in Power Query, you will need the ISO week number. Here is an example of ISO week number calculation in Power Query by using logic from Wikipedia.

ISO week numbering in Power Query

If you have ISO week number, then it is easy to calculate ISO year mathematically like this.

= Date.Year(Date.AddDays([Date], 26 - [ISOWeekNumber]))

ISO year in Power Query

I think it might be the easiest way how to do that calculation. It might be a useful addition to flexible calendar table that you can create with Power Query.

Here is the M code that you can use in Blank Query to recreate this example.

    Source = List.Dates(#date(2021, 1, 1), 10, #duration(1, 0, 0, 0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
    #"Added ISOWeekNumber" = Table.AddColumn(#"Changed Type", "ISOWeekNumber", each if
Number.RoundDown((Date.DayOfYear([Date])-(Date.DayOfWeek([Date], Day.Monday)+1)+10)/7)=0

Number.RoundDown((Date.DayOfYear(#date(Date.Year([Date])-1,12,31))-(Date.DayOfWeek(#date(Date.Year([Date])-1,12,31), Day.Monday)+1)+10)/7)

else if
(Number.RoundDown((Date.DayOfYear([Date])-(Date.DayOfWeek([Date], Day.Monday)+1)+10)/7)=53
and (Date.DayOfWeek(#date(Date.Year([Date]),12,31), Day.Monday)+1<4))


Number.RoundDown((Date.DayOfYear([Date])-(Date.DayOfWeek([Date], Day.Monday)+1)+10)/7)),
    #"Added ISOYear" = Table.AddColumn(#"Added ISOWeekNumber", "ISOYear", each Date.Year(Date.AddDays([Date], 26 - [ISOWeekNumber])))
    #"Added ISOYear"




Leave a Reply

Your email address will not be published. Required fields are marked *