Forum Discussion

rnehrboss's avatar
rnehrboss
Helper II
3 years ago

Pivot/Unpivot Question

Having trouble thinking through this pivot/unpivot question transform.

I have data like this:

IDWhich fruits do you like?How many do you have?Which fruits do you like 2?How many do you have 2?Which fruits do you like 3?How many do you have 2?
101Apple4Plumb8Orange14
102Orange3Apple2Pear9
103Peach5Orange2Grapes11
104Grapes2Apple3Plumb12

 

I'm trying to get to data like this:

101Which FruitsApple4
102Which FruitsOrange3
103Which FruitsPeach5
104Which FruitsGrape5
101Which 2 Plumb8
102Which 2 Apple2
103Which 2 Orange2
104Which 2 Apple3
101Which 3Orange14
102Which 3Pear9
103Which 3Grapes11
104Which 3Plumb12

 

And finally to this (which is simply conditionally changing the Which 2 and 3's to which... so I think that's not hard)

101Which FruitsApple4
102Which FruitsOrange3
103Which FruitsPeach5
104Which FruitsGrape5
101Which FruitsPlumb8
102Which FruitsApple2
103Which FruitsOrange2
104Which FruitsApple3
101Which FruitsOrange14
102Which FruitsPear9
103Which FruitsGrapes11
104Which FruitsPlumb12

 

Any ideas?

Thanks!

 

4 Replies

  • DOLEARY85's avatar
    DOLEARY85
    Resident Rockstar

    Hi,

     

    you could duplicate the table a couple of times and remove 2 of the which friuts and how many columns in each.

    Rename all the columns to remove numbering and so they are the same in each table and then append them together, then just unpivot the which fruits column.

     

    If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

    • rnehrboss's avatar
      rnehrboss
      Helper II

      A great idea.  Problem is I have about 30 groups of these with 10 each.. So might become quite cumbersome.

      • DOLEARY85's avatar
        DOLEARY85
        Resident Rockstar

        Ah okay, you could create a calculated table with DAX in the data view:

         

        t2 =
        UNION(SUMMARIZE('Table (6)','Table (6)'[ID],'Table (6)'[Which fruits do you like?],'Table (6)'[How many do you have?]),SUMMARIZE('Table (6)','Table (6)'[ID],'Table (6)'[Which fruits do you like 2?],'Table (6)'[How many do you have 2?]),(SUMMARIZE('Table (6)','Table (6)'[ID],'Table (6)'[Which fruits do you like 3?],'Table (6)'[How many do you have 2?.1])))
         

         

        If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

  • DOLEARY85's avatar
    DOLEARY85
    Resident Rockstar

    Sorry forgot the last part - add a new column to that table for which fruits:

     

    Column = "Which Fruits do You Like?"