Forum Discussion
Power query: Group by Date Column and keep Max for all other columns - Dynamic approach
- 4 years ago
You can create a dynamic List of functions, based on the column names, for both setting the data types, and also performing the aggregations in the Grouping.
In the aggregation transformation, C represents the column name; the underscore ( _ ) represents the subtable returned in the Table.Group function
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY7RDQAhCEN34dtExTv1ZjHuv4ZQMMdHTekjxbWoZs5cKRH0yPOa2UmhDqxBuyvDSZcBIV9TAtEi1A7RFwDa1UzIgC51v9Tg/9wK/BcRTIcFrXsf", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]), //List of column names EXCEPT for "Date" maxCols= List.RemoveItems(Table.ColumnNames(Source),{"Date"}), //set data types #"Changed Type" = Table.TransformColumnTypes(Source, {{"Date", type date}} & List.Transform(maxCols, each {_, Int64.Type})), //Group and create list of aggregation functions group = Table.Group(#"Changed Type", "Date", List.Transform(maxCols, (C) => {C, each List.Max(Table.Column(_, C)), type number}) ) in groupSource Data
Typed
Results
You can create a dynamic List of functions, based on the column names, for both setting the data types, and also performing the aggregations in the Grouping.
In the aggregation transformation, C represents the column name; the underscore ( _ ) represents the subtable returned in the Table.Group function
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY7RDQAhCEN34dtExTv1ZjHuv4ZQMMdHTekjxbWoZs5cKRH0yPOa2UmhDqxBuyvDSZcBIV9TAtEi1A7RFwDa1UzIgC51v9Tg/9wK/BcRTIcFrXsf", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
//List of column names EXCEPT for "Date"
maxCols= List.RemoveItems(Table.ColumnNames(Source),{"Date"}),
//set data types
#"Changed Type" = Table.TransformColumnTypes(Source,
{{"Date", type date}} & List.Transform(maxCols, each {_, Int64.Type})),
//Group and create list of aggregation functions
group = Table.Group(#"Changed Type",
"Date",
List.Transform(maxCols, (C) => {C, each List.Max(Table.Column(_, C)), type number})
)
in
group
Source Data
Typed
Results
- Nilselmano4 years agoFrequent Visitor
Hi ronrsnfld,
I tried this out today and it worked as intended, and i learned a lot from your comment.
Thank you!