You are currently viewing PowerApps if statement and nested if with example

PowerApps if statement and nested if with example

  • Post author:
  • Post category:Power Apps
  • Reading time:13 mins read

In this blog post, we will explore the PowerApps if statement and how it can be used to create more complex and dynamic applications. We will learn how to use IF statement in PowerApps, how to perform PowerApps nested if statements and we will cover the following examples :

  • Power Apps if else statement
  • Power Apps multiple if statements
  • if statements in PowerApps
  • Power Apps if statement multiple actions
  • Multiple if statements in power apps

The switch function can be an alternative of IF statement in PowerApps , for more details check this article

PowerApps if statement

  • PowerApps is use if statements in order to create conditional logic within your application.
  • The Power Apps if statement is a type of conditional statement that allows you to test a condition and then execute a sequence of instructions based on whether the condition is true or false.
  • You can use PowerApps if statements to create logic that controls the behavior of controls, screens, and other components in your application.
  • You can define the condition using the comparison operators such as equals (=), not equals (<>), greater than (>), less than (<), greater than or equal to (>=), or less than or equal to (<=) and you can combine multiple condition using the logical operators such as And and Or (&&,||).
  • You can use simple if and else statement or you can use multiple nested if statements

Synatx

If(condition, true_result, false_result)
  • condition is the expression that you want to test.
  • true_result is the expression that will be executed if the condition is true.
  • false_result is the expression that will be executed if the condition is false.

the true_result and false_result expressions can be any acceptable PowerApps expression, such as a function call, variable assignment, or control property, also can be nest if statements within other if statements to create more complex logic.

PowerApps if statement examples

Example1: Show and hide control based in a condition using If statement

In the following example we will use PowerApps if Statement to check if a gallery contains item or not if not contains item show text label that will tell the user that there is no data right now.

PowerApps if statement

So Place text label in your screen and write your message that you will show to the user and on the visible property write this formula

If(CountRows(ReservationGallery.AllItems)>0,false,true)
Power Apps if else statement

Example2: Setting the value of a variable based on a condition

In this example, the if statement tests whether the value of a slider control is greater than or equal 50. If the condition is true, the myVariable variable is set to the value “High”. If the condition is false, the variable is set to the value “Low” and display this variable in a text label.

In the on change property of the slider control write the following formula ” PowerApps if statement set variable

If(Slider2.Value >= 50, Set(myVariable, "High"), Set(myVariable, "Low"))

and in the Text property of the text label write the variable name

if statment 2 | Power Platform Geeks
if statment 3 | Power Platform Geeks

Example3: Navigating to a different screen based on a condition

In this example, we will use PowerApps if statement to test whether the value of a variable called UserRole is equal to “Admin”. If the condition is true, the application navigates to a screen called AdminScreen, but If the condition is false, the application navigates to a screen called UserScreen.

If(UserRole = "Admin", Navigate(AdminScreen), Navigate(UserScreen))

Example 4: Displaying an error message based on a condition

In this example, we will use PowerApps if statement to show notification message to the users according to specific condition, for example here we will check if the user check a radio button or not if not it will show him notification message to check the radio.

If(IsBlank(Radio1.Selected.Value)
,Notify("Please select the Check Type",NotificationType.Warning)

);
if statment 4 | Power Platform Geeks

PowerApps If Statement Multiple Actions

In PowerApps, an if statement can have multiple actions that are executed based on the condition. all what you have to do is using ‘;’ semi colon between the actions. By using the ; operator, you can chain together multiple actions within an if statement to create more complex logic.

Each action must be separated by the ; operator

Example

In the following example we will use multiple actions in the same if statement in PowerApps

If(
Radio1.Selected.Value="Check Out"&&ThisItem.CheckIn>=Value(txtNoofrooms.Text),
Patch('Rooms Types',LookUp('Rooms Types',ID=ThisItem.ID),{CheckIn:ThisItem.CheckIn-Value(txtNoofrooms.Text),Available:ThisItem.Available+ Value(txtNoofrooms.Text)});Patch('History Log',Defaults('History Log'),{'Action by':User().FullName,'Hotel ID':Gallery1.Selected.ID,'No of Rooms':Value(txtNoofrooms.Text),'Action Type':"Checked In"});Notify("Your Data Checked Out Successfuly",NotificationType.Success),Notify("Correct your Data ",NotificationType.Error))

powerapps nested if statement

We can use multiple if statements in one if statement that we called to Power Apps multiple if statements or Power Apps nested if statements. You can use PowerApps nested if statements within other if statements to create more complex logic in your PowerApps application.

A nested if statement is simply an if statement that is contained within another if statement.

You can replace the true or he false branch to another if statement

Syntax

If(condition1, 
    true_result1, 
    If(condition2, true_result2, false_result2))

Also instead of writing If statement you can directly write the nested conditions as the following syntax.

If( Condition1, Result1 [, Condition2, Result2, ... [ , DefaultResult ] ] )

Example 1

In the following example, the outer if statement tests whether the value of a text input control is greater than 10. If the condition is true, an inner if statement is executed to test whether the value of another text input control is less than 20. If both conditions are true, the text property of a text label will set to “Success”. If either condition is false, the text property of a text label will set “Failure”. If the outer if statement is false, the text label will set to “Failure”.

If( Value(txtvalue1.Text)> 10, 
    If(Value(txtvalue2.Text)< 20, Set(myVariable, "Success"), Set(myVariable, "Failure")), 
    Set(myVariable, "Failure"))

in the text property of the label write myVariable.

Example 2

another example with more than nested if statement that tests multiple conditions:

codeIf(Value(txtvalue1.Text)> 10, 
    If(Value(txtvalue2.Text)< 20, 
        Set(myVariable, "Success"), 
        If(Value(txtvalue3.Text)= 30, 
            Set(myVariable, "Partial Success"), 
            Set(myVariable, "Failure")
        )
    ), 
    Set(myVariable, "Failure")
)

In this example, we use PowerApps nested if statement as the following:- the outer if statement tests whether the value of a text input control that called txtvalue1 is greater than 10. If the condition is true, an inner if statement is executed to test whether the value of another text input control called txtvalue2 is less than 20. If the condition is true, the myVariable variable is set to “Success”. If the condition is false, another nested if statement is executed to test whether the value of a text input control called txtvalue3 is equal to 30. If the condition is true, the variable is set to “Partial Success”. If the condition is false, the variable is set to “Failure”. If the outer if statement is false, the variable is also set to “Failure”.

So you can create nested if statements that test multiple conditions and execute different actions based on the results.

This is how to use nested if PowerApps Statements

See Also
Join us

Heba Kamal

Microsoft MVP, MCT, Technical Speaker, Blogger, and Microsoft 365 and Power Platform Consultant.