Forum Discussion
johnfa
1 year agoFrequent 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 A 01/01/2024 31/01/2024 A 01/03/2024 31/...
Omid_Motamedise
Super User
1 year agojohnfa 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"johnfa
1 year agoFrequent Visitor
thanks Omid_Motamedise that works perfectly