Forum Discussion

jcastr02's avatar
jcastr02
Post Prodigy
1 year ago
Solved

:Stacking Data

I have a list of stores that are closing along with the stores that will recieve the volume of that closing store.  There are three recieving stores each with their base volume and added volume.  I"d...
  • p45cal's avatar
    1 year ago

    See linked-to Excel workbook below.

    My query is probably a bit amateurish but seems to work.

    I changed the column headers of your source data at row 2 since they didn't seem to go in sequence.

    Result query at cell A31.

     

    Version including source data here for pasting in Power Query Advanced Editor:

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZGxDsIwDET/JTNSYjtJk5mdjanqUKkVTIgBid/HvoTCUMFQK2edX5zrODpO7uCIfUieA0SIXrWKqOK0PrUOrEVk0FphtxNnc4t+JQStyWRy02F0ZC6uXlF70PN9mR/rgklDFIMACRCF1HVsy1ktANcIlw+yCz5e59sFYAJJsi2eGENkbcH+dh+RNQS+2uDS4fwjij6WgedtZ26r4g62dmxPIZCHbLD0BdsjtzlCEMXwQBZzoEkSt7CJP0H/+3ttThjPwzEDidnyvq1nr1FMLw==", BinaryEncoding.Base64), Compression.Deflate)),{"Closing Store #", "Final Store Closure Date", "Reporting Week" , "Change in Status" , "Receiving Store #1" , "Receiving Store #1 BASE Volume", "Receiving Store #1 Added Volume", "Receiving Store #2", "Receiving Store #2 BASE Volume" , "Receiving Store #2 Added Volume", "Receiving Store #3" , "Receiving Store #3 BASE Volume", "Receiving Store #3 Added Volume"}),
    
    ChangedType = Table.TransformColumnTypes(Source,{{"Final Store Closure Date", type date}, {"Reporting Week", type date}},"en-GB"),
    UnpivotedColumns = Table.UnpivotOtherColumns(ChangedType, {"Closing Store #", "Final Store Closure Date", "Reporting Week", "Change in Status"}, "Attribute", "Value"),
    SplitColumn = Table.SplitColumn(UnpivotedColumns, "Attribute", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Attribute.1", "Attribute.2"}),
    Trimmed = Table.TransformColumns(SplitColumn,{{"Attribute.2", Text.Trim, type text}}),
    ReplaceValue = Table.ReplaceValue(Trimmed,null,"Recieving store#",Replacer.ReplaceValue,{"Attribute.2"}),
    GroupRows = Table.Group(ReplaceValue, {"Closing Store #", "Final Store Closure Date", "Reporting Week", "Change in Status", "Attribute.1"}, {{"grp", each _, type table [#"Closing Store #"=number, Final Store Closure Date=nullable date, Reporting Week=nullable date, Change in Status=text, Attribute.1=nullable text, Attribute.2=text, Value=number]}}),
    InvokedFunction = Table.AddColumn(GroupRows, "fnStuff", each ((tbl)=> Table.Pivot(tbl, List.Distinct(tbl[Attribute.2]), "Attribute.2", "Value"))([grp])),
    RemoveColumns = Table.RemoveColumns(InvokedFunction,{"grp", "Attribute.1"}),
    ExpandedFn = Table.ExpandTableColumn(RemoveColumns, "fnStuff", {"Recieving store#", "BASE Volume", "Added Volume"})
    in
    ExpandedFn

     

     

    Link to workbook: https://app.box.com/s/aj3o10p4mun9nxmlszcyvwn970hqwhzx