Here’s a detailed exploration of the “in” operator in Power Apps, which I recently researched thoroughly.
The “in” Operator
In Power Fx, the “in” operator is used to check whether a specified value exists within a text string or a collection (such as an array or table). The expression always returns either true or false.
Let’s dive into how to use it.
Basic Usage of the “in” Operator
As mentioned earlier, the “in” operator can be used with “text strings,” “arrays,” and “tables.”
Text Strings
When used with text strings, the “in” operator performs partial string matching.
If the first specified string is contained within the second string, it returns true,
If it’s not contained, it returns false.
"pow" in "Power Apps" // true
"wop" in "Power Apps" // false
"" in "Power Apps" // true
// Note: An empty string is considered to be contained within all strings.
Arrays
When using the “in” operator with arrays, it checks whether a specified element exists within the array.
Note: In Power Apps, “arrays” can be treated as “tables” with a Value column, so the following also returns true.
Since it only checks for exact element matches in the array, unlike partial string matching, the following returns false.
If you want to check “whether there are any elements containing ‘a'”, you would use something like this.
"aaa" in ["aaa", "bbb", "ccc"] // true
"AAA" in ["aaa", "bbb", "ccc"] // true
"a" in ["aaa", "bbb", "ccc"] // false
{Value:"aaa"} in ["aaa", "bbb", "ccc"] // true
Table (Including Collection)
Let’s try using the “in” operator with the following table.
ClearCollect(
colSample,
{text:"aaa", num:0},
{text:"bbb", num:1},
{text:"ccc", num:2}
)
This also works as an “element” search, and an error occurs when comparing elements with different schemas (columns).
Therefore, when checking elements using the “in” operator, you need to match the schemas.
As expected, if even one column value differs, it returns false.
"aaa" in colSample // Error
{text:"aaa"} in colSample // Error
{text:"aaa", num:0} in colSample // true
{text:"AAA", num:0} in colSample // false
{text:"aaa", num:1} in colSample // false
{} in colSample // false
Differences Between “in” and “exactin”
The key difference between “in” and “exactin” is whether they are case-sensitive.
With “exactin”, if there’s a difference in letter case, it returns false.
"pow" in "Power Apps" // true
"pow" exactin "Power Apps" // false
"Pow" exactin "Power Apps" // true
"" exactin "Power Apps" // true
By the way, the “in” operator is also case-insensitive when searching arrays.
"AAA" in ["aaa", "bbb", "ccc"] // true
"AAA" exactin ["aaa", "bbb", "ccc"] // false
Using with Filter Function
Since the “in” operator is frequently used with the Filter function, let’s explore how they work together.
As an example, let’s apply the “in” operator with Filter to the following collection.
ClearCollect(
colSample,
{text1:"aaa", text2:"xxx", num:0},
{text1:"abc", text2:"xyz",num:1},
{text1:"bbb", text2:"yyy",num:1},
{text1:"bca", text2:"yzx",num:2},
{text1:"ccc", text2:"zzz",num:3}
)
When using “in” as the second argument in a Filter function, you can perform partial matching searches on text columns.
Additionally, by placing a table column on the left side of “in”, you can extract rows where that column contains any of the specified values.
If you want to search across multiple columns, the expression would look like this.
Note: Sometimes you might see code written like this to use a single “in” operator,
However, this can lead to false positives (although such use cases might be rare in practice).
About Delegation
Finally, let’s discuss delegation. The ability to delegate depends on the data source.
SharePoint
Since SharePoint doesn’t support delegation for the “in” operator, it’s recommended to use it only with master data or other sources with limited records.
Dataverse
Dataverse supports delegation for the “in” operator,
And you can even reverse the left and right sides of the expression.
However, note that even in Dataverse, the “exactin” operator cannot be delegated.
Related Articles
コメント