Introduction
Evaluation of a variable against multiple values is common in Power Apps for a variety of scenarios, including navigation, visibility control, validations, filtering, and the execution of specific actions.
Multiple OR (||) conditions are typically used to accomplish this, which can make the code repetitive and more difficult to read as the number of conditions increases.
Power Apps in operator makes it easier to check a value against a list in a way that is easier to understand and maintain. When multiple value comparisons are required, this method can be used in a variety of use cases to help keep formulas clean, scalable, and simple to manage.
Traditional Approach (Ex: Navigation)
If(
gblTabID= 1 || gblTabID= 2 || gblTabID = 3,
Navigate(screen1),
gblTabID= 4 || gblTabID= 5 || gblTabID= 6,
Navigate(screen2),
gblTabID = 7 || gblTabID = 8,
Navigate(screen3)
);
Issues with this approach
Why is this superior?
Groups related values logically in one place.
Makes the intent of the condition clearer.
reduces the number of variables that are used repeatedly. Simple to update: simply add or remove values from the array. Improves readability, especially when conditions grow larger
Optimized Approach
Using the 'in' operator simplifies the logic by allowing you to compare a single value against a collection (array) of values, rather than writing multiple OR conditions.
//Optimsed Version 1
If(
gblTabID in [1,2,3],
Navigate(screen1),
gblTabID in [4,5,6],
Navigate(screen2),
gblTabID in [7,8],
Navigate(screen3)
);
// Optimsed Version2
Navigate(
Switch(
true,
gblTabID in [1,2,3], screen1,
gblTabID in [4,5,6], screen2,
gblTabID in [7,8], screen3
)
)
Explanation
[1,2,9], [5,6], [7,8] are arrays (lists of values)
The in operator checks whether gblTabID exists inside that array
On the internal level, multiple comparisons such as: gblTabID = 1 || gblTabID = 2 || gblTabID = 3, etc.
You define the variable once, rather than multiple times, and then compare it to a group of values.
Conclusion
In Power Apps, a clean and effective method for handling multiple value comparisons is to use the in operator with arrays. Instead of repeating OR (||) conditions, values can be grouped into arrays like [1,2,9], making the logic more structured and easier to read.
This method can be used in a variety of situations, including visibility control, validations, filtering, and other forms of conditional logic, and it is not limited to navigation. By leveraging arrays with the in operator, formulas become more scalable, maintainable, and easier to update as requirements grow.