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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
johnfa
Frequent Visitor

Grouping by Consecutive Dates and Category

Hi Power Query gurus - does anyone know if it is possible to get from a table that looks like this

Category Start Date     End Date     
A01/01/202431/01/2024
A01/03/202431/03/2024
A01/04/202428/04/2024
A29/04/202401/06/2024
A01/08/2024 
B01/01/202431/01/2024
B01/06/202421/06/2024


to a table that looks like this

Category Start Date     End Date     Grouping
A01/01/202431/01/20241
A01/03/202431/03/20242
A01/04/202428/04/20242
A29/04/202401/06/20242
A01/08/2024 3
B01/01/202431/01/20241
B01/06/202421/06/20242


I am looking to have a grouping field which groups by each Category and related rows where the start and end dates are continuous  ie start date is 1 day after the previous end date ?  If the next start date  is more than one day after the previous end date then it should be a new Grouping

Any help greatly appreciated !
thanks


1 ACCEPTED SOLUTION

Amazing huge thanks @ThxAlot they work perfectly  - though cant pretend to understand exactly what they are doing. 

View solution in original post

5 REPLIES 5
Omid_Motamedise
Super User
Super User

@johnfa it was an interesting question, 
you can solve it by copy the below code and past it into the advaance editor.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIw1AciIwMjEyDHGMGJ1UHIGyPLG2PKm8DkjSzgHJi8kSWSPEixGaZ+C5i8AljYiYCznFBNAtqBZGwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Start Date", type date}, {"End Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Category"}, {{"Count", each  [a=Table.ToRows(_[[Start Date],[End Date]]),b=List.Accumulate(List.Skip(List.Positions(a)),{1},(x,y)=> if a{y}{0}=a{y-1}{1}+Duration.From(1) then x&{List.Last(x)} else x &{ List.Last(x)+1}), c=Table.AddIndexColumn(_,"in",0,1),d=Table.AddColumn(c,"Grouping",(m)=> b{m[in]})  ][d]}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Category", "Start Date", "End Date", "in", "Grouping"}, {"Category.1", "Start Date", "End Date", "in", "Grouping"})
in
    #"Expanded Count"
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

thanks @Omid_Motamedise  that works perfectly

ThxAlot
Super User
Super User

 

Solution 1, concise but requires enough proficiency in PQ,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIw1AciIwMjEyDHGMGJ1UHIGyPLG2PKm8DkjSzgHJi8kSWSPEixGaZ+C5g8WNSJgKucUA0CWoFkaiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Start Date", type date}, {"End Date", type date}}, "fr"),
    Rows = Table.ToRecords(#"Changed Type"),
    #"Grouped inner" = List.Transform(List.Accumulate(Rows, {}, (s,c) => if (List.Last(List.Last(s,{}))[Category]? = c[Category]) and (List.Last(List.Last(s))[End Date] + #duration(1,0,0,0) = c[Start Date]) then List.RemoveLastN(s) & {List.Last(s) & {c}} else s & {{c}}), Table.FromRecords),
    #"Extracted Category" = List.Transform(#"Grouped inner", each List.Distinct([Category]){0}),
    #"Grouped outer" = Table.Group(Table.FromColumns({#"Extracted Category", #"Grouped inner"}, {"Category", "grp"}), "Category", {"grp", each Table.AddIndexColumn(_, "Category Index", 1, 1, Int64.Type)}),
    #"Expanded outer" = Table.ExpandTableColumn(#"Grouped outer", "grp", {"grp", "Category Index"}),
    #"Expanded inner" = Table.ExpandTableColumn(#"Expanded outer", "grp", {"Start Date", "End Date"})
in
    #"Expanded inner"

 

ThxAlot_0-1731789794354.png

 

Solution 2, a bit less demanding,

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIw1AciIwMjEyDHGMGJ1UHIGyPLG2PKm8DkjSzgHJi8kSWSPEixGaZ+C5g8WNSJgKucUA0CWoFkaiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Start Date", type date}, {"End Date", type date}}, "fr"),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Previous End Date" = let end = #"Added Index"[End Date] in Table.AddColumn(#"Added Index", "Previous End Date", each try end{[Index]-1} otherwise null),
    #"Grouped inner" = Table.Group(#"Added Previous End Date", {"Category","Start Date","Previous End Date"}, {"grp", each _}, 0, (x,y) => Byte.From(x[Category]<>y[Category] or (y[Start Date]<>y[Previous End Date] + #duration(1,0,0,0)))),
    #"Grouped outer" = Table.Group(Table.RemoveColumns(#"Grouped inner",{"Start Date", "Previous End Date"}), "Category", {"grp", each Table.AddIndexColumn(_, "Category Index", 1, 1, Int64.Type)}),
    #"Expanded outer" = Table.ExpandTableColumn(#"Grouped outer", "grp", {"grp", "Category Index"}),
    #"Expanded inner" = Table.ExpandTableColumn(#"Expanded outer", "grp", {"Start Date", "End Date"})
in
    #"Expanded inner"

 



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



Amazing huge thanks @ThxAlot they work perfectly  - though cant pretend to understand exactly what they are doing. 

Anonymous
Not applicable

Try adding the final parameter to your group step, GroupKind.Local. 

--Nate

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors