Forum Discussion

gowtham1991's avatar
gowtham1991
Frequent Visitor
1 year ago
Solved

Transformming table columns

Hello Experts, 

 

I have an input table like the below

ItemidprojectidnameML0ML1ML2ML3ML4ML5ML6ML7
123 name1release2,release1,release 3 release4,release5,release6,release7 release8   

and the expected output table to be displayed in power bi is
Itemidprojectidnamerelease1release2release3release4release5release6release7release8
123 name1ML0ML0ML0ML2ML2ML2ML2ML5

I would seek your support or suggestion in how to get this done.

 
Thanks
Gowtham
  • Hi gowtham1991, you can achieve the required result by leveraging Pivot and Unpivot functions. Make sure that the column with releases contains only unique values, because column names in Power Query can't repeat (otherwise you'll need to make them unique using index funciton).


    Here is a complete code for Power Query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzc1MUdJRKijKz0pNLgGz8xJzU4GUr48BmDQEk0Zg0hhMmoBJUzBpBibNlWJ1opUMjUDyUBNA2opSc1ITi1ONdKAMQxhDAaoQyjWBiZvCGGY6ClCWOYpKCwgPhGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t, Column11 = _t]),
        #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Itemid", "projectid", "name"}, "Attribute", "Value"),
        // Change 10 to the maximum expected value of values to split. If not available, use number much larger from the expected maximum
        #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Value.1", "Value.2", "Value.3", "Value.4", "Value.5", "Value.6", "Value.7", "Value.8", "Value.9", "Value.10"}),
        #"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Split Column by Delimiter", {"Itemid", "projectid", "name", "Attribute"}, "Attribute.1", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns1",{"Attribute.1"}),
        #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Value] <> null and [Value] <> ""),
        #"Pivoted Column" = Table.Pivot(#"Filtered Rows", #"Filtered Rows"[Value], "Value", "Attribute")
    in 
        #"Pivoted Column"

     

    Input:

     

    Output:

     

     

    References where you can find more details on the proposed approach:

    Solved: Split comma delimited cell into multiple rows, kee... - Microsoft Fabric Community

    excel - How can I transpose only some columns of a table in power query? - Stack Overflow

    Table.Pivot - Table Function | Power Query M

     

    Good luck with your project!

  • gowtham1991's avatar
    gowtham1991
    1 year ago

    Hello SamWiseOwl  , I have to do this for multiple rows. My data set has multiple rows to be converted, and I could achieve this in re ordeing step, as the columns are not fixed, and very dynamic .can you suggest on work around?

  • Sergii24's avatar
    Sergii24
    1 year ago

    Hi gowtham1991, the key to your issue is using transpose/pivot or unpivot functions.

     

    I hope the answers you've got here so far have inspired you so that you can apply them to your specific case. However, the aim of this community is not to solve your problems but rather to guide you in the right direction and inspire you to learn more and resolve them on your own 🙂

    Good luck with your project!

8 Replies

    • gowtham1991's avatar
      gowtham1991
      Frequent Visitor

      Hello SamWiseOwl  , I have to do this for multiple rows. My data set has multiple rows to be converted, and I could achieve this in re ordeing step, as the columns are not fixed, and very dynamic .can you suggest on work around?

  • Hi gowtham1991, you can achieve the required result by leveraging Pivot and Unpivot functions. Make sure that the column with releases contains only unique values, because column names in Power Query can't repeat (otherwise you'll need to make them unique using index funciton).


    Here is a complete code for Power Query:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzc1MUdJRKijKz0pNLgGz8xJzU4GUr48BmDQEk0Zg0hhMmoBJUzBpBibNlWJ1opUMjUDyUBNA2opSc1ITi1ONdKAMQxhDAaoQyjWBiZvCGGY6ClCWOYpKCwgPhGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t, Column11 = _t]),
        #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Itemid", "projectid", "name"}, "Attribute", "Value"),
        // Change 10 to the maximum expected value of values to split. If not available, use number much larger from the expected maximum
        #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Value.1", "Value.2", "Value.3", "Value.4", "Value.5", "Value.6", "Value.7", "Value.8", "Value.9", "Value.10"}),
        #"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Split Column by Delimiter", {"Itemid", "projectid", "name", "Attribute"}, "Attribute.1", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns1",{"Attribute.1"}),
        #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Value] <> null and [Value] <> ""),
        #"Pivoted Column" = Table.Pivot(#"Filtered Rows", #"Filtered Rows"[Value], "Value", "Attribute")
    in 
        #"Pivoted Column"

     

    Input:

     

    Output:

     

     

    References where you can find more details on the proposed approach:

    Solved: Split comma delimited cell into multiple rows, kee... - Microsoft Fabric Community

    excel - How can I transpose only some columns of a table in power query? - Stack Overflow

    Table.Pivot - Table Function | Power Query M

     

    Good luck with your project!

    • gowtham1991's avatar
      gowtham1991
      Frequent Visitor

      Hello Sergii24 , I have to do this for multiple rows. My data set has multiple rows to be converted, and I could achieve this.can you suggest on work around?

      • Sergii24's avatar
        Sergii24
        Super User

        Hi gowtham1991, the key to your issue is using transpose/pivot or unpivot functions.

         

        I hope the answers you've got here so far have inspired you so that you can apply them to your specific case. However, the aim of this community is not to solve your problems but rather to guide you in the right direction and inspire you to learn more and resolve them on your own 🙂

        Good luck with your project!

  • v-venuppu's avatar
    v-venuppu
    Community Support

    Hi gowtham1991 ,

    May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

    Thank you.

     

    Regards,

    Rama U.

  • v-venuppu's avatar
    v-venuppu
    Community Support

    Hi gowtham1991 ,

    I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.

    Thank you.

    Regards,

    Rama U.

  • v-venuppu's avatar
    v-venuppu
    Community Support

    Hi gowtham1991 ,

    I hope this information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please accept it as a solution and give it a 'Kudos' so other community members with similar problems can find a solution faster.

    Thank you.

    Regards,

    Rama U.