cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Help me make if statements more succinct

I'm almost embarrassed to be asking such a simple code question but if there is an elegant answer, it will make my app significantly less verbose. 

 

I have many places where I evaluate an expression (which may be several lines to achieve) and do a quick check to see if it's within bound else I return something different. One example is I want to ensure that the following returns a number between 0 and 365. If it does, I want the number, if it doesn't, I want "?".

 

RoundUp(
    Today() - DateValue(
        Right(
            ThisItem.Description,
            Len(ThisItem.Description) - Find(
                "; modified",
                ThisItem.Description
            ) - 6
        ),
        "en-US"
    ),
    0
)

 

 

 

Typically I would do something like 

 

If(Long Expression > Condition, Long Expression, Error value)

 

 

When the expression I am evaluating is several lines as demonstrated above, it's pretty ugly. Is there a more succinct way to handle this short of writing the entire thing twice within the if statement? In other code I would hold this as a variable however that would add even more clutter to my PowerApps as this happens in many, many places and those values are not otherwise useful. 

1 ACCEPTED SOLUTION

Accepted Solutions
timl
Super User
Super User

Hi @Anonymous 

There's no need to be embarrassed about asking, it's a good question 🙂

Judging by how you use ThisItem in your syntax, I assume that you intend to apply this logic in a gallery, or some other control that displays a collection of data. In this case, I would avoid variables because you would potentially need to create a variable for every row of data.

In addition to @v-yutliu-msft  suggestion of placing LongExpression in a label and referring to that, another approach is to add LongExpression to the Items property of your gallery control with the AddColumns function. The expression would look like this:

AddColumns( YourDataSource,
            "LongExpression",
            RoundUp(
                  Today() - DateValue(
                  Right(
                       ThisItem.Description,
                        Len(ThisItem.Description) - Find(
                       "; modified",
                       ThisItem.Description
                      ) - 6
                   ),
                   "en-US"
                   ),
                   0
            )
)

This will add LongExpression as a column to your data source. Once you do that, you can use the syntax that you suggested in your post....

If(LongExpression > Condition, LongExpression, Error value)


Finally, I'd suggest that people vote on this idea of adding Functions/Macros to PowerApps because this could help out in these types of situation.

https://powerusers.microsoft.com/t5/Power-Apps-Ideas/Create-custom-functions-macros/idi-p/6187 

 

View solution in original post

11 REPLIES 11
poweractivate
Super User
Super User

@Anonymous 

 

If you ever reuse the part that you don't want to abstract away into a variable anywhere else then it may be unwise to be so hesitant to abstract it away into a variable.


If you are absolutely sure you never need it again, the nested condition might be actually fine.

 

Otherwise, just double check and see if you are absolutely sure you can't combine the If statements into a single logical check. To do this, look carefully and see if the two if statements have something in common and write a single if statement. Short of that, the nesting may be fine if it is not reused anywhere else. Note that often times something that looks like it isn't needed later actually is - the moment that happens, even if it reused only in one other place, this is where a variable that you are so hesitant in using, likely would probably be better to use in that case.

poweractivate
Super User
Super User

@Anonymous 

 

Here's a more obvious one to look into.

 

Use the && (and) operator to combine the checks into a single instead of nesting - however, that depends - if it's two different things you need to do, maybe you do need to nest.

 

Your question is about Long Expression not needing to be written twice. That already in and of itself is repeating something twice. In our opinion that alone would justify a variable just to reduce repetition - even if only one time - however that's our opinion.

EricLott
Resident Rockstar
Resident Rockstar

PowerApps can get pretty ugly. Just use //comments and add some extra line break if needed. You can also use Set() to break the end If() statement up into smaller pieces. Also, there's Flow, which is very powerful. If there's a big function you'll use a lot, you can outsource it to Flow and get the result delivered back to PowerApps, which would reduce it to something like YourFlowName.Run(YourArguments).Result

---
If this answered your question, please click "Accept Solution". If this helped, please Thumbs Up.

poweractivate
Super User
Super User

@Anonymous 

 

Your question is about Long Expression not needing to be written twice.

 

That already in and of itself is repeating something twice.

 

In our opinion that alone would justify a variable just to reduce repetition (Don't Repeat Yourself or DRY principle) - even if only one time in the same line - however that's our opinion.

Anonymous
Not applicable

I do agree that repetition is ugly however I'll personally weigh that with careful utilization of my variables as to not create too much abstraction that could be hard for someone else to follow.

 

This is really just a general question of mine to see if I'm missing a trick that people use. I find the situation where I'm evaluating and comparing and then returning the evaluated expression again happens very frequently. I was hoping there was the equivalent of  x++ is the same as x=x+1. 

@Anonymous 

 

Is that huge Round inline expression the one you referred to as "Long Expression"?

 In our opinion, it is worth putting that into a variable.

 

Also if it needs to be changed later (and it may need to, for any number of reasons) - changing it once instead of twice in some long expression is preferable to us, though that's our opinion. 

 

 

RoundUp(
    Today() - DateValue(
        Right(
            ThisItem.Description,
            Len(ThisItem.Description) - Find(
                "; modified",
                ThisItem.Description
            ) - 6
        ),
        "en-US"
    ),
    0
)

 

@Anonymous 

 

Also for performance if you're doing this a lot - although we are unsure of the specific implementation of the underlying engine at this time, writing it inline may run the operation in the underlying engine twice (or maybe even more, depending on the situation) instead of somewhere closer to just once if you set a variable - though we can't be absolutely sure on that. If that is multiplied by a certain number and if our assertion is true, then there may be a performance impact besides a maintenance impact. We cannot be sure on the performance impact though.

v-yutliu-msft
Community Support
Community Support

Hi @Anonymous ,

Could you tell me where do you want to use this formula: RoundUp(....) and how do you want to justify this value?

I assume that you set a label's Text to RoundUp(....), and then you click a check button, of the data is between 0 and 365, the label will retain displaying this data, if isn't, change the label's text and display "?".

I've made a similar test for your reference:

1)set the label's Text:

If(IsBlank(testvar)||testvar="b",
   Text(RoundUp(
             Today() - DateValue(
             Right(
             ThisItem.Description,
             Len(ThisItem.Description) - Find(
                "; modified",
                ThisItem.Description
                    ) - 6
                    ),"en-US"
                                   ),
                   0)
            ),testvar="c","?")

//please notice that you must use Text(RoundUp(....)), because "?" is text type , while the result of "roundup()" is number. If you want them display in one same control, you need to make them data type the same

2)set the check button's OnSelect:

Set(testvar,"a");If(Value(RoundUp(....))>=0&&Value(RoundUp(...))<=365,Set(testvar,"b"),Set(testvar,"c"))

 //Please replace the .... with the formulas that you listed.

use variable to justify situations

If the variable is blank, then the label will display the original data.

If the variable meet the condition, then the label will display the original data too.

If the variable not meet the condition, then the label will display "?".

 

What's more, if you want the label's Text change based on a textinput.

You could also set the Textinput's OnChange to :

Set(testvar,"a");If(Value(RoundUp(....))>=0&&Value(RoundUp(...))<=365,Set(testvar,"b"),Set(testvar,"c"))

To sum up, since you need to change the label's text, so it is a behavior formula, you should set this formula in one behavior property,like a button's OnSelect, a textinput's OnChange, ect.

 

 

 

Best regards,

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
timl
Super User
Super User

Hi @Anonymous 

There's no need to be embarrassed about asking, it's a good question 🙂

Judging by how you use ThisItem in your syntax, I assume that you intend to apply this logic in a gallery, or some other control that displays a collection of data. In this case, I would avoid variables because you would potentially need to create a variable for every row of data.

In addition to @v-yutliu-msft  suggestion of placing LongExpression in a label and referring to that, another approach is to add LongExpression to the Items property of your gallery control with the AddColumns function. The expression would look like this:

AddColumns( YourDataSource,
            "LongExpression",
            RoundUp(
                  Today() - DateValue(
                  Right(
                       ThisItem.Description,
                        Len(ThisItem.Description) - Find(
                       "; modified",
                       ThisItem.Description
                      ) - 6
                   ),
                   "en-US"
                   ),
                   0
            )
)

This will add LongExpression as a column to your data source. Once you do that, you can use the syntax that you suggested in your post....

If(LongExpression > Condition, LongExpression, Error value)


Finally, I'd suggest that people vote on this idea of adding Functions/Macros to PowerApps because this could help out in these types of situation.

https://powerusers.microsoft.com/t5/Power-Apps-Ideas/Create-custom-functions-macros/idi-p/6187 

 

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (3,932)