Forum Discussion
Power Query Merge based on conditions
1) Split your second table into three queries where each is filtered by your conditions (i.e., one table has customer, all, all; another has customer, state, all; etc.). Disable load on these tables
2) Now that you have a table for each scenario, you can do the appropriate correspoding merge. You can set the key(s) or remove duplicates to speed up merge.
3) Once joined, you can combine (append) the tables back together. This gives you all the invetory rows with warehouses. Remove warehouse rows that don't have any corresponding inventory. Don't load
4) use an antijoin with original Inventory table with #3 to get all your inventory rows without warehouses. Don't load
5) Combine (append) tables from #3 and #4 to get your complete InventoryWarehouses table. Load this table
The flow of all this will look like:
Queries:
CustomerWarehouses:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVnDO11PSUXJJzStLLQIynP2BRLihUqwOsrRTfmlOCpK8EVjexdUNKp+YkwMnw43Bku4eniiS/h4Ig9Hlwh2hhsYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, City = _t, State = _t, Warehouse = _t])
in
Source
Inventory:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNlHSUXLKKU1VSMrMTgWyHZ2cFZzz9YAsl9S8stQiIMPZXylWB6zYFMgLSk3BprakKD+zBMjy9YQpNgPy3ItSU/NgyiMio6DK/VLLFSLzi7JBzEiYenM0l7h7eEKVO+eklqXmJOalANn+HjD1FqiOcXF1gyoPSazMyS9CcYslulsQhqO5JRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Serial No" = _t, Product = _t, Customer = _t, City = _t, State = _t])
in
Source
CustomerAllAll:
let
Source = CustomerWarehouses,
Filter = Table.SelectRows(Source, each ([State] = "all") and ([City] = "all")),
Distinct = Table.Distinct(Filter, {"Customer"}),
MergeInv = Table.NestedJoin(Distinct, {"Customer"}, Inventory, {"Customer"}, "Inventory", JoinKind.LeftOuter)
in
MergeInv
CustomerStateAll:
let
Source = CustomerWarehouses,
Filter = Table.SelectRows(Source, each ([City] = "all") and ([State] <> "all")),
Distinct = Table.Distinct(Filter, {"Customer", "State"}),
MergeInv = Table.NestedJoin(Distinct, {"Customer", "State"}, Inventory, {"Customer", "State"}, "Inventory", JoinKind.LeftOuter)
in
MergeInv
CustomerStateCity:
let
Source = CustomerWarehouses,
Filter = Table.SelectRows(Source, each ([State] <> "all") and ([City] <> "all")),
Distinct = Table.Distinct(Filter, {"Customer", "City", "State"}),
MergeInv = Table.NestedJoin(Distinct, {"Customer", "State", "City"}, Inventory, {"Customer", "State", "City"}, "Inventory", JoinKind.LeftOuter)
in
MergeInv
InventoryWithWarehouses:
let
Source = Table.Combine({CustomerStateCity, CustomerStateAll, CustomerAllAll}),
RemoveWHCols = Table.RemoveColumns(Source,{"Customer", "City", "State"}),
ExpandInv = Table.ExpandTableColumn(RemoveWHCols, "Inventory", {"Serial No", "Product", "Customer", "City", "State"}, {"Serial No", "Product", "Customer", "City", "State"}),
RemoveEmptyWH = Table.SelectRows(ExpandInv, each ([Customer] <> null))
in
RemoveEmptyWH
InventoryNoWarehouses:
let
Source = Table.NestedJoin(InventoryWithWarehouses, {"Serial No", "Product", "Customer", "City", "State"}, Inventory, {"Serial No", "Product", "Customer", "City", "State"}, "Inventory", JoinKind.RightAnti),
RemoveWHCols = Table.RemoveColumns(Source,{"Warehouse", "Serial No", "Product", "Customer", "City", "State"}),
ExpandInv = Table.ExpandTableColumn(RemoveWHCols, "Inventory", {"Serial No", "Product", "Customer", "City", "State"}, {"Serial No", "Product", "Customer", "City", "State"})
in
ExpandInv
InventoryWarehouses:
let
Source = Table.Combine({InventoryWithWarehouses, InventoryNoWarehouses})
in
Source
Result: