If and Switch Function in Power Apps

1. If function in Power Apps

The If function tests one or more conditions until a true result is found. If such a result is found, a corresponding value is returned. If no such result is found, a default value is returned.

Power Apps Syntax a) If( Condition, ThenResult [, DefaultResult ] )
b) If( Condition1, ThenResult1 [, Condition2, ThenResult2, ... [ , DefaultResult ] ] )
• Condition(s) - Required. Formula(s) to test for true. Such formulas commonly contain comparison operators (such as <,>, and =) and test functions such as IsBlank and IsEmpty.
• ThenResult(s) - Required. The corresponding value to return for a condition that evaluates to true.
• DefaultResult - Optional. The value to return if no condition evaluates to true. If we do not specify this argument, blank is returned.

A. If Condition without Default result

The following formula is written on the Text property of the Text label control in Power Apps.

Power Apps Formula

If(Dropdown1.Selected.Value="Rohan", 60)

If the selected value is Rohan, then 60 is returned, otherwise as no default value is provided so blank is returned, blank means nothing.

As the selected value in the following image is “Ashish”, so the condition becomes false and hence blank is returned in the label control.

If function in Power Apps

Once Rohan is selected from the dropdown, the condition becomes true and hence 60 is returned in the label control.

If function in Power Apps

B. If Condition with Default result

The following formula is written on the Text property of the Text label control in Power Apps.

Power Apps Formula

If(Dropdown1.Selected.Value="Rohan", 60, 100)

If the selected value is Rohan, then 60 is returned, otherwise as the default value is provided so 100 is returned.

As the selected value in the following image is “Ashish”, so the condition becomes false and hence the default value 100 is returned in the label control.

If function in Power Apps

Once Rohan is selected from the dropdown, the condition becomes true and hence 60 is returned in the label control.

If function in Power Apps

C. Multiple conditions in the If function

Add a Slider control, and name the control as Slider1. And add a Text label control in Power Apps. The following formula is written on the Text property of the Text label control in Power Apps.

Power Apps Formula

If(Slider1.Value>20, "Good", Slider1.Value>30,"Better", "Worst")

If the slider value is greater than 20, then “Good” is returned, and if the slider value is greater than 30, then “Better” is returned, otherwise as the default value is provided so “Worst” is returned.

As the default value of the slider is 50. The first condition is true, and the corresponding result is returned.

If function in Power Apps

Note here that the second condition is also true, but it is not evaluated because it appears later in the argument list than a condition that evaluates to true. If both the conditions are false then the default result is returned if provided, otherwise blank is returned.

2. Switch function in Power Apps

The Switch function evaluates a formula and determines whether the result matches any value in a sequence that we specify. If a match is found, a corresponding value is returned and it stops further matching. If an exact match is not found, then the default value is returned if specified, if not specified (as this is an optional parameter) then blank is returned.

Power Apps 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

Note: During the match the small and Capital letters are considered, the match must be exact match. It means “Ashish” and “ashish” are considered different during the match.

Example: A. Switch function without Default result

The following formula is written on the Text property of the Text label control in Power Apps.

Power Apps Formula

Switch(Dropdown1.Selected.Value,"Rohan", 60)

If the selected value is Rohan, then 60 is returned, otherwise as no default value is provided so blank is returned, blank means nothing.

As the selected value in the following image is “Ashish”, so the condition becomes false and hence blank is returned in the label control.

Switch function in Power Apps

Once Rohan is selected from the dropdown, the condition becomes true and hence 60 is returned in the label control.

Switch function in Power Apps

B. Switch function with Multiple conditions and without Default result

The following formula is written on the Text property of the Text label control in Power Apps.

Power Apps Formula

Switch(Dropdown2.Selected.Value,"Rohan", 60, "Ashish", 100)

If the selected value is Rohan, then 60 is returned, and when the selected value is “Ashish”, then 100 is returned, otherwise default result is returned if no condition evaluates to true, as no default result is provided so blank is returned, blank means nothing.

As the selected value in the following image is “Ashish”, so the condition becomes true and hence 100 is returned in the label control.

Switch function in Power Apps