Thanks for including sufficient detail in your question.
You can try the code below, it gives me your expected output. I don't know how performant this code will be if you're working with a lot of rows since I can't test it. It might better to use Table.NestedJoin
instead of Table.Contains
, although Table.Contains
probably stops checking once a match has been found, which is better in this scenario.
I don't think you said anything in your question about checking whether the value for SALES CODE
in TABLE2 is in any of the values for columns SALES CODE 1
, SALES CODE 2
, SALES CODE 3
in TABLE1. I think that's what this bit SUMPRODUCT((TABLE1!B$2:B$18=C2)*(TABLE1!E$2:G$18=F2))>0
in your Excel formula is doing.
Obviously, you will need to replace table1
and table2
in the code below with your own code/data, as mine are just hard coded examples.
I recommend that you try it with as much real data as you can just to ensure that the code works and that the STATUS
column always contains the value you expect it to contain.
You can turn the TRUE/FALSE
values into YES/NO
with Table.TransformColumns
. I've kept them as TRUE/FALSE
for now.
let
table1 = Table.FromRows({
{"Avocado", "3100", 1, "EN", "EU01", "US", "UK"},
{"Avocado", "3200", 1, "EN", "EU01", "US", "UK"},
{"Avocado", "3300", 1, "EN", "EU01", "US", "UK"},
{"Avocado", "4500", 1, "EN", "EU01", "US", "UK"},
{"Avocado", "8900", 1, "EN", "EU01", "US", "UK"},
{"Star Fruit", "WEST", 1, "EN", "INDIA01", "INDIA02", "NA"},
{"Star Fruit", "NORTH", 1, "EN", "INDIA01", "INDIA02", "NA"},
{"Star Fruit", "EAST", 1, "EN", "INDIA01", "INDIA02", "NA"},
{"Star Fruit", "SOUTH", 1, "EN", "INDIA01", "INDIA02", "NA"},
{"Banana", "KP1", 1, "EN", "XX1", "NA", "NA"},
{"Banana", "PP1", 1, "EN", "XX2", "NA", "NA"},
{"Apple", "444", 1, "EN", "BAN03", "NA", "NA"},
{"Orange", "555", 1, "EN", "BAN03", "NA", "NA"},
{"Orange", "555", 1, "EN", "BAN04", "NA", "NA"},
{"Orange", "555", 1, "EN", "BAN05", "NA", "NA"},
{"Plum", "123", 1, "EN", "BAN03", "NA", "NA"},
{"Watermelon", "", 1, "EN", "EU01", "US", "UK"}
}, type table [FRUIT LIST = text, AREA CODE = text, QTY = Int64.Type, LANGUAGE = text, SALES CODE 1 = text, SALES CODE 2 = text, SALES CODE 3 = text]),
table2 = Table.FromRows({
{"123", "Avocado", "3100", 100, "EN", "EU01"},
{"124", "Avocado", "3100", 100, "EN", "US"},
{"125", "Avocado", "3100", 100, "EN", "UK"},
{"126", "Avocado", "WEST", 100, "EN", "UK"},
{"127", "Avocado", "3100", 1, "EN", "UK"},
{"128", "Avocado", "3100", null, "EN", "UK"},
{"129", "Avocado", "3100", 100, " ", "UK"},
{"130", "Avocado", "3100", 100, "EN", "INDIA01"},
{"131", "Avocado", "3100", 100, "EN", ""},
{"234", "Star Fruit", "WEST", 200, "EN", "INDIA01"},
{"235", "Star Fruit", "NORTH", 100, "EN", "INDIA01"},
{"236", "Star Fruit", "EAST", 43, "EN", "INDIA01"},
{"237", "Star Fruit", "SOUTH", 56, "EN", "INDIA01"},
{"238", "Star Fruit", "WEST", 78, "EN", "INDIA02"},
{"239", "Star Fruit", "NORTH", 99, "EN", "INDIA02"},
{"240", "Star Fruit", "EAST", 133, "EN", "INDIA02"},
{"241", "Star Fruit", "SOUTH", 45, "EN", "INDIA02"},
{"56", "Watermelon", "", 100, "EN", "UK"}
}, type table [ITEM = text, FRUIT LIST = text, AREA CODE = text, QTY = Int64.Type, LANGUAGE = text, SALES CODE = text]),
statusColumn = Table.AddColumn(table2, "STATUS", (currentRow) =>
// Check quantity first since it should be a quick/cheap comparison, especially with short-circuit evaluation.
currentRow[QTY] <> null and (if currentRow[FRUIT LIST] = "Banana" then currentRow[QTY] = 1 else currentRow[QTY] > 1)
// FRUIT LIST && AREA CODE matches any TABLE1 row?
and Table.Contains(table1, currentRow[[FRUIT LIST], [AREA CODE]], {"FRUIT LIST", "AREA CODE"})
// AREA CODE && LANGUAGE matches any TABLE1 row?
and Table.Contains(table1, currentRow[[AREA CODE], [LANGUAGE]], {"AREA CODE", "LANGUAGE"})
// AREA CODE && SALES CODE matches any TABLE1 row?
and Table.Contains(table1, currentRow, (table1row as record, currentRow as record) =>
table1row[AREA CODE] = currentRow[AREA CODE]
// SALES CODE needs custom treatment as there is no SALES CODE column in TABLE1
and List.Contains(Record.FieldValues(table1row[[SALES CODE 1], [SALES CODE 2], [SALES CODE 3]]), currentRow[SALES CODE])
)
, type logical)
in
statusColumn