Skip to content

Instantly share code, notes, and snippets.

@ImkeF
Last active November 18, 2019 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImkeF/87130d9f67837dbceb3f1d5b3847c0cb to your computer and use it in GitHub Desktop.
Save ImkeF/87130d9f67837dbceb3f1d5b3847c0cb to your computer and use it in GitHub Desktop.
Aggregates all columns from the <code>SourceTable</code> that are not included in <code>GroupColumnNames</code> with Sum as default operation. This can be changed in optional parameter <code>AggregationFunction</code>.
let func =
(SourceTable as table, GroupColumnNames as list, optional AggregationFunction as function, optional Type as type) =>
let
/* Debug parameters
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXIGYkMDIGFqoBSrgxAzAomZQcScgEwXIDYGiZkjxFyB2AQkZgEUiwUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [GroupColA = _t, GroupColB = _t, SumCol1 = _t, SumCol2 = _t]),
SourceTable = Table.TransformColumnTypes(Source,{{"GroupColA", type text}, {"GroupColB", type text}, {"SumCol1", Int64.Type}, {"SumCol2", Int64.Type}}),
GroupColumns = {"GroupColA", "GroupColB"},
*/
MyType = if Type = null then type number else Type,
AggregateFunction = if AggregationFunction = null then List.Sum else AggregationFunction,
AggregateColumns = List.Difference(Table.ColumnNames(SourceTable),GroupColumnNames),
DynamicGrouping = List.Transform(AggregateColumns, (ColName) => {ColName, (Partition)=> AggregateFunction(Table.Column(Partition,ColName)), MyType}),
#"Grouped Rows" = Table.Group(SourceTable, GroupColumnNames, DynamicGrouping)
in
#"Grouped Rows" ,
documentation = [
Documentation.Name = " Table.GroupDynamicAggregation ",
Documentation.Description = " Aggregates all columns from the <code>SourceTable</code> that are not included in <code>GroupColumnNames</code> with Sum as default operation. This can be changed in optional parameter <code>AggregationFunction</code>. ",
Documentation.LongDescription = " Aggregates all columns from the <code>SourceTable</code> that are not included in <code>GroupColumnNames</code> with Sum as default operation. This can be changed in optional parameter <code>AggregationFunction</code>. ",
Documentation.Category = " Table ",
Documentation.Source = " ",
Documentation.Version = " 1.0 ",
Documentation.Author = " Imke Feldmann: www.TheBIccountant.com. ",
Documentation.Examples = {[Description = " ",
Code = " TableGroupDynamicAggregation(#table( {""GroupColA"", ""GroupColB"", ""SumCol1"", ""SumCol2""}, List.Zip( { {""A"" ,""A"" ,""B"" ,""B""}, {""C"" ,""C"" ,""D"" ,""E""}, {""10"" ,""20"" ,""30"" ,""40""}, {""50"" ,""60"" ,""70"" ,""80""} } ) )) ",
Result = " #table( {""GroupColA"", ""GroupColB"", ""SumCol1"", ""SumCol2""}, List.Zip( { {""A"" ,""B"" ,""B""}, {""C"" ,""D"" ,""E""}, {""30"" ,""30"" ,""40""}, {""110"" ,""70"" ,""80""} } ) ) "]}]
in
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment