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.
Solved! Go to Solution.
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
@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.
@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.
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.
@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.
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.
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,
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
Dear Community Members, We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd. During this period, members will not be able to Kudo, Comment, or Reply to any posts. On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities. What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!
We are excited to announce the Summer of Solutions Challenge! This challenge is kicking off on Monday, June 17th and will run for (4) weeks. The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions in the Forums to as many questions as you can. Answers can be provided in all the communities. Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”) - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024 Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods. You must enter into each weekly Entry Period separately. How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different. Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers. At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing. Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner. A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random. Individuals will be contacted before the announcement with the opportunity to claim or deny the prize. Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners. Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event. ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE** Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersNumber of SolutionsSuper UsersNumber of Solutions @anandm08 23 @WarrenBelz 31 @DBO_DV 10 @Amik 19 AmínAA 6 @mmbr1606 12 @rzuber 4 @happyume 7 @Giraldoj 3@ANB 6 (tie) @SpongYe 6 (tie) Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersSolutionsSuper UsersSolutions @anandm08 10@WarrenBelz 25 @DBO_DV 6@mmbr1606 14 @AmínAA 4 @Amik 12 @royg 3 @ANB 10 @AllanDeCastro 2 @SunilPashikanti 5 @Michaelfp 2 @FLMike 5 @eduardo_izzo 2 Meekou 2 @rzuber 2 @Velegandla 2 @PowerPlatform-P 2 @Micaiah 2 Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 3:Community MembersSolutionsSuper UsersSolutionsPower Apps anandm0861WarrenBelz86DBO_DV25Amik66Michaelfp13mmbr160647Giraldoj13FLMike31AmínAA13SpongYe27 Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 4:Community MembersSolutionsSuper UsersSolutionsPower Apps DBO-DV21WarranBelz26Giraldoj7mmbr160618Muzammmil_0695067Amik14samfawzi_acml6FLMike12tzuber6ANB8 SunilPashikanti8
On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners. The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights. Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.
We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for. We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer. Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together. Here's to new beginnings and endless possibilities! If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport. To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages