The Switch function evaluates a formula and tries to find it in the list of matches defined. It stops in the first match that validates the condition and returns the corresponding value or the default value if you provide one. The basic structure of the Switch function is Switch(<condition>, <match>, <value if match>, .... <default value if no match> )

If by mistake, you add the same value twice, Switch will only consider the first one, so be sure that your Switch cases are always unique. For example, Switch(1+1,2, "Correct 1",2, "Correct 2","Incorrect") will return “Correct 1” and ignore the remaining values.

You can argue that you can get the same result with an If function. Although they serve different purposes, they follow similar patterns. They both allow for multiple values and stop at the first available occurrence that matches the condition. The issue with using an If function is that you would need to replicate the condition in each validation step. If the condition is the same, then is much more useful to use the Switch function, because you only define it once and validate for potential occurrences of that condition.

Limitations

  1. You have to describe all combinations of match and value if match, so you can’t have an array of matches and values and use it to validate the condition.

Recommendations:

  1. Prefer ordered lists in the Switch. This will allow you to keep your list organized and easily searchable. For example. Switch(fruit, "AP", "Apple", "BA", "Banana", "Other")
  2. Make the `Switch` exhaustive, meaning that you should always create a default value. If you see the default value, you know that something’s not correct. You can be missing value in the list or have an issue in the condition, either way; you’ll know.
  3. Test your Switch. This goes for every part of your code, but you should test your Switch statements on how your app behaves for each value. Validating every outcome will help you understand if the formula makes sense and double-check if matches defined are necessary or if any is missing.

Localization

  1. Please note that formulas may have localization-based differences. For example, you should write Switch(fruit, "AP", "Apple", "BA", "Banana", "Other") with “,” separating each of the arguments, but if your localization is Portugal, you should use “;” instead.

Sources:

If and Switch functions in PowerApps

Back to the PowerApps Function Reference

Featured Image by Javier Allegue Barros on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

Leave a Reply

%d bloggers like this: