Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
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.
| ID | WeekID | DayofWeek | MaxFrequency | |||
| 11 | 202320 | 1 | 2 | 1 | ||
| 11 | 202319 | 3 | 2 | 3 | 3 | |
| 11 | 202318 | 0 | 3 | 0 | 0 | 0 |
| 11 | 202317 | 1 | 3 | 1 | 1 | 1 |
| 11 | 202316 | 6 | 3 | 6 | 6 | 6 |
| 11 | 202315 | 4 | 3 | 4 | 4 | 4 |
| 11 | 202314 | 4 | 3 | 4 | 4 | 4 |
| 11 | 202313 | 5 | 2 | 5 | 5 | |
| 11 | 202312 | 4 | 3 | 4 | ||
| 11 | 202311 | 6 | ||||
| 11 | 202310 | 3 | ||||
| 11 | 202309 | 2 | ||||
| 11 | 202308 | 4 | ||||
| 11 | 202307 | 3 | ||||
| 11 | 202306 | 4 | ||||
| 11 | 202305 | 3 | ||||
| 11 | 202304 | 0 | ||||
| 11 | 202303 | 1 | ||||
| 11 | 202302 | 6 | ||||
| 11 | 202301 | 0 | ||||
| 22 | 202320 | 0 | ||||
| 22 | 202319 | 6 | ||||
| 22 | 202318 | 0 |
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.
| ID | WeekID | DayofWeek | MaxFrequency |
| 1118815 | 202336 | 1 | 4 |
| 1118815 | 202335 | 2 | 4 |
| 1118815 | 202334 | 1 | 5 |
| 1118815 | 202333 | 1 | 5 |
| 1118815 | 202332 | 3 | 4 |
| 1118815 | 202331 | 2 | 4 |
| 1118815 | 202330 | 1 | 4 |
| 1118815 | 202329 | 1 | 3 |
| 1118815 | 202328 | 1 | 2 |
| 1118815 | 202327 | 1 | 1 |
| 1118815 | 202326 | 3 | 1 |
| 1102233 | 202335 | 4 | 3 |
| 1102233 | 202334 | 3 | 3 |
| 1102233 | 202333 | 3 | 3 |
| 1102233 | 202332 | 3 | 3 |
| 1102233 | 202331 | 5 | 3 |
| 1102233 | 202329 | 5 | 2 |
| 1102233 | 202327 | 5 | 1 |
| 1102269 | 202335 | 5 | 2 |
| 1102269 | 202334 | 2 | 2 |
| 1102269 | 202330 | 3 | 2 |
| 1102269 | 202328 | 4 | 2 |
| 1102269 | 202327 | 4 | 1 |
| 1102269 | 202326 | 5 | 1 |
| 1102359 | 202335 | 4 | 5 |
| 1102359 | 202334 | 5 | 6 |
| 1102359 | 202333 | 5 | 6 |
| 1102359 | 202332 | 5 | 5 |
| 1102359 | 202331 | 5 | 4 |
| 1102359 | 202330 | 3 | 3 |
| 1102359 | 202329 | 5 | 3 |
| 1102359 | 202328 | 5 | 2 |
| 1102359 | 202327 | 5 | 1 |
| 1102359 | 202326 | 2 | 1 |
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
= 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])
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
| User | Count |
|---|---|
| 17 | |
| 9 | |
| 8 | |
| 7 | |
| 6 |