Forum Discussion

D3K's avatar
D3K
Advocate II
4 years ago
Solved

Transform Data in Power Query

Hello everyone!

Looking for a help here, cause I'm stuck 🙂
I have a table with a pretty complicated structure and need to transform it in a correct database type

In a single column we have Phone ID, Date, Order Number and SKU ID one under another.

Please the image below

 

 How is it possible to transform original table to the target view?

Any help will be highly appreciated

Thanks a lot!

  • Hi D3K 

    Try this

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZNNboNADEavgtg2sfw7trlEDxBl13ZXVcr9F2XIEBiVLCqFFfLj8+BnuFzGN3EbyAc2Pxc5p46nkRwBsd5gMVAar6fL+H77+LwNWC8SKhTD1+3neyAFDGBkGogn44lyDnK0BoXAc8kvSQyPegI37JC4o2lOG2UG3VESw9hoyS7LaGG13DoLqG5UhFRqmRpWkF1YCnoN6yNspad1qDWLwPlHiVGINSUCmE2JTaiTljkmLc4zC32NEsxQ31HqsmTOtSx3KgyzoY7KUyqci85t5rQDnbKuuTtY1FM2nSoQ+E+driredBrUZxadMnGZhOv7rrsq8/Av+sJWncdDkwbyRrOHzfWTaHN9vCc2vru2O45+E/NPqLvO1G/icE/XXw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Status = _t, Quantity = _t, Price = _t]),
        #"Reordered Columns" = Table.ReorderColumns(Source,{"Status", "Quantity", "Price"}),
        #"Added Custom" = Table.AddColumn(#"Reordered Columns", "Phone ID", each if Text.StartsWith([Status],"+") then [Status] else null),
        #"Added Conditional Column" = Table.AddColumn(#"Added Custom", "ORDER Number", each if Text.StartsWith([Status], "Order") then Text.Middle([Status],6,11) else null),
        #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Date", each if Text.StartsWith([Status], "Order") then Text.Middle([Status],23,10) else null),
        #"Added Conditional Column2" = Table.AddColumn(#"Added Conditional Column1", "SKU ID", each if not Text.StartsWith([Status],"+") and not Text.StartsWith([Status], "Order") then [Status] else null),
        #"Reordered Columns1" = Table.ReorderColumns(#"Added Conditional Column2",{"Status", "Date", "Phone ID", "ORDER Number", "SKU ID", "Quantity", "Price"}),
        #"Filled Down" = Table.FillDown(#"Reordered Columns1",{"Date", "Phone ID", "ORDER Number"}),
        #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([SKU ID] <> null)),
        #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Status"})
    in
        #"Removed Columns"

     

    Best Regards,

    Community Support Team _Tang

    If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

4 Replies

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

    Hi D3K 

    Try this

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZNNboNADEavgtg2sfw7trlEDxBl13ZXVcr9F2XIEBiVLCqFFfLj8+BnuFzGN3EbyAc2Pxc5p46nkRwBsd5gMVAar6fL+H77+LwNWC8SKhTD1+3neyAFDGBkGogn44lyDnK0BoXAc8kvSQyPegI37JC4o2lOG2UG3VESw9hoyS7LaGG13DoLqG5UhFRqmRpWkF1YCnoN6yNspad1qDWLwPlHiVGINSUCmE2JTaiTljkmLc4zC32NEsxQ31HqsmTOtSx3KgyzoY7KUyqci85t5rQDnbKuuTtY1FM2nSoQ+E+driredBrUZxadMnGZhOv7rrsq8/Av+sJWncdDkwbyRrOHzfWTaHN9vCc2vru2O45+E/NPqLvO1G/icE/XXw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Status = _t, Quantity = _t, Price = _t]),
        #"Reordered Columns" = Table.ReorderColumns(Source,{"Status", "Quantity", "Price"}),
        #"Added Custom" = Table.AddColumn(#"Reordered Columns", "Phone ID", each if Text.StartsWith([Status],"+") then [Status] else null),
        #"Added Conditional Column" = Table.AddColumn(#"Added Custom", "ORDER Number", each if Text.StartsWith([Status], "Order") then Text.Middle([Status],6,11) else null),
        #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Date", each if Text.StartsWith([Status], "Order") then Text.Middle([Status],23,10) else null),
        #"Added Conditional Column2" = Table.AddColumn(#"Added Conditional Column1", "SKU ID", each if not Text.StartsWith([Status],"+") and not Text.StartsWith([Status], "Order") then [Status] else null),
        #"Reordered Columns1" = Table.ReorderColumns(#"Added Conditional Column2",{"Status", "Date", "Phone ID", "ORDER Number", "SKU ID", "Quantity", "Price"}),
        #"Filled Down" = Table.FillDown(#"Reordered Columns1",{"Date", "Phone ID", "ORDER Number"}),
        #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([SKU ID] <> null)),
        #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Status"})
    in
        #"Removed Columns"

     

    Best Regards,

    Community Support Team _Tang

    If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

    • D3K's avatar
      D3K
      Advocate II

      v-xiaotang 
      So elegant solution! Works like a charm
      Excactly what I need
      Thanks a lot!

  • D3K it can be done, throw it in a sample pbix file and also share a sample file that can be used and will able to provide the solution.

     

     

    Follow us on LinkedIn and  to our YouTube channel

     

    Learn about conditional formatting at Microsoft Reactor

    My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

     

    Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.