Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
imranamikhan
Helper V
Helper V

Power query week of month sequence

Hi all,

 

I have created a week of month column by simply using the Date.WeekOfMonth function to return the week number per date.

 

However, where a week crosses months, I want the week number to continue its sequence and then reset the following Monday (my start day of the week).

 

Example below with the result I am trying to achieve with Power Query:

 

example.png

 

2 ACCEPTED SOLUTIONS
lbendlin
Super User
Super User

This data is immutable. Don't waste your time trying to do this in Power Query (It may not even be possible since you would have to look back into prior months' week numbering, recursively).  Use an external Calendar reference table that has this precomputed.

 

For reference here's a function that tries and fails miserably.

 

let
    Source = List.Dates(#date(2024,1,1),71,#duration(1,0,0,0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Date.WeekOfMonth", each Date.WeekOfMonth([Column1],Day.Monday)),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "WeekDayFirstOfMonth", each Date.DayOfWeek(Date.StartOfMonth([Column1]),Day.Monday)),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "AdjustedWeekOfMonth", each if [WeekDayFirstOfMonth]=0 then [Date.WeekOfMonth] else if Date.Day([Column1])<=(7-[WeekDayFirstOfMonth]) then 5 else [Date.WeekOfMonth]-1)
in
    #"Added Custom2"

View solution in original post

imranamikhan
Helper V
Helper V

@lbendlin - I went back to the drawing board and started thinking about how I would do this in Excel. I ended up with the following Excel formula:

 

=QUOTIENT(DAY([@Date]-WEEKDAY([@Date],3))-1,7)+1

 

The above achieved the result I needed in the spreadsheet. I then translated the above with equivalent functions in Power Query using:

 

let 

getWkDay = Date.DayOfWeek([Date], Day.Monday), 

getMonday = Date.AddDays(Date.From([Date]), - getWkDay),

prvMondayMinusOne = Date.Day(getMonday) - 1,

result = Number.IntegerDivide(PrvMondayMinusOne, 7) + 1 //count number of complete weeks up to this date

in result

 

 

Hopefully this will help anyone else trying to achieve a similar result.

View solution in original post

4 REPLIES 4
imranamikhan
Helper V
Helper V

@lbendlin - I went back to the drawing board and started thinking about how I would do this in Excel. I ended up with the following Excel formula:

 

=QUOTIENT(DAY([@Date]-WEEKDAY([@Date],3))-1,7)+1

 

The above achieved the result I needed in the spreadsheet. I then translated the above with equivalent functions in Power Query using:

 

let 

getWkDay = Date.DayOfWeek([Date], Day.Monday), 

getMonday = Date.AddDays(Date.From([Date]), - getWkDay),

prvMondayMinusOne = Date.Day(getMonday) - 1,

result = Number.IntegerDivide(PrvMondayMinusOne, 7) + 1 //count number of complete weeks up to this date

in result

 

 

Hopefully this will help anyone else trying to achieve a similar result.

imranamikhan
Helper V
Helper V

Thanks for the advice @lbendlin.

 

I have a date reference table I am using and I am attempting to add this type of sequencing for week numbers as a new column into the date reference table (but without success).

 

Annoyingly I cannot locate a calendar reference table online with this type of sequencing for week numbers.

 

Knowing you think this is a futile exercise, I will instead populate this column manually with the sequencing I need.

Yes, I truly believe this is a futile exercise.  Were I to do it I would for a given date find the latest prior month that starts on a monday,  and then would use List.Accumulate to conditionally count the weeks and days until the current day.  What a glorious waste of time 🙂

 

 

let
  Source = List.Dates(#date(2024, 1, 1), 71, #duration(1, 0, 0, 0)), 
  #"Converted to Table" = Table.FromList(
    Source, 
    Splitter.SplitByNothing(), 
    {"Date"}, 
    null, 
    ExtraValues.Error
  ), 
  #"Added Custom" = Table.AddColumn(
    #"Converted to Table", 
    "WeekOfMonth", 
    each List.Accumulate(
      {Int64.From(#date(2024, 1, 1)) .. Int64.From([Date])}, 
      0, 
      (state, current) =>
        if Date.DayOfWeek(Date.From(current), Day.Monday) > 0 then
          state
        else if Date.Day(Date.From(current)) < 7 then
          1
        else
          state + 1
    )
  )
in
  #"Added Custom"

 

lbendlin
Super User
Super User

This data is immutable. Don't waste your time trying to do this in Power Query (It may not even be possible since you would have to look back into prior months' week numbering, recursively).  Use an external Calendar reference table that has this precomputed.

 

For reference here's a function that tries and fails miserably.

 

let
    Source = List.Dates(#date(2024,1,1),71,#duration(1,0,0,0)),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Date.WeekOfMonth", each Date.WeekOfMonth([Column1],Day.Monday)),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "WeekDayFirstOfMonth", each Date.DayOfWeek(Date.StartOfMonth([Column1]),Day.Monday)),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "AdjustedWeekOfMonth", each if [WeekDayFirstOfMonth]=0 then [Date.WeekOfMonth] else if Date.Day([Column1])<=(7-[WeekDayFirstOfMonth]) then 5 else [Date.WeekOfMonth]-1)
in
    #"Added Custom2"

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.