Forum Discussion
Frefin
3 years agoNew Member
Help grouping rows by consecutive dates and category
Hello, I need help with a transformation in Power Query. I have the following table: Person ID Role Location Value Date 1 Project Director Location1 1.5 31/01/2023 1 Projec...
- 3 years ago
There may be more efficient methods but you can do a "double grouping"
- Group by "Person ID", "Role", "Location", "Value"
- For each sub-table
- Add a custom column that extracts only the Year and Month
- Create a new table that includes all YrMnths from start to finish
- Merge the tables
- nulls will show up where the original table is missing a month from the new table
- Sort the YrMnth column from the All Dates column (will be Column 1)
- Group by Person ID using "GroupKind.Local"
- Then delete the null rows
- Extract the start and end dates
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZHBCsIwDIZfRXoeNk1W9QE8KvM+diijyDys0u39sQ7EThSbXBpI8/Glf9tWGVWpSww338+b4xBTCTG1TqF38xDG57XZ2nSS0WA0ApLqKiZHQs5KONCwE/r2bw7/c/DtfeXcek+WL9uTMu7sRnf1vzHDx/CgAWU2EmApk1pmszJb9gN1ajV3H5epqSTEAuIjv0IH8Yh1aoUOy3a8suoe", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Person ID" = _t, Role = _t, Location = _t, Value = _t, Date = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{ {"Person ID", Int64.Type}, {"Role", type text}, {"Location", type text}, {"Value", type number}, {"Date", type date}}, "en-GB"), #"Grouped Rows" = Table.Group(#"Changed Type", {"Person ID", "Role", "Location", "Value"}, { {"Consecutives", (t)=> let #"YrMnth" = Table.AddColumn(t,"YrMnth", each Date.Year([Date]) * 1000 + Date.Month([Date]),Int64.Type), #"All Months" = Table.FromColumns({ List.Numbers(List.Min(#"YrMnth"[YrMnth]), List.Max(#"YrMnth"[YrMnth]) - List.Min(#"YrMnth"[YrMnth])+1)}), #"Merge" = Table.Join(#"YrMnth","YrMnth", #"All Months","Column1", JoinKind.RightOuter), #"Sort" = Table.Sort(#"Merge",{"Column1", Order.Ascending}), #"Remove" = Table.RemoveColumns(#"Sort", {"YrMnth","Column1"}), #"Group by Consecutive Dates" = Table.Group(#"Remove", {"Person ID"}, { {"Start Date", each List.Min([Date])}, {"End Date", each List.Max([Date])} }, GroupKind.Local), #"Remove Null Rows" = Table.SelectRows(#"Group by Consecutive Dates", each [Person ID] <> null) in #"Remove Null Rows"} }), #"Expanded Consecutives" = Table.ExpandTableColumn(#"Grouped Rows", "Consecutives", {"Start Date", "End Date"}), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Consecutives",{{"Start Date", type date}, {"End Date", type date}}) in #"Changed Type1"Results from your posted data
ronrsnfld
3 years agoSuper User
There may be more efficient methods but you can do a "double grouping"
- Group by "Person ID", "Role", "Location", "Value"
- For each sub-table
- Add a custom column that extracts only the Year and Month
- Create a new table that includes all YrMnths from start to finish
- Merge the tables
- nulls will show up where the original table is missing a month from the new table
- Sort the YrMnth column from the All Dates column (will be Column 1)
- Group by Person ID using "GroupKind.Local"
- Then delete the null rows
- Extract the start and end dates
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZHBCsIwDIZfRXoeNk1W9QE8KvM+diijyDys0u39sQ7EThSbXBpI8/Glf9tWGVWpSww338+b4xBTCTG1TqF38xDG57XZ2nSS0WA0ApLqKiZHQs5KONCwE/r2bw7/c/DtfeXcek+WL9uTMu7sRnf1vzHDx/CgAWU2EmApk1pmszJb9gN1ajV3H5epqSTEAuIjv0IH8Yh1aoUOy3a8suoe", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Person ID" = _t, Role = _t, Location = _t, Value = _t, Date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Person ID", Int64.Type}, {"Role", type text}, {"Location", type text},
{"Value", type number}, {"Date", type date}}, "en-GB"),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Person ID", "Role", "Location", "Value"}, {
{"Consecutives", (t)=>
let
#"YrMnth" = Table.AddColumn(t,"YrMnth", each Date.Year([Date]) * 1000 + Date.Month([Date]),Int64.Type),
#"All Months" =
Table.FromColumns({
List.Numbers(List.Min(#"YrMnth"[YrMnth]), List.Max(#"YrMnth"[YrMnth]) - List.Min(#"YrMnth"[YrMnth])+1)}),
#"Merge" = Table.Join(#"YrMnth","YrMnth", #"All Months","Column1", JoinKind.RightOuter),
#"Sort" = Table.Sort(#"Merge",{"Column1", Order.Ascending}),
#"Remove" = Table.RemoveColumns(#"Sort", {"YrMnth","Column1"}),
#"Group by Consecutive Dates" = Table.Group(#"Remove", {"Person ID"}, {
{"Start Date", each List.Min([Date])},
{"End Date", each List.Max([Date])}
}, GroupKind.Local),
#"Remove Null Rows" = Table.SelectRows(#"Group by Consecutive Dates", each [Person ID] <> null)
in
#"Remove Null Rows"}
}),
#"Expanded Consecutives" = Table.ExpandTableColumn(#"Grouped Rows", "Consecutives", {"Start Date", "End Date"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Consecutives",{{"Start Date", type date}, {"End Date", type date}})
in
#"Changed Type1"
Results from your posted data