Forum Discussion

kellyylx's avatar
kellyylx
Helper I
1 year ago
Solved

Duplicate rows based on a column

Hi I have this table below and I want to transform the table to have individual records for each country 

countryFruit Name  Weight (kg)
Singapore; Philippines   Apple5
MalaysiaBanana2
IndiaMango3
Singapore; IndiaKiwi7
ChinaPineapple6
China; HK; IndonesiaOrange3
TaiwanBlueberry2
KoreaRaspberry1
JapanPapaya4


this is my intended outcome

countryFruit Name   Weight (kg)
SingaporeApple5
Philippines   Apple5
MalaysiaBanana2
IndiaMango3
SingaporeKiwi7
IndiaKiwi7
ChinaPineapple6
ChinaOrange3
HKOrange3
IndonesiaOrange3
TaiwanBlueberry2
KoreaRaspberry1
JapanPapaya4


How can I do this in power bi? 

  • Hi kellyylx 

     

    In Power Query please try the following

     

    • Highlight the country column and go to the Transform  tab in the ribbon and choose Split column > By delimiter
    • In the Select the delimiter used field, choose custom and enter ; and then space and ok
    • Highlight all country columns and choose unpivot columns

       

     

    Hope this helps
    Joe

     

     

3 Replies

  • Joe_Barry's avatar
    Joe_Barry
    Solution Sage

    Hi kellyylx 

     

    In Power Query please try the following

     

    • Highlight the country column and go to the Transform  tab in the ribbon and choose Split column > By delimiter
    • In the Select the delimiter used field, choose custom and enter ; and then space and ok
    • Highlight all country columns and choose unpivot columns

       

     

    Hope this helps
    Joe

     

     

    • kellyylx's avatar
      kellyylx
      Helper I

      hi thanks for the solution, I realised i missed out something in my original table where I have the country code for the country

       

      country codecountryFruit NameWeight (kg)
      A; CSingapore; PhilippinesApple5
      BMalaysiaBanana2
      DIndiaMango3
      A; DSingapore; IndiaKiwi7
      EChinaPineapple6
      E; P; LChina; HK; IndonesiaOrange3
      GTaiwanBlueberry2
      HKoreaRaspberry1
      OJapanPapaya4

       

      and my intended outcome is 

      country codecountryFruit NameWeight (kg)
      ASingaporeApple5
      CPhilippinesApple5
      BMalaysiaBanana2
      DIndiaMango3
      ASingaporeKiwi7
      DIndiaKiwi7
      EChinaPineapple6
      EChinaOrange3
      PHKOrange3
      LIndonesiaOrange3
      GTaiwanBlueberry2
      HKoreaRaspberry1
      OJapanPapaya4

       

      you can assume that the order of the country code and country is the same (eg E; P; L for China; HK; Indonesia means that China - C, HK - P and Indonesia - L)

       

      how can i go about doing this?

       

      • Joe_Barry's avatar
        Joe_Barry
        Solution Sage

        Before you Unpivot the columns,

         

        • please repeat the process for the country codes.
        • Then highlight the country code 1 and country 1 columns and in the transform tab choose merge columns and choose Tab as a separator. Repeat for the other countires.
        • Then unpivot the combined columns
        • Highlight the new column go to the Transform tab and choose split column by dilimter and choose Tab

         

        Hope this helps