Forum Discussion

AnonymeC's avatar
AnonymeC
Regular Visitor
1 year ago
Solved

[Help needed] Group by several columns without selecting individually 1 by 1

Hi everyone,

 

My aim is very simple yet somehow I couldn't find the ressources to do this task, I was hoping you could help me.

 

So my goal is to do a simple group by for all the columns in my worksheet with data, I am aware that with "Advanced" functionnality you can add multiple "New column name", but the issue is I have more than 100 columns in my data. Is there a simple way to ask to do the grouping with Operation "Sum" to all remaining columns in my dataset (excluding the column used for grouping of course). Doing manually 1 by 1 a hundred times "Add Aggregation" is really not an idea that please me I must say 😄

 

Thanks a lot for your help.

  • Hi AnonymeC 

     

    let
    Source = YourSource,
    UnPivot = Table.UnpivotOtherColumns(Source, {"Code"}, "Attribute", "Value"),
    Pivot = Table.Pivot(UnPivot, List.Distinct(UnPivot[Attribute]), "Attribute", "Value", List.Sum)
    in
    Pivot

    or

    let
    Source = YourSource,
    Group = Table.Group(Source, {"Code"},
    List.Transform(
    List.Skip(Table.ColumnNames(Source)),
    each {_, Expression.Evaluate("each List.Sum(["& _ &"])", #shared), type number } ) )
    in
    Group 

    Stéphane 

6 Replies

  • Please provide a representative data sample (preferably as text which can be copy/pasted, or a link to a file) along with expected data output from that sample.

     

    It is not clear to me exactly what you want to do and the above will be helpful. In general:

     

    • Create a list of the column names you wish to aggregate starting with Table.ColumnNames(#"Previous Step") and applying some selection criteria.
    • Programmatically create a list of your aggregation argument lines (probably using List.Transform).
    • Use that created list as an argument to the Table.Group function

    Again, depending on your expected output, you might apply that list to the List.Accumulate, List.Generate, or a recursive function.

  • AnonymeC's avatar
    AnonymeC
    Regular Visitor

    Hi, please see below an example of data

     

    This is my input:

    CodeJulAugSepQ1OctNovDecQ2
    AAAA25714310215
    AAAA02136219
    BBBB5551555515
    BBBB5551555515
    CCCC3519102214

     

    Expected Output:

    CodeJulAugSepQ1OctNovDecQ2
    AAAA27817912324
    BBBB1010103010101030
    CCCC3519102214

     

    Except I have much more columns than these 8 here, so doing manually the Add Aggregation is not a suitable solution.

    • ronrsnfld's avatar
      ronrsnfld
      Super User

      Try this:

       

      let
      
      //Change Source to your own data source
          Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcgQCJR0lIyA2BWJzIDY0ARLGIIYBVMbQVClWB64WLgpVZobEtwQrdAICqIEwbGiKKUCiUmcggFppCrcOyZVGELfHxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Code = _t, Jul = _t, Aug = _t, Sep = _t, Q1 = _t, Oct = _t, Nov = _t, Dec = _t, Q2 = _t]),
      
      //Set data types
          #"Changed Type" = Table.TransformColumnTypes(Source, List.Transform(List.Skip(Table.ColumnNames(Source)), each {_, Int64.Type})),
              
          #"Group by Code" = Table.Group(#"Changed Type","Code",{
              {"Sums", (t)=> [a=List.Skip(Table.ColumnNames(t)),
                              b=List.Transform(a, each List.Sum(Table.Column(t,_))),
                              c=Record.FromList(b,a)][c]
                                  }}),
          #"Expanded Sums" = Table.ExpandRecordColumn(#"Group by Code", "Sums", List.Skip(Table.ColumnNames(Source))),
          #"Changed Type2" = Table.TransformColumnTypes(#"Expanded Sums", List.Transform(List.Skip(Table.ColumnNames(#"Expanded Sums")), each {_, Int64.Type}))
      in
          #"Changed Type2"
  • Hi AnonymeC 

     

    let
    Source = YourSource,
    UnPivot = Table.UnpivotOtherColumns(Source, {"Code"}, "Attribute", "Value"),
    Pivot = Table.Pivot(UnPivot, List.Distinct(UnPivot[Attribute]), "Attribute", "Value", List.Sum)
    in
    Pivot

    or

    let
    Source = YourSource,
    Group = Table.Group(Source, {"Code"},
    List.Transform(
    List.Skip(Table.ColumnNames(Source)),
    each {_, Expression.Evaluate("each List.Sum(["& _ &"])", #shared), type number } ) )
    in
    Group 

    Stéphane 

  • AnonymeC's avatar
    AnonymeC
    Regular Visitor

    Thank you all for your feedback, the code shared by Stéphane worked perfectly fine so I took it as solution, but appreciate the other answers, will give it a try.