The Power Apps Switch function evaluates a single condition against multiple possible matches.ย If a match is found, a corresponding value is returned. If no match is found, a default value is returned.
It is similar to the If function, but it can be more efficient and easier to read for complex conditions.
Syntax:
Switch(ย Formula,ย Match1,ย Result1ย [,ย Match2,ย Result2, … [,ย DefaultResultย ] ] )
- Formulaย – Required. Formula to evaluate for matches. This formula is evaluated only once.
- Match(s)ย – Required. Values to compare with the result fromย Formula. If an exact match is found, the correspondingย Resultย is returned.
- Result(s)ย – Required. The corresponding value to return when an exact match is found.
- DefaultResultย – Optional. If an exact match isn’t found, this value is returned. If you don’t specify this argument,ย blankย is returned.
Return value, the Switch function will evaluate the Formula and compare it to each Match value in turn. If a match is found, the corresponding Result value is returned. If no match is found, the DefaultResult value is returned.
Using Switch function
Example 1:
As you can see, here we have a gallery which contains employee’s details such as employee Id, employee name and department.
The following example shows how to use the Switch function to evaluate the Department Name and return short name of each department for match value in gallery.
For this, first we will add a Text label control inside gallery and set it’s Text property to formula.
Switch( ThisItem.Department.Value, "Finance", "F", "Data Engineering", "DE", "Supply Chain", "SC", "Data & Analytics", "DA", "N/A" )
Now, you can see it evaluates the formula ThisItem.Department.Value, which returns the current selected department name in gallery and compare it to each matching value in turn.
If match is found then it returns the result value that is short name of department against matching value.
Example 2:
The following example shows how to use the Switch function to evaluate the Month Number select in dropdown list and return a Month Name for corresponding matching month number.
As you can see, here we have a dropdown control named drp_monthno contains a month numbers.
Let’s add one Text label control to display month name for selected month number in drop down.
ย Now, set the Text property of Text label control to formula given below.
Switch ( Value(drp_monthno.SelectedText.Value), 1, "January", 2, "Feburary", 3, "March", 4, "April", 5, "May", 6, "June", 7, "July", 8, "August", 9, "September", 10, "October", 11, "Novemeber", 12, "December", "N/A" )
You can see, it returns the month name for selected month number in dropdown list.
Use cases of the Switch Function
- You can use Switch function when you have to evaluate a single condition against multiple possible matches.
- Use the Switch function when you want to make your code more efficient and readable.
Also Read..
ShowColumns function in Power Apps
Reset multiple control values and all the controls within a form
Calendar and Clock function in Power Apps
Submit multiple records to a data source