How To Use PowerApps Switch Function (with examples)

Power Apps Switch Function

The PowerApps Switch function is basically a more elegant way to compare multiple conditions in alternative to an IF function. Everything done with the Switch function can be done with the IF function as well. The Switch function makes your code easier to read and understand and therefore easier to maintain. I want to encourage you to use the Switch function whenever applicable.

To provide some guideline when to use Switch vs IF function, here is how I decide when to use which function.

When To Use PowerApps Switch Function

As soon as you are having two or more conditions checking for equality within an IF function on one value, I would consider using the Switch function.

Why the Switch function should be preferred to the IF function, if the mentioned criteria are met?

  • Less code
  • Easier to read and understand
  • Easier to maintain

Example Use Case for PowerApps Switch Function

To get a better feel for when to use the Switch function and what are the benefits, let’s take a look at a concrete example.

Let’s imagine we are developing a PowerApp to sell tickets for public transport and we are having the following price structure.

Ticket TypePrice
Single Ride Ticket$3
Pay-Per-Ride$5
7-Day Unlimited Ride$32
30-Day Unlimited Ride$127

The user can select the type of ticket he wants to buy. Based on the selected ticket type, the price should be displayed. The result will look like this:

PowerApps Switch Function
Drop Down to demonstrate Switch function

I create a Drop Down field and set its “Items” property to:

["Single Ride Ticket","Pay-Per-Ride","7-Day Unlimited Ride","30-Day Unlimited Ride"]

I add two Labels: one for displaying “Ticket price:” and one for displaying the price itself. The “Text” property of the Label for the price makes use of the Switch function:

"$" & Switch(
    TicketTypesDropdown.Selected.Value,
    "Single Ride Ticket",3,
    "Pay-Per-Ride",5,
    "7-Day Unlimited Ride",32,
    "30-Day Unlimited Ride",127
)

To illustrate why although it is possible to achieve the same result with the IF function, Switch is the better choice. Let me show you the same implementation with IF.

"$" & If(
    TicketTypesDropdown.SelectedText.Value = "Single Ride Ticket",3,
    TicketTypesDropdown.SelectedText.Value = "Pay-Per-Ride",5,
    TicketTypesDropdown.SelectedText.Value = "7-Day Unlimited Ride",32,
    TicketTypesDropdown.SelectedText.Value = "30-Day Unlimited Ride",123
)

Within the Switch function you use “TicketTypesDropdown.SelectedText.Value” once. This is leading to less code and better readability. In case you need to replace the Drop Down with something else, with the Switch you have to change only one line.

PowerApps Switch Function in Detail

Syntax

Switch( Formula, Match1, Result1 [, Match2, Result2, ... [, DefaultResult ] ] )

Input Parameter

  • Formula (mandatory): The value you want to check for equality.
  • Match1 (mandatory): A value you want to compare the Formula parameter against.
  • Result1 (mandatory): The outcome if the Match1 is matching. (You can have multiple pairs of Match and Result)
  • DefaultResult (optional): The outcome if none of the given matches match.

Return value

  • Result1, if Match1 evaluates to true, Result2 if Match2 is true, …
  • DefaultResult, if DefaultResult is defined and no Match is true.
  • Blank, if there is no DefaultResult defined and no Match is true

Example: Switch function with default value

Imagine you want to display the level of a trainee based on the number of lessons they have taken.

Switch(NumberOfLessons,
    0, "Beginner",
    1, "Beginner",
    2, "Intermediate",
    3," Professional",
    "Expert"
)

This will result in:

  • 0,1: Beginner
  • 2: Intermediate
  • 3: Professional
  • Anything else: Expert

2 thoughts on “How To Use PowerApps Switch Function (with examples)”

  1. What does the “$” mean? In some basic languages its a symbol for a string data type, i.e., “F$” would declare F as a string variable. But what does it mean in your example?
    Thank you,
    Kevin

    1. Hi Kevin,

      in the examples it just means dollar. Since the result of the switch statements in the examples are prices the resulting string is like $5 for ‘Pay-Per-Ride’.

      But you are correct, the dollar sign often has a meaning in programming languages. By the way for PowerApps too. You can do something like this:

      $"2+3 = {2+3}" => 2+3 = 5

Leave a Comment

Your email address will not be published. Required fields are marked *