Forum Discussion
Filter a table by grouping rows: aggregation plus calculations
- 5 years ago
Hello pescadicto
you have to add one more function to the group function, that takes the grouped table. Afterwards you can expand it again. Here a example
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMAxKGSrE60UpOQFZSmiGQNELiGwFJYzDfGchKBik3AXNdgKwUsHJTOD8ZrNxMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Feature1 = _t, Feature2 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Feature1", type text}, {"Feature2", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type number}, {"AllRows", each _, type table [ID=text, Feature1=text, Feature2=number]}}), #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Feature1", "Feature2"}, {"Feature1", "Feature2"}), #"Reordered Columns" = Table.ReorderColumns(#"Expanded AllRows",{"ID", "Feature1", "Feature2", "NewFeature"}) in #"Reordered Columns"Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy - 5 years ago
Hello pescadicto
check out this dynamic solution. Note, that in the expand-function I had to delete the "ID", otherwise this column would be double and the program throw and error
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMAxKGSrE60UpOQFZSmiGQNELiGwFJYzDfGchKBik3AXNdgKwUsHJTOD8ZrNxMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Feature1 = _t, Feature2 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Feature1", type text}, {"Feature2", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type number}, {"AllRows", each _, type table [ID=text, Feature1=text, Feature2=number]}}), GetColumnNamesOfTableToBeExpanded = List.Distinct(List.Combine(List.Transform(#"Grouped Rows"[AllRows], each Table.ColumnNames(_)))), #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows",List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"}), List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"})), #"Reordered Columns" = Table.ReorderColumns(#"Expanded AllRows",{"ID", "Feature1", "Feature2", "NewFeature"}) in #"Reordered Columns"Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Thank you! It worked. My real table has a lot of columns. It is posible to reformulate
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Feature1", "Feature2"}, {"Feature1", "Feature2"}),in such a way that the column names do not have to be made explicit?
Hello pescadicto
check out this dynamic solution. Note, that in the expand-function I had to delete the "ID", otherwise this column would be double and the program throw and error
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUUpMAxKGSrE60UpOQFZSmiGQNELiGwFJYzDfGchKBik3AXNdgKwUsHJTOD8ZrNxMKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Feature1 = _t, Feature2 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Feature1", type text}, {"Feature2", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"NewFeature", each List.Sum([Feature2]), type number}, {"AllRows", each _, type table [ID=text, Feature1=text, Feature2=number]}}),
GetColumnNamesOfTableToBeExpanded = List.Distinct(List.Combine(List.Transform(#"Grouped Rows"[AllRows], each Table.ColumnNames(_)))),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows",List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"}), List.Difference(GetColumnNamesOfTableToBeExpanded, {"ID"})),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded AllRows",{"ID", "Feature1", "Feature2", "NewFeature"})
in
#"Reordered Columns"
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
- pescadicto5 years agoHelper I
Great work! thanks Jimmy801!