Forum Discussion

ngct1112's avatar
ngct1112
Icon for Post Patron rankPost Patron
5 years ago
Solved

Efficient way to transform a table

Hi All,

I am looking for an efficient way to transform a table.

The way I am working on it is 

1.) pivot column 2.) add conditional columns 3.) Groupby

From:

IDUserCostRank
001_1000A0.551
001_1000B1.272
001_1000C1.33
001_2001K0.851
001_2001L0.882
001_2001M0.933


To:

IDUser 1Supplier 1 CostUser 2Supplier 2 CostUser 3Supplier 3 Cost
001_1000A0.55B1.27C1.3
001_2001K0.85L0.88M0.93


Great thanks.

But my way to handle this work is so inefficient, may I seek for some helps here? 

 

  • Hi, ngct1112 

     

    I tried it, and I got the final result, but since PowerQuery is not my area of expertise, I did not find a very effective method. Ideally, I should create a custom function to complete it.
    Below is my code. You can see that this is not efficient, maybe you have better insights.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwjDc0MDBQ0lFyBGIDPVNTIGWoFKuDIucEEtQzMgdSRuhyzmA5YyBpDJcyAhJAAW+wkRaoRkLlfCByFihGQuV8wXKWUDNjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, User = _t, Cost = _t, Rank = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"User", type text}, {"Cost", type number}, {"Rank", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Content", each _, type table [ID=nullable text, User=nullable text, Cost=nullable number, Rank=nullable number]}}),
    //there should be a function
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "User 1", each [Content]{0}[User]),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Supplier 1 Cost", each [Content]{0}[Cost]),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "User 2", each [Content]{1}[User]),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Supplier 2 Cost", each [Content]{1}[Cost]),
    #"Added Custom4" = Table.AddColumn(#"Added Custom3", "User 3", each [Content]{2}[User]),
    #"Added Custom5" = Table.AddColumn(#"Added Custom4", "Supplier 3 Cost", each [Content]{2}[Cost]),
    // function end
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom5",{"Content"})
    in
    #"Removed Columns"

     By the way, there is a PowerQuery forum here. Perhaps this question can be answered quickly in that forum, and there are many experts in this area. If you can find a better answer, please share it for others to learn, thank you.

     

     

    Best Regards,
    Community Support Team _ Zeon Zheng

1 Reply

  • Hi, ngct1112 

     

    I tried it, and I got the final result, but since PowerQuery is not my area of expertise, I did not find a very effective method. Ideally, I should create a custom function to complete it.
    Below is my code. You can see that this is not efficient, maybe you have better insights.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwjDc0MDBQ0lFyBGIDPVNTIGWoFKuDIucEEtQzMgdSRuhyzmA5YyBpDJcyAhJAAW+wkRaoRkLlfCByFihGQuV8wXKWUDNjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, User = _t, Cost = _t, Rank = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"User", type text}, {"Cost", type number}, {"Rank", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Content", each _, type table [ID=nullable text, User=nullable text, Cost=nullable number, Rank=nullable number]}}),
    //there should be a function
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "User 1", each [Content]{0}[User]),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Supplier 1 Cost", each [Content]{0}[Cost]),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "User 2", each [Content]{1}[User]),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Supplier 2 Cost", each [Content]{1}[Cost]),
    #"Added Custom4" = Table.AddColumn(#"Added Custom3", "User 3", each [Content]{2}[User]),
    #"Added Custom5" = Table.AddColumn(#"Added Custom4", "Supplier 3 Cost", each [Content]{2}[Cost]),
    // function end
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom5",{"Content"})
    in
    #"Removed Columns"

     By the way, there is a PowerQuery forum here. Perhaps this question can be answered quickly in that forum, and there are many experts in this area. If you can find a better answer, please share it for others to learn, thank you.

     

     

    Best Regards,
    Community Support Team _ Zeon Zheng