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.
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
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.
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.