List.ContainsAny
Returns true if any of several values appears in a list.
List.ContainsAny(list, values, optional equationCriteria)Returns: A logical value, true or false.
What it does
List.ContainsAny checks a list against a second list of values and returns true if at least one of them is present. It is a quick way to flag a row or a batch when it has any of several codes, such as any of three restricted payment methods, without writing one test per code.
Syntax and arguments
list: the list to search.values: a list of the values to look for.equationCriteria(optional): how values are compared, as in List.Contains.
Example
A memo field on bills is checked for any of three words that signal a possible duplicate or a rush payment. The batch is flagged if any word appears.
let
BatchCodes = {"STD", "URGENT", "STD"},
Watch = {"URGENT", "MANUAL", "CHECK-ONLY"},
NeedsReview = List.ContainsAny(BatchCodes, Watch)
in
NeedsReview
NeedsReview is true, because "URGENT" is in the batch. The test is one line, and the watch list can be changed without rewriting the logic.
Example: a second, different use
Checking a list of account codes against a list of restricted accounts, as one of several control tests on a journal batch.
Things to watch
- It is a yes or no answer. To see which values matched, use List.Intersect.
- Exact matching is the default, so use Comparer.OrdinalIgnoreCase for text typed by people.
- An empty values list returns false, which can hide a missing watch list. Check its count.
- Keep the watch list in a separate query or table, so people can update it without changing the code.
In business terms
Flagging journal batches, scanning memo fields for keywords, and checking payments against lists of blocked or restricted codes all use this pattern. Document the watch list as part of the control.