In this blog post we will learn what is Switch function in PowerApps and how to use it with examples and when to work with PowerApps Switch function instead of IF conditions.
What is Switch function in PowerApps?
- Switch function in PowerApps is a way to evaluate a single condition against multiple possible matches in alternative to an IF function.
- The Switch function preferred to the IF function because it is using less code that makes your code easier to read and maintain.
- Switch function in PowerApps specifies whether or not the result matches any value in a set. If a match is discovered, the corresponding value is returned. If no match is found, the default value is returned.
- If and Switch are very similar, but you should use the best function for your situation
- Use If for a single condition
- Use If for multiple unrelated conditions
- Use Switch to evaluate a single condition against multiple possible matches “preferred Switch function to If function”
PowerApps Switch function Syntax
Switch( Formula, Match1, Result1 [, Match2, Result2, ... [, DefaultResult ] ] )
- Switch: is the function name
- Formula: This is Required is the formula to evaluate for matches and it is evaluated only once.
- Match: This is Required Values to compare with the result from Formula
- Result: This is Required value to return when an exact match is found
- DefaultResult : This is Optional this value is returned if none of the given matches found.
The Switch function in PowerApps will return result1 if match1 evaluates to true and result 2 if match 2 evaluates to true and so on
It returns the DefaultResult if no Match is true and if you didn’t define a DefaultResult and no match is true then it will return blank.
PowerApps Switch function Examples
EX1: in this example we will use the PowerApps Switch function to change the fill color of a label according to user input if user enter 20 then the label will colored Red, 50 then the label will colored Yellow, 80 then the label will colored Green otherwise then the label will colored Blue that we called the Default result, the value that is returned if none of the given matches Match.
Switch(
TextInput3.Text,
Text(20),
Red,
Text(50),
Yellow,
Text(80),
Green,Blue
)
EX2:
In the following example I will filter a travel Request collection according to a context variable “UserRecords” if it’s value is equal to “StaffmemberRecords” then it will display only the record that created by this staff member, and if the variable is equal to “LineManagerRecords” it will show all records created by the lie manager and the staff members.
Switch(
UserRecords,
"StaffmemberRecords",
Filter(
'Travel Request',
'Created By'.Email = User().Email
),
"LineManagerRecords",
Filter(
colRequests,
'Approval Status'.Value <> "Draft"
))
Conclusion
If and Switch are the two primary conditional operators in Power Apps. The Switch function compares an input value to multiple conditions based on equality. and you can use If for a single condition or for multiple unrelated conditions
and use Switch to evaluate a single condition against multiple possible matches “preferred Switch function to If function”.
See Also
- Nested Collection In PowerApps :3 Level
- How To Use PowerApps DateDiff Function?
- PowerApps Parse JSON Example
- Show Internet Base Time In PowerApps |
- Canvas App Collections
- How To Overcome PowerApps Delegation Limit?
- How To Use PowerApps Value Function?
- Share PowerApps With External Users / Guest Users
- How to use DAX DAY Function in Power BI?
- How to use Date function in DAX Power BI?
- How to use Power BI DAX CALENDERAUTO?
Join us
- Subscribe to Power Platform Geeks.
- Register to Saudi Arabia Power Platform User Group.
Need Help
- Have a related question? Please ask it at deBUG.to Community.