Formula - How to use the IF and Switch functions - 3 common examples

This post summarises three frequent questions that I often see about conditional operations, and includes sample formula. In this post, we'll cover how to match a single input value against numeric bandings, how to match a single input value against a list of acceptable values, and how to match a single input value against a list of exact matches.

The If and switch statements are the two primary conditional operators in Power Apps. Confusion typically arises when app builders attempt to use the Switch function rather than If, or vice versa. For reference, the link to the online documentation is here:
For this post, we'll demonstrate the syntax using the following dataset of student exam grades.

1. Determine where an input value belongs within a set of numeric/bounded ranges

The first common use case scenario is to determine where an input value sits within a set of ranges, or bandings. The example here would be to convert the numeric score to a grade, using the following rules:

  • A - Score greater than 80
  • B - Score between 60 and 70
  • C - Score between 50 and 60
  • D - Score between 40 and 50
  • E - Score between 30 and 40
  • Fail - Score < 30
The formula that carries out this task looks like this (if we were accepting the input value from a text input control called txtInputValue):

With({input:Value(txtInputValue.Text)},
If(input >= 80, "A",
input >= 70, "B",
input >= 60, "C",
input >= 50, "D",
input >= 40, "E",
"Fail"
)
)
With this formula, we substitute 'input' with the actual input value that we want to use. As an example, the screenshot beneath illustrates how we would refer to ThisItem.Score from a gallery control.


To clarify how the If function works, we pass matching sets of conditions and output values. The If function returns the first output value that matches a condition. When the function finds a matching condition, the execution of the If function terminates and Power Apps will not continue checking the remaining conditions that are specified in the call to the function.

If there are no matching conditions, the If function returns the final argument that we specify.

2. Determine if an input value belongs within a set of numeric/bounded ranges

The second common use case scenario is to compare an input value against a list of values. The example here would be show a category label, based the following rules:

  • Sciences - Apply this label when exam name matches "Maths", "Physics", "Chemistry".
  • Arts - Apply this label when exam name matches  "Music", "Art", "Dance".
  • Other - Apply this label for all other exams

The formula that carries out this task looks like this. Again, we substitute 'input' with the actual input value that we want to use.
With({input:txtInputValue.Text},
If(input in ["Maths", "Physics", "Chemistry"], "Sciences",
input in ["Music", "Art", "Dance"], "Arts",
"Other"
)
)

3. Testing against sets of values (existance)

In the final use case scenario, we'll examine the use of the Switch function. Unlike the examples that we've seen earlier, the Switch function is designed to match an input value against multiple conditions based on equality.

The example here would be to convert a numeric 'level' value to a label, based the following rules:

  • 1 - Apply the label "Beginner"
  • 2 - Apply the label "Intermediate"
  • 3 - Apply the label "Advanced"
  • Other values - Apply the label "Unknown"

Switch(Value(txtInputValue.Text),
1, "Beginner",
2, "Intermediate",
3, "Advanced",
"Unknown"
)

To clarify how the Switch function works, the first value we pass defines the input value. Next, we pass pairs of values that specify the match condition and the output value. The Switch function returns the first output value where the input value is equal to the match condition. If there are no matching values, the Switch function returns the final argument that we specify.

Conclusion

The two primary conditional operators in Power Apps are If and Switch. The Switch function is designed to compare an input value against multiple conditions based on equality. In cases where we want to compare an input against a range or list of values, the If function is more suitable.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Data - How to group data in a gallery and calculate sums
January 20, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Data - How to get a row by ordinal number
April 28, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Email - Sending email attachments with the Office 365 Outlook connector
March 30, 2021
Formula - What to try when numbers don't format correctly
March 24, 2021
Controls - How to convert HTML to Text
March 23, 2021
Formulas - how to return all days between two dates
March 15, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Location - Finding the closest location and and sorting records by distance, based on the current location of the user
January 24, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021