“If” is one of the most used functions in any programming language and PowerApps is not the exception. The If function tests if the first condition is met and returns the result. You can define an optional value when all conditions are false. So if you have something like
If(IsToday(birthday),"Happy Birthday") you’ll get a “Happy Birthday” message when the birthday variable contains the date of your birthday. Notice that nothing happens if it’s not your birthday so to add a default value just
If(isCorrect,"Value is Correct", "Error"), and the field will display Value is Correct if the value passes the validations and Errorif there’s an issue.
The If function in PowerApps can be different than in other programming languages that only support an
If(<condition> then <value if true> else <value if false>). It’s quite powerful, allowing you to write something like
If(<first validation>,"Failed the first validation",<second validation>,"Failed the second validation", "All Good") if you have multiple validations. All Good is the default value in case all the other conditions are false. You can use this strategy, for example, to validate user entry fields, where you have a lot of conditions that check the user inserted something invalid and, if all checks fail, it means the value is correct.
Please don’t confuse an If formula with multiple conditions with a Switch function. Both serve different purposes and are inherently different. If starts from the beginning of the conditions and stops as soon as it finds the first true one. A Switch would evaluate a single formula and checks if any of the values provided fit in the criteria. These values can be true or false or any other type. Check the Switch reference for more details.
- You can only nest 50 If statements. Once you reach this limit, think if you’re overcomplicating the overall formula. Re-evaluate the conditions and combine validations into smaller chunks.
- Keep the If formulas as simple as possible. It’s quite easy to get lost in complex formulas, so simplicity is your friend.
- Use the
If(<condition 1>, <result if true>, .... , <default value>)when possible instead of creating multiple If statements. It makes for better and simple formulas.
- To check if all conditions are true, prefer using the
&&instead of nested If statements. For example prefer using
If(IsToday(birthday) && IsToday(birthdayparty), "Happy Birthday & See you tonight" , DateDiff(Today(),birthday,Days) & " day(s) to birthday")instead of
If(IsToday(birthday), If(IsToday(birthdayparty), "Happy Birthday & See you tonight") , DateDiff(Today(),birthday,Days) & " day(s) to birthday"). They both return the same value but the first one is largely more readable.
- Please note that formulas may have localization-based differences. For example, you should write
If(Today(),Now())with “,” separating each of the arguments, but if your localization is Portugal, you should use “;” instead.
Back to the PowerApps Function Reference