Forum Discussion

abbr's avatar
abbr
New Member
4 years ago
Solved

Condintional Grouping in PowerQuery and Remove Items In and Out

Hi,all:      I have a warehouse,like warehouse1,get items in and out ,the amount of in item maybe different from out item ,but the material is the same.I have a table like pic left,want to get it li...
  • Vijay_A_Verma's avatar
    4 years ago

    What is the logic behind this. Shouldn't it be A,B,C with amount of 501 rather than B,C with amount of 501 as 1 amount is left after movement of A? However assuming your output is correct

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQoHEY5AbGhgACQ985RidaKVjGAyTkBshCxjDJNxBmJjZBkTZNN0LS2BpH9piVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, WareHouse = _t, Item = _t, Amount = _t, State = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"WareHouse", type text}, {"Item", type text}, {"Amount", Int64.Type}, {"State", type text}}),
        #"Grouped Rows1" = Table.Group(#"Changed Type", {"WareHouse", "Item"}, {{"Temp", each Text.Combine([State],",")
    , type nullable text}}),
        #"Filtered Rows" = Table.SelectRows(#"Grouped Rows1", each not Text.Contains([Temp], "Out")),
        #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Temp"}),
        #"Grouped Rows2" = Table.Group(#"Removed Columns", {"WareHouse"}, {{"Item", each Text.Combine([Item],", "
    ), type nullable text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"WareHouse"}, {{"Amount", each List.Sum([Amount]), type nullable number}}),
        #"Merged Queries" = Table.NestedJoin(#"Grouped Rows2", {"WareHouse"}, #"Grouped Rows", {"WareHouse"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Amount"}, {"Amount"})
    in
        #"Expanded Grouped Rows"