cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper II
Helper II

If in If

Hey,

Is there an opportunity to have an if statement in and if staement?

I mean something like

if(condition)
{
     if(condition)
     {
          statement
     }
     elseif
     {
          statement
     }
     else
}
else
statement

or do I have to use if(condition, statement, condition2, statement2, condition3, statement3)?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User
Super User

Re: If in If

@JonasH 

Yes, you can use nested if statements in PowerApps.  Here's an example.  Assume "x" is the name of a Number type variable.

 

If(
    x > 10,
    If(
        x <= 15,
        "Number is between 10 and 15
        x <= 20,
        "Number is between 16 and 20,
        "Number is greater than 20
    ),
    "Number is less than 10"
)

 

However, in many cases flattening the IF statement will make your code more readable.  In my opinion, the code below is easier to understand.

 

If(
    And(x > 10,x <=15),"Number is between 10 and 15
    And(x > 15,x <=20),"Number is between 16 and 20,
    x > 20,"Number is greater than 20,
    "Number is less than 10"
)

 

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

View solution in original post

3 REPLIES 3
Highlighted
Super User
Super User

Re: If in If

@JonasH 

Yes, you can use nested if statements in PowerApps.  Here's an example.  Assume "x" is the name of a Number type variable.

 

If(
    x > 10,
    If(
        x <= 15,
        "Number is between 10 and 15
        x <= 20,
        "Number is between 16 and 20,
        "Number is greater than 20
    ),
    "Number is less than 10"
)

 

However, in many cases flattening the IF statement will make your code more readable.  In my opinion, the code below is easier to understand.

 

If(
    And(x > 10,x <=15),"Number is between 10 and 15
    And(x > 15,x <=20),"Number is between 16 and 20,
    x > 20,"Number is greater than 20,
    "Number is less than 10"
)

 

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

View solution in original post

Highlighted
Dual Super User II
Dual Super User II

Re: If in If

You can definately nest If() functions, but I'm not sure that's really what you want.  You would do it like this

If(condition,If(condition2,true,false),if(condition3),true, false))

In this example Condition2 is only tested if condition is true and Condition3 is only tested if Condition is false.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Highlighted
Advocate IV
Advocate IV

Re: If in If

Yes you can (see posts above).

 

I just want to note that depending on the situation you can also use 'Switch':

Switch( TextInput1.Text,
  "foo", Notify( "foo!" );
  "bar", Notify( "bar!" );
  Notify( "probably baz..." );
);

Just keep in mind that the switch condition is only evaluated once and you can not use 'formula matching'.

Helpful resources

Announcements
Ignite

Microsoft Ignite

This will be a conference that you do not want to miss!

secondImage

New Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

August 2020 CYST Challenge

Check out the winners of the recent 'Can You Solve These?' community challenge!

Experience what’s next for Power Apps

Join us for an in-depth look at the new Power Apps features and capabilities at the free Microsoft Business Applications Launch Event.

Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (4,328)