Forum Discussion

k1s1's avatar
k1s1
Helper I
5 years ago
Solved

Pivot columns vs group

Hello

 

I'm struggling with pivoting and grouping amd woudl greatly appreciate some help

 

I have a few hundred rows of data like this:

 

5 Ideas a "Score before" from 1-7 and an Uplift also between 1-7.  

 

 

Idea

Score before

Uplift

idea4

6

7

idea4

4

5

idea5

4

5

idea1

4

5

idea2

5

5

idea5

7

7

idea5

7

6

idea4

7

7

idea2

6

6

 

If pivot and re-order columns, like this


 

 

 

   #"Filtered Rows1" = Table.SelectRows(#"Renamed Columns2", each ([Idea] = "idea5")),
    #"Pivoted Column1" = Table.Pivot(Table.TransformColumnTypes(#"Filtered Rows1", {{"Uplift", type text}}, "en-GB"), List.Distinct(Table.TransformColumnTypes(#"Filtered Rows1", {{"Uplift", type text}}, "en-GB")[Uplift]), "Uplift", "Idea", List.Count),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column1",{"Score before", "1", "2", "3", "4", "5", "6", "7"}),

 

 

 

I get something like this:

 

 

Score before1234567
11010000
20000001
31110100
402210960
500171085
61016132421
70012102450

 

 

But what I'd really like is to get the pivot arranged like this:

 

IdeaScore before1234567
idea511010000
idea520000001
idea531110100
idea5402210960
idea5500171085
idea561016132421
idea570012102450
idea41

0

001000
idea420100201
idea430001211
idea4401114844
idea45000312109
idea460003153015
idea47000651745

 

i.e. in the Table above - for a given Idea, idea5, if the score before was 4, the number of times it was rated 5 in he Uplift was 9

 

Is some kind of Grouping the answer instead of using the pivot function? 

 

  • Hello k1s1 

     

    not able to follow you. Could you please a few lines of your dataset and what the expected result is of that. What I understood is that this "9" is a count of rows where idea5 with rating 4 and uplift 9. I'm wrong? If not try this... add a count-column with each 1. Then pivot the Uplift-column and summing the count-column. Here the code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WykxJTTRR0lECYXOlWB1CIkYoIsZkiRgBsSGGCKYaY6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Idea = _t, #"Score before" = _t, Uplift = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Idea", type text}, {"Score before", Int64.Type}, {"Uplift", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Count", each 1, type number),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Custom", {{"Uplift", type text}}, "de-DE"), List.Sort(List.Distinct(Table.TransformColumnTypes(#"Added Custom", {{"Uplift", type text}}, "de-DE")[Uplift]), Order.Ascending), "Uplift", "Count", List.Sum)
    in
        #"Pivoted Column"

    transforms this

     

    into this


    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

     

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    "i.e. in the Table above - for a given Idea, idea5, if the score before was 4, the number of times it was rated 5 in he Uplift was 9"

     

    but can the 9 be obtained from the example data you provided above or does it derive from the complete table? 

     

    In this latter case you should load the table in such a way that it can be copied and explained in more detail (perhaps with some more examples)  the logic to be applied)

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello k1s1 

     

    not able to follow you. Could you please a few lines of your dataset and what the expected result is of that. What I understood is that this "9" is a count of rows where idea5 with rating 4 and uplift 9. I'm wrong? If not try this... add a count-column with each 1. Then pivot the Uplift-column and summing the count-column. Here the code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WykxJTTRR0lECYXOlWB1CIkYoIsZkiRgBsSGGCKYaY6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Idea = _t, #"Score before" = _t, Uplift = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Idea", type text}, {"Score before", Int64.Type}, {"Uplift", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Count", each 1, type number),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Custom", {{"Uplift", type text}}, "de-DE"), List.Sort(List.Distinct(Table.TransformColumnTypes(#"Added Custom", {{"Uplift", type text}}, "de-DE")[Uplift]), Order.Ascending), "Uplift", "Count", List.Sum)
    in
        #"Pivoted Column"

    transforms this

     

    into this


    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

     

    • k1s1's avatar
      k1s1
      Helper I

      Hi Jimmy,

       

      Your solution  is working perfectly, thank you.

       

      I don't understand it though.

       

          #"Pivoted Column" = Table.Pivot(
              Table.TransformColumnTypes(#"Added Custom", {{"Uplift", type text}}), 
                                                                      
              List.Sort(
                          List.Distinct(
                                          Table.TransformColumnTypes(
                                                                      #"Added Custom", {{"Uplift", type text}}
                                                                      )[Uplift]
                                      ), Order.Ascending
                      ),
               "Uplift", "Count", List.Sum
                                      )

       

      Would you mind explaining the code above?  I can't work out what it's doing

       

       

      • Jimmy801's avatar
        Jimmy801
        Community Champion

        Hello k1s1 

         

        this is basically the code by the GUI and as the pivoted column  has to be text, the GUI is transforming it twice

        Here the link to the description of the function so you can check out every parameter of it

        https://docs.microsoft.com/en-us/powerquery-m/table-pivot 


        If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
        Kudoes are nice too

        Have fun

        Jimmy