PQ Function to Group by All Non-Number Columns

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I would like to write a Power Query function which lets me pass a table as a parameter, and the function will output a table which groups the input table by all non-numeric columns. I'd like for it to be dynamic so I don't need to specify any column labels... just pass a table and it will group by each non-numeric column and then sum each of the numeric columns. Is that possible?
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I think so.

Code:
(InTbl as table) as table =>


let  
    NumCols = Table.ToList(Table.SelectColumns(Table.SelectRows(Table.Schema(InTbl), each ([Kind] = "number")),{"Name"})),
    OtherCols = Table.ToList(Table.SelectColumns(Table.SelectRows(Table.Schema(InTbl), each ([Kind] <> "number")),{"Name"})),


    #"Unpivoted Columns" = Table.UnpivotOtherColumns(InTbl, OtherCols, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", List.Combine({{"Attribute"}, OtherCols}), {{"Amount", each List.Sum([Value]), type number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Attribute]), "Attribute", "Amount", List.Sum)
in
    #"Pivoted Column"

The datatypes should be set prior to passing the table to the function.
 
Upvote 0
I forgot to paste some updates in time.

Code:
(InTbl as table) as table =>


let  
    NumCols = Table.ToList(Table.SelectColumns(Table.SelectRows(Table.Schema(InTbl), each ([Kind] = "number")),{"Name"})),
    OtherCols = Table.ToList(Table.SelectColumns(Table.SelectRows(Table.Schema(InTbl), each ([Kind] <> "number")),{"Name"})),
    
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(InTbl, OtherCols, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", List.Combine({{"Attribute"}, OtherCols}), {{"Amount", each List.Sum([Value]), type number}}),
    #"Pivoted Column" = if List.Count(NumCols) = 0 or List.Count(OtherCols) = 0 then Table.FromList({[Error Message="Columns Types were not set correctly"]}, Record.FieldValues, {"Error Message"}) else Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Attribute]), "Attribute", "Amount", List.Sum)
in
    #"Pivoted Column"
 
Upvote 0

Forum statistics

Threads
1,214,548
Messages
6,120,146
Members
448,948
Latest member
spamiki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top