Forum Discussion
nikz26
8 years agoRegular Visitor
Lookup based on multiple criteria
Hi Guys, I need help on how to lookup using multiple criteria. I need to update the "Status" of Sheet1 based on the criteria from Sheet2. The criteria is if the employee falls on a specific date ...
- 8 years ago
You can add a column in the query editor that checks the conditions like this:
Table.SelectRows(Sheet2, (Sheet2) => Sheet2[START DATE]<=[Date] and Sheet2[STOP DATE] >= [Date] and Sheet2[ID]=[ID])[Status]{0}If this is too slow, you have to use a function like this:
(Table1 as table, DateColumn1 as text, ListJoinColumns1, TableLookup as table, DateFrom as text, DateUntil as text, ListJoinColumns2, LookupField as text) => let #"Merged Queries" = Table.NestedJoin(Table1,ListJoinColumns1,TableLookup,ListJoinColumns2,"Lookup",JoinKind.LeftOuter), #"Added Custom" = Table.AddColumn(#"Merged Queries", "Custom", each Record.Field(Table.SelectRows([Lookup], (Lookup) => Record.Field(Lookup, DateFrom)<=Record.Field(_, DateColumn1) and Record.Field(Lookup, DateUntil)>=Record.Field(_, DateColumn1)){0}, LookupField)), Result = Table.RemoveColumns(#"Added Custom", {"Lookup"}) in Resultname it "fnEventDurationMultiple" and you can invoke it like this:
fnEventDurationMultiple(Sheet1, "Date", {"ID"}, Sheet2, "START DATE", "STOP DATE", {"ID"}, "Status")
ImkeF
8 years agoCommunity Champion
You can add a column in the query editor that checks the conditions like this:
Table.SelectRows(Sheet2, (Sheet2) => Sheet2[START DATE]<=[Date] and Sheet2[STOP DATE] >= [Date] and Sheet2[ID]=[ID])[Status]{0}If this is too slow, you have to use a function like this:
(Table1 as table, DateColumn1 as text, ListJoinColumns1, TableLookup as table, DateFrom as text, DateUntil as text, ListJoinColumns2, LookupField as text) =>
let
#"Merged Queries" = Table.NestedJoin(Table1,ListJoinColumns1,TableLookup,ListJoinColumns2,"Lookup",JoinKind.LeftOuter),
#"Added Custom" = Table.AddColumn(#"Merged Queries", "Custom", each Record.Field(Table.SelectRows([Lookup], (Lookup) => Record.Field(Lookup, DateFrom)<=Record.Field(_, DateColumn1) and Record.Field(Lookup, DateUntil)>=Record.Field(_, DateColumn1)){0}, LookupField)),
Result = Table.RemoveColumns(#"Added Custom", {"Lookup"})
in
Resultname it "fnEventDurationMultiple" and you can invoke it like this:
fnEventDurationMultiple(Sheet1, "Date", {"ID"}, Sheet2, "START DATE", "STOP DATE", {"ID"}, "Status")