If statements are one of the most important constructs within software development. PowerApps If statements differ a little from what you might know from other languages.
- PowerApps If Function
- PowerApps If Function Examples
- If else statement
- If else if (elseif)
- If multiple conditions
- If statement multiple actions
- If not
- If checkbox is checked
- If IsBlank
- If collection IsEmpty
- If collection contains text / If exists in list / If in collection / if value in collection
- If begins with
- If Combobox is blank
- If Combobox selected
- If dropdown equals
- If dropdown is selected
- If date is greater than today
- Nested if statement
- If edit mode
- If gallery is empty
- If greater than and less than / If number between / if function between two values
Note: In case you want to compare one variable against multiple possible values, you might want to take a look at the Switch function.
Note: In case you want to set a (default) value, in case a variable is null, you might want to take a look at the Coalesce function.
PowerApps If Function
PowerApps If function let you make decisions based on one or more conditions. You can react on the value of a variable or state of UI control.
Syntax
If( Condition1, ThenResult1 [, Condition2, ThenResult2, ... [ , DefaultResult ] ] )
Input Parameters
- Conditions1 (mandatory): The condition you want to check. (Needs to evaluate to true or false)
- ThenResult1 (mandatory): Defines what should happen, if Condition1 is true.
- Condition2 (optional): Another Condition (there could multiple Condition/Result pairs)
- ThenResult2 (optional): Defines what should happen, if Condition2 is true.
- DefaultResult (optional): Defines what should happen, when none of the conditions evaluates to true.
Return Value
- Blank if no condition is matched
- The result for the conditions, that evaluates to true.
PowerApps If Function Examples
If else statement
Within PowerApps there is not a Else keyword. Just add your else path after the last condition/result pair. As an example for a PowerApps if else condition, take a look at the example below.
Set(isOrderPayed, false);
If(
isOrderPayed, // IF order is paid?
Navigate(OrderOverview), // yes, THEN show order overview
Navigate(Payment) // ELSE show payment screen
);
If else if (elseif)
In case you need multiple elseif branches, just formulate one if branch after the other. If needed provide a else option as the last statements without a condition.
If(
price > 10, "expensive",
price > 20, "very expensive",
"fair price"
);
// price = 9, returns "fair price"
// price = 14, returns "expensive"
// price = 21, returns "very expensive"
If multiple conditions
When you have multiple conditions to check, where the result should be the same: Use And and/or Or to build your conditions!
Here are some Powerapps if multiple conditions examples:
If And
This example shows how to use And to check, that a number is in certain a range. And evaluates to true if both conditions evaluate to true.
Set(age, 25);
If(
age > 19 And age < 30, // two conditions connected via And (both must be true)
"I am in my twenties."
);
// age = 25: true
// age = 19: false
If Or
This example shows how to use Or to check, that a number is in matches some criteria. Or evaluates to true if one condition evaluate to true.
If(
age < 10 Or age > 90, // two conditions connected via Or (one must be true)
"I am young or old."
);
// age = 25: false
// age = 9: true
// age = 95: true
If statement multiple actions
Sometimes you want to do more than one thing in a If statement result. You can do this by using the semicolon as shown:
If(isOrderPayed,
Navigate(Payment); // action #1 SEMICOLON
Notify("Please pay!"); // action #2 SEMICOLON
);
If not
To toggle the result of a condition use Not or ‘!’ like this:
If(
Not(false),
"Not false is true."
);
If(
!false,
"Not false is true."
);
If checkbox is checked
To find out if a chcekbox is checked or not, just use its value.
If(ActiveCheckbox.Value, "checked","not checked");
If IsBlank
Want to know if a text is blank or not? Use IsBlank function:
If(IsBlank("some string"),"blank","not blank"); // Returns "not blank"
If(IsBlank(""),"blank","not blank"); // Returns "blank"
If Not IsBlank
If(Not(IsBlank("some string")),true,false); // Returns "true"
If(Not(IsBlank("")),true,false); // Returns "false"
If collection IsEmpty
To check if a collection is emtpy, just use IsEmpty.
If(IsEmpty([]),"empty","not empty"); // Returns "empty"
If(IsEmpty(["a","b"]),"empty","not empty"); // Returns "not empty"
If collection contains text / If exists in list / If in collection / if value in collection
There are multiple ways to check if an item exists in a collection. Using the in operator is the simplest.
ClearCollect(
Weekdays,
["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
);
If(
"Tuesday" in Weekdays,
"Tuesday is a weekday."
);
If begins with
To find out if a text begins with a certain string, make use of StartsWith function:
If(
StartsWith("abc","a"),
Notify("Starts with 'a'.")
);
If Combobox is blank
For instance for validation purposes it is good to know, if the user has selected a value of a combox.
If(
IsBlank(MyComboBox.SelectedItems.Value),
"Combobox not selected."
)
If Combobox selected
For instance for validation purposes it is good to know, if the user has selected a value of a combox.
If(
Not(IsBlank(MyComboBox.Selected.Value)),
"Combobox selected."
)
If dropdown equals
When a certain dropdown option selected, you want to react on the item? You check the value of Form.Selected.Value. In case you are want to check multiple options, the Switch function is a more elegant way to check the conditions.
If(
MyDropdown.Selected.Value = "Monday",
"Monday selected."
)
If dropdown is selected
You have dropdown and you want to know, if there is an option selected? Do this:
If(
Not(IsBlank(MyDropdown.Selected.Value)),
"Dropdown selected."
)
If date is greater than today
Need to know if a date is greater than today? Just do a comparison like this one:
If(
DateAdd(Today(),1,Days) > Today(),
"In future."
);
Nested if statement
You can also use if statements within an if statement’s condition. Here is a simple example to understand nested if statements:
If(price > 10,
If(price > 20,"very expensive","expensive"),
"fair price"
);
// price = 9, returns "fair price"
// price = 14, returns "expensive"
// price = 21, returns "very expensive"
If edit mode
Switching between different modes in Forms is common. To find out in which mode your form is, just check the Mode of the form like this:
If(
MyForm.Mode = DisplayMode.Edit,
"edit"
);
If gallery is empty
To find out, if a gallery is empty and has no items: You can count the number of row and compare them to 0.
If(
CountRows(MyGallery.AllItems) = 0,
"Gallery is empty."
);
If greater than and less than / If number between / if function between two values
In case you want to verify, if a number is between two values. Just use the And to use two conditions. In the example it is checked if age is greater than 19 and less than 30.
Set(age, 25);
If(
age > 19 And age < 30, // two conditions connected via And (both must be true)
"I am in my twenties."
);
I thought my brain was going to spontaneously combust as I had been trying to solve my problem with multiple conditions using the If statement in my powerapp. Then I saw your explanation.
You just prevented an ugly scene and saved a life with your post! Thank you so much. What’s most important it is written clearly and simply.