cancel
Showing results for
Did you mean:
Frequent Visitor

## Calculate max value to not fo over certain number

Hello,

I am working on a game idea and using PowerApps that goes into a collection. That may not be the best way but it has been working so far. I have a set value that can decrease through each turn, but I don't want it to exceed 100 or go below 0.

Example:

1st entry: Start at 100, lose 20 points so the new total is 80.

2nd entry: Now at 80, nothing happened so stays there.

3rd entry: Still at 80, but lost 20 points, so now at 60.

4th entry: Now at 60, but gained 30. no problems so far.

5th entry: Now at 90, but gained 30. BUT, I don't want the total to be 120 but the max number at 100.

Here is the code I was trying to use:

If((100 - 10*Atk1_damage_counter - 25*Atk2_damage_counter + 35 * heatlth_counter) >100,
100,
100 - 10*Atk1_damage_counter - 25*Atk2_damage_counter + 35 * health_counter)

Hopefully this makes sense.

cwsmith43

1 ACCEPTED SOLUTION

Accepted Solutions
Frequent Visitor

Hello Eric,

Thank you for the reply back. I tried the formula below but it is erroring out. It says there is an "expected operator error. Saying that something is missing to connect the pieces but I am not seeing the error in the formula. The entire Set(...) is underlined in red. I will attach a screenshot. I appreciate your help and I like your idea of the variable piece. I tried to reorganize the calculation part thinking maybe PA did not like the - sign, but i ran a test calculation and that isn't an issue. I tried cutting out the 2nd variable piece, Health, to see if it was that but still got the red line of shame.

Set(HealthChange, 35 * MonsterA_Def1_damage_counter - 10 * MonsterA_Atk1_damage_counter - 25 * MonsterA_Atk2_damage_counter);

Set(Health, If(100 - HealthChange >= 100, 100,100 - HealthChange))

11 REPLIES 11
Anonymous
Not applicable

For debugging, I'd recommend moving the formula you use to calculate the change to a new variable, HealthChange in this example. Then it's easier to see what's going on.

``````Set(HealthChange, 10*Atk1_damage_counter - 25*Atk2_damage_counter + 35 * heatlth_counter);
Set(Health, If(100 - HealthChange >= 100, 100, 100 - HealthChange))``````

---

Frequent Visitor

Hello Eric,

Thank you for the reply back. I tried the formula below but it is erroring out. It says there is an "expected operator error. Saying that something is missing to connect the pieces but I am not seeing the error in the formula. The entire Set(...) is underlined in red. I will attach a screenshot. I appreciate your help and I like your idea of the variable piece. I tried to reorganize the calculation part thinking maybe PA did not like the - sign, but i ran a test calculation and that isn't an issue. I tried cutting out the 2nd variable piece, Health, to see if it was that but still got the red line of shame.

Set(HealthChange, 35 * MonsterA_Def1_damage_counter - 10 * MonsterA_Atk1_damage_counter - 25 * MonsterA_Atk2_damage_counter);

Set(Health, If(100 - HealthChange >= 100, 100,100 - HealthChange))

Anonymous
Not applicable

That's odd. Here is the test code I'm using and it all seems to be working.

``````Set(Health,100);
Set(MonsterA_Def1_damage_counter, .45);
Set(MonsterA_Atk1_damage_counter, .35);
Set(MonsterA_Atk2_damage_counter, .25);
Set(HealthChange, 35 * MonsterA_Def1_damage_counter - 10 * MonsterA_Atk1_damage_counter - 25 * MonsterA_Atk2_damage_counter);
Set(Health, If(100 - HealthChange >= 100, 100,100 - HealthChange))``````

What does it say when you hover your mouse over the bold red lines under HealthChange?

Resident Rockstar

You will want to leverage the Min and Max functions to accomplish this.

For example, I have buttons and their On Select functions

• I have a button which will increment a variable, by 20
• UpdateContext({locCounter: Min(locCounter + 20,100)}) - recommended to use local variable
• Set(gblCounter, Min(gblCounter + 20,100))
• I have a button which will decrement a variable, by 15
• UpdateContext({locCounter: Max(locCounter -15,0)}) - recommended to use local variable
• Set(gblCounter, Max(gblCounter -15,0))

The Min function returns the smallest of the included values. This would limit the variable from exceeding 100.

The Max function returns the largest of the included values. This would limit the variable from going below 0.

You will just need to update your local, or global, variable accordingly.

Frequent Visitor

It's weird, I type in just the first variable:

Set( Health,100) and it gives the error red underline.

Error text is "Behavior function in a non-behavior property. You can't use this property to change values elsewhere in the app."

I checked the app to see if I had "Health" named anywhere in the app and I do not. I even tried "cows" and still the error message. Everything you have seems right and The variable setup is right, just not sure why it hates me. 😞

Resident Rockstar

You appear to have the Text property of the control selected. 🙂

Set is a function, and needs to be invoked by an OnSelect, or OrChange, etc.

Frequent Visitor

Ok, did not know this and I think I may have complicated the issue. I had the variable trying to operate inside a text box. The variables do not work there. When I put it in a button, the variable worked. So now I need to get the updated Health Points to stay updated based on what worked the last turn. I am attaching a screenshot of what my screen looks like if that helps. I am hoping the steps from before will work in the buttons.

I appreciate the help.

Resident Rockstar

To track things, in order, you'll need the collection to show the records (which you have).

For the buttons at the top

• OnSelect functions to increment the variable as needed)
• Apply the same logic noted above about Min/Max to keep the variable from going out of range

For the counters (squares) are the bottom

• Text values, which show the current values
• CountIf( Collection, Success = True)

For the running HP

• Text value, showing the variable being manipulated by the buttons at the top

You will need to account for scenarios where the variable gets updated to 0 (i.e. Death), to prompt the app to reset the collection and variables as needed.

Frequent Visitor

Can you put an If statement in the UpdateContext piece? I use a random number to determine if the attack is successful and if the counter changes. So, I have an 80% chance that the first attack will work. At the moment, I have a Random number that is created. If the random number is 0.80 or lower, I have a statement that says "Success" or if over 0.80, its a miss and my counter does not increase.

So, the question is, with the formula you provided earlier, can an if statement work in the UpdateContext to account for a hit/miss?

Thank you again!

Announcements

#### Power Platform Connections Ep 14 | J. Panchal | Thursday, 18 May 2023

Episode Fourteen of Power Platform Connections sees David Warner and Hugo Bernier talk to Microsoft PM Jocelyn Panchal, alongside the latest news, videos, product reviews, and community blogs.   Use the hashtag #PowerPlatformConnects on social media for a chance to have your work featured on the show.      Show schedule in this episode:  00:00 Cold Open 00:32 Show Intro 01:10 Jocelyn Panchal Interview 24:10 Blogs & Articles 29:50 Outro & Bloopers  Check out the blogs and articles featured in this week’s episode:   https://www.nathalieleenders.com/Blog/index.php/;focus=STRATP_com_cm4all_wdn_Flatpress_42136159&path=?x=entry:entry230511-101930#C_STRATP_com_cm4all_wdn_Flatpress_42136159__-anchor  @NathLeenders https://www.keithatherton.com/posts/2023-05-12-msbuild2023-cloud-skills-challenge/  @MrKeithAtherton https://elliskarim.com/2023/05/13/how-to-find-files-in-onedrive-that-match-a-naming-pattern/  @MrCaptainKarim https://www.linkedin.com/pulse/my-fond-memories-scottish-summit-2022-pranav-khurana/ @pranavkhuranauk https://www.linkedin.com/feed/update/urn:li:activity:7061777660745560064/?updateEntityUrn=urn%3Ali%3Afs_feedUpdate%3A%28V2%2Curn%3Ali%3Aactivity%3A7061777660745560064%29  @thevictordantas  Action requested: Feel free to provide feedback on how we can make our community more inclusive and diverse.  This episode premiered live on our YouTube at 12pm PST on Thursday 18th May 2023.  Video series available at Power Platform Community YouTube channel.  Upcoming events:  Power Apps Developers Summit – May 19-20th - London European Power Platform conference – Jun. 20-22nd - Dublin Microsoft Power Platform Conference – Oct. 3-5th - Las Vegas  Join our Communities:  Power Apps Community Power Automate Community Power Virtual Agents Community Power Pages Community  If you’d like to hear from a specific community member in an upcoming recording and/or have specific questions for the Power Platform Connections team, please let us know. We will do our best to address all your requests or questions.

#### Microsoft Power Platform Conference | Registration Open | Oct. 3-5 2023

We are so excited to see you for the Microsoft Power Platform Conference in Las Vegas October 3-5 2023! But first, let's take a look back at some fun moments and the best community in tech from MPPC 2022 in Orlando, Florida.   Featuring guest speakers such as Charles Lamanna, Heather Cook, Julie Strauss, Nirav Shah, Ryan Cunningham, Sangya Singh, Stephen Siciliano, Hugo Bernier and many more.   Register today: https://www.powerplatformconf.com/

#### Check out the new Power Platform Communities Front Door Experience!

We are excited to share the ‘Power Platform Communities Front Door’ experience with you!   Front Door brings together content from all the Power Platform communities into a single place for our community members, customers and low-code, no-code enthusiasts to learn, share and engage with peers, advocates, community program managers and our product team members. There are a host of features and new capabilities now available on Power Platform Communities Front Door to make content more discoverable for all power product community users which includes ForumsUser GroupsEventsCommunity highlightsCommunity by numbersLinks to all communities Users can see top discussions from across all the Power Platform communities and easily navigate to the latest or trending posts for further interaction. Additionally, they can filter to individual products as well.       Users can filter and browse the user group events from all power platform products with feature parity to existing community user group experience and added filtering capabilities.     Users can now explore user groups on the Power Platform Front Door landing page with capability to view all products in Power Platform.    Explore Power Platform Communities Front Door today. Visit Power Platform Community Front door to easily navigate to the different product communities, view a roll up of user groups, events and forums.