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/...
ThxAlot
Super User
1 year ago
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"
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"