Nick Burns
Member
- Joined
- May 24, 2017
- Messages
- 162
- Reaction score
- 0
- Points
- 16
- Excel Version(s)
- Office 365
I created a function to dynamically sum all Numeric columns AND maintain all the columns.
I also added the option to make any column the Count column.

Here's the function:
I was just wondering if there are any improvements that could be made.
EDIT: (Sorry Column1 was inserted when I loaded the totals, that column shouldn't be there).
I also added the option to make any column the Count column.

Here's the function:
Code:
(tbl as table, GroupByColumnList as list, optional CountColumnName as nullable text) as table =>
let
TableSchema = Table.SelectColumns(Table.Schema(tbl),{"Name","Kind"}),
NumberColumns = Table.SelectColumns(Table.SelectRows(TableSchema, each ([Kind] = "number")),{"Name"})[Name],
NonNumberColumns = List.Difference(Table.SelectColumns(Table.SelectRows(TableSchema, each ([Kind] <> "number")),{"Name"})[Name],GroupByColumnList & {CountColumnName}),
SumColumns = List.Transform(NumberColumns, (ColName) => {ColName, (Partition)=> List.Sum(Table.Column(Partition,ColName)), type nullable number}),
NonSumColumns = List.Transform(NonNumberColumns, each {_, each null, type nullable text }),
CountColumns = List.Transform({CountColumnName}, each {_, each Table.RowCount(_), Int64.Type}),
AllColumns = if CountColumnName = null then SumColumns & NonSumColumns else SumColumns & NonSumColumns & CountColumns,
DynamicGroup = Table.Group(tbl, GroupByColumnList, AllColumns),
ReorderColumns = Table.SelectColumns(DynamicGroup,Table.ColumnNames(tbl)),
Result = if CountColumnName = null then ReorderColumns else Table.RenameColumns(ReorderColumns,{{CountColumnName, "Count"}})
in
Result
I was just wondering if there are any improvements that could be made.
EDIT: (Sorry Column1 was inserted when I loaded the totals, that column shouldn't be there).
Attachments
Last edited: