Forum Discussion

AnonymeC's avatar
AnonymeC
Regular Visitor
3 years ago
Solved

Equivalent to "Group By" function on multiple columns?

Hello,

 

I have a huge database that basically looks like:

 

CodeMarketFactoryJul 21Aug 21Sep 21....Apr 23May 23Jun 23
3424555332DACHXXX03424...225012

....

 

I have several rows with the same combination of Code/Market/Factory, and would like to consolidate all these rows in one unique row per combination. I know I can do it with "Group By" function, which is convenient with a small numbers of columns, but I have over 30 columns of date of shipments. Is there an easy way to do that without doing 30 times this with Group By?

 

Thank you very much for your help.

 

  • slorin's avatar
    slorin
    3 years ago

    Hi

    UnPivot then pivot

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

    Stéphane 

4 Replies

  • Hello, AnonymeC how would you like to summarize other columns? In general, one need to group by first 3 columns and select "All rows" as "operation". This gives you the whole table. One may apply a custom (user defined) function then to modify that table to get what you want. Just let us know what would you like to do to other columns - simple sum operation? 

  • AnonymeC's avatar
    AnonymeC
    Regular Visitor

    Hello AlienSx , yes that's right for the rows that have the same combination of Code/Market/Factory, I would like to do a sum.

     

    Example:

    If I have originally this table:

    CodeMarketFactoryJul21Aug21Sep21...Apr23May23Jun23
    0000011DACHPlant10238...0766
    0000011DACHPlant1247...051

     

    It should consolidate into:

    CodeMarketFactoryJul21Aug21Sep21...Apr21May21Jun21
    0000011DACHPlant122715...0817

     

    Many thanks.

    • slorin's avatar
      slorin
      Super User

      Hi

      UnPivot then pivot

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

      Stéphane