Advanced Transformation

Hi,

I am stuck a transforming one of below attached table; this is my stock transfer from one depot to another depot.

# TOS Number TOO From Depot TOO Number Supplier Name Brand Name TOO Qty TOS Qty Qty BL Rate Amount
Date Type Destination TOO Date Size CBs Btls CBs Btls
1 ALR00023367(F) D-TO-D Alwar Depot ALR00059384 My Company Shampoo1 10 0 10 0 90 1000 10,000.00
5-Aug-19 Bharatpur Depot 30-Jul-19 90 ml(100 Bottles)
2 BMR01012205(F) D-TO-D Barmer Depot BMR01071221 My Company Shampoo2 50 0 50 0 432 700 35,000.00
8-Aug-19 Jodhpur Depot 5-Aug-19 180 ml
Shampoo4 8 0 8 0 72 900 7,200.00
375 ml
Conditioner1 50 0 50 0 450 1100 55,000.00
750 ml

Now i want to transform data using power query as per below desired format so that i can have quantity from one depot to another depot which got transferred. Request your expertise in this matter;

TOS Number TOS Date TOO Type From Depot To Depot TOO Number TOO Date Supplier Name Brand Name Size TOO Qty CBs TOO Qty Btls TOS Qty CBs TOS Qty Btls Qty BL Rate Amount
ALR00023367(F) 8/5/2019 D-TO-D Alwar Depot Bharatpur Depot ALR00059384 7/30/2019 My Company Shampoo1 90 ml(100 Bottles) 10 0 10 0 90 1000 10000
BMR01012205(F) 8/8/2019 D-TO-D Barmer Depot Jodhpur Depot BMR01071221 8/5/2019 My Company Shampoo2 180 ml 50 0 50 0 432 700 35000
BMR01012205(F) 8/8/2019 D-TO-D Barmer Depot Jodhpur Depot BMR01071221 8/5/2019 My Company Shampoo4 375 ml 8 0 8 0 72 900 7200
BMR01012205(F) 8/8/2019 D-TO-D Barmer Depot Jodhpur Depot BMR01071221 8/5/2019 My Company Conditioner1 750 ml 50 0 50 0 450 1100 55000

Early solution is highly appreciated. Thanks

Regards
Harish Rathore

Hello @harishrathore,

Thank You for posting your query onto the Forum.

Is this the type of result you’re looking for? Below is the screenshot of the result provided for the reference -

I’m also attaching the working of the PBIX file for the reference so that you can go through the “Transformation Steps”. I’m not explaining the steps here since it’ll simply increase the length of the post.

In case, you find it difficult to understand any of the steps please give a shout and will be help to guide you. :+1:

Hoping you find this useful and meets your requirements that you’ve been looking for. :slightly_smiling_face:

Thanks and Warm Regards,
Harsh

Advanced Transformation.xlsx (11.0 KB)

Advanced Transformations.pbix (29.2 KB)

3 Likes

Hi @harishrathore,

Paste this into a new blank query.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lVJNa4QwEP0r4mkXVpiYTaPHqvRQugjbvckeAhtQUCOaUuyvb8bP1HYLC0PmzQdv5o1mmese3ERoadylb9AlstNFLXShakymqTPVjb0XX4jiqDNvpMtuG0x2PWQuMeD57QwAPqVPfPeyR3LvknoJVspP0TqJbJRe+lhIg6OJTr0Tq6oRdY8jc1E1SiEbAfOADcIxgtnBMBmLzIPAI+G4T5SLVujmYx1IwQO+1ENwqnJnCJxIaV3Kbr9KsQ3JfeQ7nYEA8X1gG1mRaCu5jhkbuekkd3QhHZvlLOBIMc8HXZTZugJb16u65baqn6pJgLLuK/mdnnbCbxBMm8ye+8OdRrjuszHK2YMjY1XfCvzXZEv+PsWACBlGM/sWG+PsH7nXbw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"#" = _t, #"TOS Number" = _t, TOO = _t, #"From Depot" = _t, #"TOO Number" = _t, #"Supplier Name" = _t, #"Brand Name" = _t, #"TOO Qty" = _t, #"(blank)" = _t, #"TOS Qty" = _t, #"(blank).1" = _t, #"Qty BL" = _t, Rate = _t, Amount = _t]),
    DemoteHeaders = Table.DemoteHeaders(Source),
    FillDown = Table.FillDown(DemoteHeaders,{"Column1"}),
    AddIndex = Table.AddIndexColumn(FillDown, "Index", 0, 1, Int64.Type),
    AddGroupKey = Table.FillDown( Table.AddColumn(AddIndex, "MOD", each if Number.Mod([Index], 2)=0 then [Index] else null ), {"MOD"} ),
    GroupRows = Table.Group( AddGroupKey, {"MOD"}, {{"AllRows", each _, type table [Column1=any, Column2=nullable text, Column3=nullable text, Column4=nullable text, Column5=nullable text, Column6=nullable text, Column7=nullable text, Column8=nullable text, Column9=nullable text, Column10=nullable text, Column11=nullable text, Column12=any, Column13=any, Column14=any, Index=number, MOD=number]}}),
    TransformTable = Table.AddColumn(GroupRows, "Custom", each Table.Transpose( Table.UnpivotOtherColumns( Table.RemoveColumns( [AllRows], { "Index", "MOD" } ), {"Column1"}, "Attribute", "Value")[[Value]]))[[MOD], [Custom]],
    ExpandTable = Table.ExpandTableColumn(TransformTable, "Custom", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23"}, {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23"}),
    PromoteHeaders = Table.PromoteHeaders(ExpandTable, [PromoteAllScalars=true]),
    ReplaceEmptyValue = Table.ReplaceValue(PromoteHeaders,"",null,Replacer.ReplaceValue,{"0", "TOS Number", "TOO", "From Depot", "TOO Number", "Supplier Name", "Brand Name", "TOO Qty", "(blank)", "TOS Qty", "(blank).1", "Qty BL", "Rate", "Amount", "Date", "Type", "Destination", "TOO Date", "", "Size", "CBs", "Btls", "CBs_1", "Btls_2"}),
    FillDownAll = Table.FillDown(ReplaceEmptyValue,{"0", "TOS Number", "TOO", "From Depot", "TOO Number", "Supplier Name", "Brand Name", "TOO Qty", "(blank)", "TOS Qty", "(blank).1", "Qty BL", "Rate", "Amount", "Date", "Type", "Destination", "TOO Date", "", "Size", "CBs", "Btls", "CBs_1", "Btls_2"}),
    RenameColumns = Table.RenameColumns(FillDownAll,{{"TOO", "TOO Type"}, {"Date", "TOS Date"}, {"Destination", "To Depot"}, {"TOO Qty", "TOO Qty CBs"}, {"(blank)", "TOO Qty Btls"}, {"TOS Qty", "TOS Qty CBs"}, {"(blank).1", "TOS Qty Btls"}}),
    RemoveColumns = Table.RemoveColumns(RenameColumns,{"0", "Type", "", "CBs", "Btls", "CBs_1", "Btls_2"}),
    ChangeType = Table.TransformColumnTypes(RemoveColumns,{{"TOS Number", type text}, {"TOO Type", type text}, {"From Depot", type text}, {"TOO Number", type text}, {"Supplier Name", type text}, {"Brand Name", type text}, {"TOO Qty CBs", Int64.Type}, {"TOO Qty Btls", Int64.Type}, {"TOS Qty CBs", Int64.Type}, {"TOS Qty Btls", Int64.Type}, {"Qty BL", Int64.Type}, {"Rate", Int64.Type}, {"Amount", Int64.Type}, {"TOS Date", type date}, {"To Depot", type text}, {"TOO Date", type date}, {"Size", type text}})
in
    ChangeType 

.
The key is seperating the sets of rows

And transforming them into a single line with a combination of UnpivotOtherColumns, SelectColumns and Transpose steps

With this result

Here’s a sample file. eDNA - adv transform.pbix (59.9 KB)
I hope this is helpful.

3 Likes

Huge thanks to @Harsh and @Melissa . I am glad to have different solutions to arrive at the desired outcome. Both solutions are unique. I wish i could select both answer as “Solution”. These would save at least 2 days for me and my team.

Thanks & Lots of Regards
Harish Rathore

1 Like

Hello @harishrathore,

You’re Welcome. :slightly_smiling_face:

We’re glad that we were able to assist you and you found both of them useful in your scenario.

Thanks and Warm Regards,
Harsh