25 Power Apps IF Function Examples
- Posted by - Matthew Devaney
- on -
- 25 Comments
The Power Apps IF Function performs a logical comparison to see if the result is true. Then it conditionally executes the next lines of code based on whether the result was true or false. The examples on this page show how the Power Apps IF Function works for every data type.
Important note: for most examples the IF Function the 2nd argument is true and the 3rd argument is false. These true and false values are only placeholders. They should be replaced with your own code in the real-world.
I try to explain to all new powerapps developers or citizens that writing “if(my condition,true,false)” is useless and make the readability of code worse.
Can you tell us if there is a real need of this syntax when writing just “myCondition” in a Boolean field is enough ?
Alexsolex,
Agreed that when your IF function evaluates to either true or false you should skip it and just write varTextValue=”ABC” instead of If(varTextValue=”ABC”, true, false).
What I am trying to focus on with my examples is how to write conditions within the IF Function’s first argument. My thinking was if the reader understands how to write conditions, then they could fill-in the true and false outcomes with their own value. Let’s see what feedback is received on this article. If I have assumed too much knowledge of the reader then I will revise it.
As always, I appreciate the feeback Alexsolex 🙂
Hi Matthew. Really handy list, thank you.
I’m not sure if you also do this, but when I have a conditional with OR like this:
If(varInventoryItem=”Samsung” Or varInventoryItem=”LG” , true, false)
I prefer to write it as:
If(varInventoryItem in [“Samsung” , “LG”] , true, false)
as it makes it easier to read and to add more items to the list (in this case, of Manufacturers).
What do you think?
Cheers,
Andrea
Andrea,
Yes, I do use this method… a lot! Thank you for reminding me about it. I will update my article later this week to include. You rock 🎸
Hi Matthew, I tried to modify nested if in power automate following what is explained on this article and could not save the compose action….
First, i created Initialize variable action of string type ‘varTransport’ and the value is based on a Msforms Input
Then a compose action : if(“Train” in variables(‘varTransport’),true,”Autobus” in variables(‘varTransport’),true,”Location automobile” in variables(‘varTransport’),true,false)
Am I missing something ?
Thanks,
Jerome,
This article shows code examples for Power Apps. You appear to be asking about Power Automate. Is it possible you have misread my article and believed it was for Power Automate?
I wish this article had of existed when I started my PowerApps journey 2 years ago. It would have saved me many tears. Especially the logical operators section. Will be sharing this with the up and comers I am mentoring.
Heidi,
I am glad to hear I am writing the content you wished existed. But the better news to me is, you will share it with the dev you are mentoring
Having a good mentor is important. Thanks for doing what you do and helping others grow.
Hi Matthew:
Thanks for this – did not realize that there was a switch style statement as you showed in Check multiple conditions!
cheers
Drew,
Just a quick note on this. It only executes code for the first logical comparison that passes the true/false check. If multiple logical comparisons pass it still only executes the first. Thanks for dropping me a comment 🙂
Matthew,
I’m a huge fan of your work. Thanks for everything you do.
In reading the article, I had a question:
Code:
Check if the Buyer column of varInventoryItem has a user name equal to “Sarah Green”
If(varInventoryItem = Buyer.’Display Name’, true, false)
Wouldn’t it be as follows?:
If(varInventoryItem.Buyer.’Display Name’ = “Sarah Green”, true, false)
Best,
Pete
Pete,
Good catch! I agree that requires an update.
Check if the OrderStatus of varInventoryItem equals “Ordered”
should this code like below
thank you for your great posting
LDK,
Good catch. Thank you 😊
I think this Output is not ok:
Fernando,
Excellent find. It’s now fixed.
I tried this syntax but did not get the desired result. I ended up using a switch.
Also, it works if I treat it with nested If statements. Has something changed since you wrote this?
If(
varTitle = “Dishwasher”,
“Dishwasher was found!”,
varTitle = “Kitchen Sink”,
“Kitchen Sink was found!”,
varTitle = “Refrigerator”
“Refrigerator was found!”,
“No matches found”
)
Oscar,
It’s missing a comma after “Refrigerator.” I’ll go fix that now 😉
Hi @Matthew,
Sorry for late reply, correct me if I am wrong but this code that you on example shouldn’t return false?
Regards
Lütfi,
You’re correct. I’ve fixed it now. Thanks!
If(
IsBlank(DataCardValue35.SelectedItems),Set(varTask,true),
IsBlank(DataCardValue1.SelectedItems),Set(varApplicationArea,true),
IsBlank(DataCardValue36.SelectedItems),Set(varTimein,true),
IsBlank(DataCardValue40.SelectedItems),Set(varTimeout,true),
TimeValue( DataCardValue36.Selected.Value)>=TimeValue(DataCardValue40.Selected.Value), Set(varLabel11, true),Set(varLabel11, false) Or
IsBlank(DataCardValue8.SelectedItems),Set(varTask,true),
IsBlank(DataCardValue2.SelectedItems),Set(varApplicationArea,true),
IsBlank(DataCardValue4.SelectedItems),Set(varTimein,true),
IsBlank(DataCardValue5.SelectedItems),Set(varTimeout,true),
TimeValue( DataCardValue4.Selected.Value)>=TimeValue(DataCardValue5.Selected.Value), Set(varLabel11, true),Set(varLabel11, false),
IsBlank(DataCardValue10.SelectedItems),Set(varTask,true),
IsBlank(DataCardValue3.SelectedItems),Set(varApplicationArea,true),
IsBlank(DataCardValue14.SelectedItems),Set(varTimein,true),
IsBlank(DataCardValue15.SelectedItems),Set(varTimeout,true),
TimeValue( DataCardValue14.Selected.Value)>=TimeValue(DataCardValue15.Selected.Value), Set(varLabel11, true),Set(varLabel11, false),
IsBlank(DataCardValue7.SelectedItems),Set(varTask,true),
IsBlank(DataCardValue17.SelectedItems),Set(varApplicationArea,true),
IsBlank(DataCardValue29.SelectedItems),Set(varTimein,true),
IsBlank(DataCardValue24.SelectedItems),Set(varTimeout,true),
TimeValue( DataCardValue29.Selected.Value)>=TimeValue(DataCardValue24.Selected.Value), Set(varLabel11, true),Set(varLabel11, false),
This is my if statement but its not working says can’t evaluate formula. I have 4 forms and in each I want to validate if the forms aint blank and that time in and time out can’t be the same. Please help
Hey! I think I’ve searched the entire internet and I can’t find anything with the past 2 years about making a data card in Power Apps conditionally required based on a response from another data card. The code that I can find is older than 2021 and does not work. Do you know what I would put in the ‘Required’ field of a datacard I want to make required based on a response from a previous datacard?
Hello Matthew, thank you for your wonderful post.
I was wondering if you might have a solution for setting up a column in model-driven apps in power apps using formula as data type. I’ve tried various formulas I found posted and shared across the internet, but unfortunately, I didn’t find anything that would work. I am trying to text column Owner with the lookup data type against defined values (the user names). However, it doesn’t seem to work in a simple Excel manner, aka If(Owner=”Bob Marley”,”750000″,”0″) – see screenshot attached. Using calculated data type setup for the column doesn’t work as Owner field could only have if statement of “contains data” or “doesn’t contain data” – see screenshot 2.
I would really appreciate any advise, thank you in advance.
Hi there.
Thanks for the nice info love your stuff.
I have an IF scenario.
I have many SharePoint lists that I want to display in PowerApps based on n condition.
If the previous answer is “Option1” then SP List A must display but if “Option2” is selected SP List B must display. and so on and so on.
I get as far as [If(PlantDataCard.Selected.Value = “Salads and Pickles”,
DisplayList(‘[OT] Devices – Bethlehem – Salads and Pickles’),
false)} but it is not happy with the “DisplayList” section.
I believe your example in the “IF Condition Using Multiple Logical Operators” section will result in an error. You are missing an open parentheses.