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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
buyer66
Frequent Visitor

Highest frequencies of day of week in rolling 7 weeks for each ID (with link and examples)

https://docs.google.com/spreadsheets/d/1g-YB3VdZSiEJ57Q0ewrb7uZ3J6Wi5h0L5KxQkklKKNM/edit?usp=sharing... 

I have a table with 3 columns: ID, WeekID and DayofWeek
Would like to calculate the MaxFrequency column that shows the highest appearance of frequency of a day in rolling 7 weeks.
For example, ID 11 in WeekID 202320 has 2 tie frequencies: 1 and 4, each appears twice, then the value for MaxFrequency will be 2.
In WeekID 202318, rolling 7 weeks shows: 0-1-6-4-4-5-4, so 4 has 3 appearances, then MaxFrequency will be 3.

Don't know how to get it automated with Power Query?

Appreciate your help.

IDWeekIDDayofWeekMaxFrequency   
11202320121  
112023193233 
1120231803000
1120231713111
1120231663666
1120231543444
1120231443444
1120231352 55
1120231243  4
112023116    
112023103    
112023092    
112023084    
112023073    
112023064    
112023053    
112023040    
112023031    
112023026    
112023010    
222023200    
222023196    
222023180    
4 REPLIES 4
buyer66
Frequent Visitor

Thank you so much. It works perfectly.
However, just realized that I need to account for the ID that has no value in some WeekID. 
So in the example below, MaxFrequency for ID 1102269 will be counted = 1 for Week 202335 (because WeekID 202333, 202332, 202331, 202329 are missing) then only values 5,2,3 of Week 202335, 34, 30 are taken into account.

 

IDWeekIDDayofWeekMaxFrequency
111881520233614
111881520233524
111881520233415
111881520233315
111881520233234
111881520233124
111881520233014
111881520232913
111881520232812
111881520232711
111881520232631
110223320233543
110223320233433
110223320233333
110223320233233
110223320233153
110223320232952
110223320232751
110226920233552
110226920233422
110226920233032
110226920232842
110226920232741
110226920232651
110235920233545
110235920233456
110235920233356
110235920233255
110235920233154
110235920233033
110235920232953
110235920232852
110235920232751
110235920232621
buyer66
Frequent Visitor

Thanks for your kind help. The script runs well but rows are duplicated.

Could you please help to check? Thank you.

let
Source = GoogleSheets.Contents("https://docs.google.com/spreadsheets/d/1g-YB3VdZSiEJ57Q0ewrb7uZ3J6Wi5h0L5KxQkklKKNM/edit#gid=2033782..."),
Sheet1_Table = Source{[name="Sheet1",ItemKind="Table"]}[Data],
PromoteHeaders = Table.PromoteHeaders(Sheet1_Table, [PromoteAllScalars=true]),
ChangeType = Table.TransformColumnTypes(PromoteHeaders,{{"WeekID", Int64.Type}, {"DayofWeek", Int64.Type}, {"MaxFrequency", Int64.Type}, {"", Int64.Type}, {"_1", Int64.Type}, {"_2", Int64.Type}, {"ID", type text}}),
SelectColumns = Table.SelectColumns(ChangeType,{"ID", "WeekID", "DayofWeek"}),
AddColumn = Table.AddColumn(SelectColumns, "Custom", each Table.Combine(
Table.Group(SelectColumns, "ID",
{"n",each let a=[DayofWeek] in Table.AddColumn(_,"MaxFrequency", Function.ScalarVector(type function(r as any) as any, (m)=>List.Transform(List.Positions(a),(x)=>List.Max(List.Transform(List.Range(a,x,7), each List.Count(List.PositionOf(List.Range(a,x,7),_,2)))))))})[n] ) ),
Expand = Table.ExpandTableColumn(AddColumn, "Custom", {"MaxFrequency"}, {"MaxFrequency"}),
RemoveDuplicates = Table.Distinct(Expand)
in
RemoveDuplicates

let
Source = GoogleSheets.Contents("https://docs.google.com/spreadsheets/d/1g-YB3VdZSiEJ57Q0ewrb7uZ3J6Wi5h0L5KxQkklKKNM/edit#gid=2033782..."),
Sheet1_Table = Source{[name="Sheet1",ItemKind="Table"]}[Data],
PromoteHeaders = Table.PromoteHeaders(Sheet1_Table, [PromoteAllScalars=true]),
ChangeType = Table.TransformColumnTypes(PromoteHeaders,{{"WeekID", Int64.Type}, {"DayofWeek", Int64.Type}, {"MaxFrequency", Int64.Type}, {"", Int64.Type}, {"_1", Int64.Type}, {"_2", Int64.Type}, {"ID", type text}}),
SelectColumns = Table.SelectColumns(ChangeType,{"ID", "WeekID", "DayofWeek"}),
AddColumn = Table.Combine(
Table.Group(SelectColumns, "ID",
{"n",each let a=[DayofWeek] in Table.AddColumn(_,"MaxFrequency", Function.ScalarVector(type function(r as any) as any, (m)=>List.Transform(List.Positions(a),(x)=>List.Max(List.Transform(List.Range(a,x,7), each List.Count(List.PositionOf(List.Range(a,x,7),_,2)))))))})[n]),
in
AddColumn

wdx223_Daniel
Super User
Super User

= Table.Combine(Table.Group(Source,"ID",{"n",each let a=[DayofWeek] in Table.AddColumn(_,"MaxFrequency",Function.ScalarVector(type function(r as any) as any,(m)=>List.Transform(List.Positions(a),(x)=>List.Max(List.Transform(List.Range(a,x,7),each List.Count(List.PositionOf(List.Range(a,x,7),_,2)))))))})[n])

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.