#PowerPlatformTip 91: 'IF Function in Formulas'
Skip redundant If(condition, true, false) in Power Apps: use the condition directly for cleaner boolean formulas and faster apps. Examples included.
TL;DR: Skip redundant
If(condition, true, false), a condition already returns a boolean, so just write the condition directly for cleaner, faster formulas.
💡 Challenge
In Power Apps and similar platforms, developers often use the IF function with redundant additional elements, like in expressions such as If(1+1=2, true, false) or If(varAdmin="name@mail.com", true, false). This approach unnecessarily lengthens and complicates the formula without adding any real benefit.
✅ Solution
By understanding that conditions within the IF function inherently yield a boolean value (true or false), you can streamline your formulas. Instead of using the IF function, directly use the condition, such as 1+1=2 or varAdmin="name@mail.com", which will naturally return true or false based on the condition’s evaluation.
🔧 How It’s Done
Here’s how to do it:
- Instead of writing
If(1+1=2, true, false)
🔸 Use1+1=2directly. - Instead of writing
If(varAdmin="name@mail.com", true, false)
🔸 UsevarAdmin="name@mail.com"directly.
🎉 Result
This approach results in a formula that is not only cleaner and more readable but also more efficient, as it directly returns a boolean value based on the condition, eliminating the unnecessary use of the IF function.
🌟 Key Advantages
🔸 Simplified Formulas: Your formulas become significantly simpler and more straightforward, enhancing readability and understanding.
🔸 Improved Performance: By removing superfluous elements, your app’s performance may potentially improve.
🔸 Easy Maintenance: Simplified formulas are less cumbersome to maintain and update, saving time and effort in the long run.
📚 If Reference: Else-If, Missing Else, Switch, Even/Odd
Results below were run with score = 72, day = "Tue" and ok = true.
Multiple branches (if / else if / else)
If takes any number of condition/result pairs and an optional final else value. The first condition that is true wins:
If(score >= 90, "A", score >= 80, "B", score >= 70, "C", "F")
// "C"
If without else returns Blank
🔸 If(score >= 90, "A", score >= 80, "B") returns Blank(); IsBlank(...) on it is true.
🔸 If(ok, "shown") returns "shown", and If(Blank(), "yes") returns Blank(): a blank condition counts as false. If(Blank(), "yes", "no") returns "no".
If versus Switch
Switch compares one value against a list of options; If evaluates independent conditions. Both have an optional default and return Blank() without one:
Switch(day, "Mon", "Start", "Fri", "End", "Midweek") // "Midweek"
Switch(day, "Mon", "Start", "Fri", "End") // Blank()
If(day = "Mon", "Start", day = "Fri", "End", "Midweek") // "Midweek"
Use Switch when every branch tests the same value for equality; use If for >=, &&, || or different variables.
Even or odd with Mod (pair / impair)
🔸 Mod(7, 2) is 1, Mod(8, 2) = 0 is true, so If(Mod(n, 2) = 0, "even", "odd") returns "odd" for 7.
🔸 Mod(-7, 2) is 1, so the test also works for negative numbers.
Nesting and combining
🔸 If(ok, If(score > 80, "A", "B"), "C") returns "B".
🔸 Conditions can be combined without If: ok && score > 50 returns true directly, in line with the tip above.
🎥 Video Tutorial
🛠️ FAQ
1. Why is using the condition directly better than If(condition, true, false)?
The condition itself evaluates to true or false, making the IF wrapper redundant and the formula unnecessarily verbose.
2. Can simplifying formulas this way impact app performance?
Yes, removing unnecessary IF calls can slightly improve performance and results in cleaner, more maintainable code.
3. When should I still use the IF function?
Use IF when you need to return values other than a boolean result, such as specific text or numeric values based on the condition.
4. How do I write if / else if / else in Power Apps?
Chain condition/result pairs in one If: If(c1, r1, c2, r2, c3, r3, else). The first true condition wins; with score = 72 the formula If(score >= 90, "A", score >= 80, "B", score >= 70, "C", "F") returns "C".
5. What does If return when no condition is true and there is no else?
Blank(). If(score >= 90, "A", score >= 80, "B") with score = 72 returns Blank().
6. How do I check whether a number is even or odd (pair / impair)?
Mod(n, 2) = 0 is true for even numbers. If(Mod(n, 2) = 0, "even", "odd") returns "odd" for 7 and works for negative numbers too (Mod(-7, 2) is 1).
7. If or Switch?
Switch(value, match1, result1, match2, result2, default) when all branches compare the same value for equality; If for everything else. Both return Blank() when nothing matches and no default is given.
🔗 Related Tips
🔸 #PowerPlatformTip 33: ‘Coalesce’ - another way to cut redundant If() wrapping, this time for picking the first non-blank value.
🔸 #PowerPlatformTip 23: ‘Filter Blank’ - a condition worth writing directly rather than wrapping it in If().
This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy – live Power Platform workshops, free or CHF 49. Join the workshop waitlist to get notified about new dates. For companies that want this implemented for them, see KMUpower – Power Platform consulting & automation for SMEs (DACH).