cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Katdragon
Frequent Visitor

Setting the value of a global variable to the result of a with statement

Hi everyone,

If you've read some of my previous posts then you'll know I've created a Power App that keeps track of CPD points for people in my company (thanks to everyone who helped me!)

 

I've run into a bit of a problem though. I'm able to count the number of points for a particular user, but I just found out that there are restrictions on the number of points they can earn per type of activity they do. For example, they can get 1 CPD point for a webinar they attend, but they can only earn a maximum of 5 points per year. So if they attend seven webinars, they can still only get 5 points. Right now, my counter includes all of the session points they've accrued and not applied a restriction so if they've attended seven webinars it's showing 7 points.

 

This is the counter code that's currently sitting in the Text field of a label:

 

 

With(
    {
        webinarData: Filter(
            CPDtracker,
            User().FullName in Practitioner.DisplayName && CPDYear = "2023" && Format = "Webinar"
        )
    },
    Sum(
        webinarData,
        Points
    )
)

 

 


What I'd like to do is take the result of that calculation and put it in a variable to compare to a threshold value (ie, 5 points).

The logic will be:

If the result of the calculation <= threshold value, then set the global variable to the calculation value and pass it on to another screen that calculates the total number of points

Otherwise,

If the result of the calculation > threshold value then set the global variable to the threshold value and pass it on to another screen that calculates the total number of points.

 

My major issue is that I can't get the results of the current 'With' statement into a variable to compare to the threshold value.

I've set up the threshold values as global variables in the OnStart area of the App ie:

 

 

//Points threshold for Webinars is 5
Set (
    glblWebinarsThreshold,
    5
);

 

 

 For testing purposes I've used

 

//Test threshold for Webinars is 0
Set (
    glblTestThreshold,
    0
);

but for some reason I can't set up a variable to compare it to.

 

I've tried the following:

 

 

With(
    {
        webinarData: Filter(
            CPDtracker,
            User().FullName in Practitioner.DisplayName && CPDYear = "2023" && Format = "Webinar"
        )
    },
    Set(glblTestThreshold,Sum(
        webinarData,
        Points
    )
    )
)

 

 

which gives me the 'Behavior function in a non-behavior property' error

I'm probably doing something very stupid wrong - help please!

1 ACCEPTED SOLUTION

Accepted Solutions
poweractivate
Most Valuable Professional
Most Valuable Professional

@Katdragon 

Oh I know the issue, I have a mistake in my formula, the With must be nested again and I forgot to do that.
Try like this below:

With(
    {
        webinarData: Filter(
            CPDtracker,
            User().FullName in Practitioner.DisplayName && CPDYear = "2023" && Format = "Webinar"
        )
    },
    With(
        {
            webinarSum: Sum(
                webinarData,
                Points
            )
        },
        Set(
            gloWebinarPoints,
            If(
                webinarSum <= glblWebinarsThreshold,
                webinarSum,
                glblWebinarsThreshold
            )
        )
    )
)

See if it helps @Katdragon 

View solution in original post

8 REPLIES 8
poweractivate
Most Valuable Professional
Most Valuable Professional

I think the issue you're facing could be because you try to use the Set function within a non-behavior property.

In Power Apps, behavior functions like Set should be used within a property that supports behavior, such as the OnSelect property of a button.

 

To set the global variable based on the condition you described, you can follow these steps:

  1. Calculate the Webinar Points: First, calculate the sum of the webinar points in a context variable within a behavior property like OnSelect of a button.

  2. Apply the Threshold: Next, compare the sum to the threshold, and set the global variable accordingly.

Here's the formula to perform these steps:

In the OnSelect property of a button, you can write:

With(
    {
        webinarData: Filter(
            CPDtracker,
            User().FullName in Practitioner.DisplayName && CPDYear = "2023" && Format = "Webinar"
        ),
        webinarSum: Sum(
            webinarData,
            Points
        )
    },
    Set(
        gloWebinarPoints,
        If(
            webinarSum <= glblWebinarsThreshold,
            webinarSum,
            glblWebinarsThreshold
        )
    )
)

This formula should calculate the sum of webinar points and then check if the sum is less than or equal to the threshold value (glblWebinarsThreshold). If it is, it sets the global variable gloWebinarPoints to the sum. If the sum is greater than the threshold value, it sets the global variable to the threshold value.

 

You can then use the global variable gloWebinarPoints on other screens to calculate the total number of points.

 

See if it helps @Katdragon 

Thanks so much, I'll give it a go!

I was wondering if it would be possible to do this without using a button since I just want the display to come up automatically (should I use the OnStart area of the app?)

poweractivate
Most Valuable Professional
Most Valuable Professional

@Katdragon 

Yes you have two options for moving this whole formula away from the button OnSelect and to a more appropriate place for your scenario.

 

Option #2 is generally much better than Option #1:

 

Option #1: OnVisible of first screen - You can move the formula to the first screen. However, if you have only one screen right now, you should create a second blank screen, and then toggle back and forth in the Power Apps Editor between the two screens so you can fire the OnVisible of the first screen successfully in the Power Apps Editor.

 

You would do this again in the Editor whenever you want to simulate starting the app again.

It may also be possible to do this from preview mode, but only if the screen is being changed from another screen to the first screen. It's often easiest to just toggle select between the other screen and the first screen again outside of Preview mode, when doing it this way.

 

Option #2: OnStart of the App - but if you do move your formula to the OnStart property of App you must remember to click on the ellipses to the right of the word App in Tree View (and the Tree view itself is on the left side of the Power Apps Editor), then click on Run OnStart to fire the OnStart and run the formula successfully. 

 

poweractivate_0-1691255080659.png

 

You must do this each time you want to run the OnStart formula again and test in the Editor as if running the app from the very beginning.

The OnStart property of App does not fire automatically at any time in Power Apps Editor or Preview mode in the Editor, it only fires usually in the live published app at the beginning on play of the app.

To test in the Editor as if playing the app from the beginning, but remaining in the Power Apps Editor, you must click the Run OnStart manually each time.

 

See if it helps @Katdragon 

@poweractivate thanks so much. I'll probably go with Option #2 then. 

 

I'm having a teeny tiny problem with the Sum code (I tried this before and it didn't work) since it's not recognising 'Points' as being a column from CPDtracker. 

 

Any thoughts?

Thanks!

poweractivate
Most Valuable Professional
Most Valuable Professional

@Katdragon You have Points initially in your original formula:

With(
    {
        webinarData: Filter(
            CPDtracker,
            User().FullName in Practitioner.DisplayName && CPDYear = "2023" && Format = "Webinar"
        )
    },
    Sum(
        webinarData,
        Points
    )
)


Was this working before? It would seem it should not work in your original formula either as you gave it. If so, check if Points is the correct name, or if it should be something else on your side.

Also check if the formula I gave for some reason works only in the OnSelect of Button , but not in OnStart? 

I think  it's probable your original formula with Points didn't work either, but I am not sure. It's possible it did. It may matter where you placed the formulas. For now just tell me if the original formula with Points works for some reason and not the new one for some reason. Are you still using the original formula since it was sitting in a Text property of a label as you mentioned before?

 

 

Katdragon
Frequent Visitor

Hi @poweractivate ,

I created a new button to test my original code and got no errors. 

I then plugged your code into the button and got the errors. 

 

Edit: It also isn't recognising webinarData

Katdragon_0-1692764798984.png

Katdragon_1-1692764851622.png

 

 

 

I've attached a screenshot so you can see what I'm doing button-wise!btnscreenshot.PNG

poweractivate
Most Valuable Professional
Most Valuable Professional

@Katdragon 

Oh I know the issue, I have a mistake in my formula, the With must be nested again and I forgot to do that.
Try like this below:

With(
    {
        webinarData: Filter(
            CPDtracker,
            User().FullName in Practitioner.DisplayName && CPDYear = "2023" && Format = "Webinar"
        )
    },
    With(
        {
            webinarSum: Sum(
                webinarData,
                Points
            )
        },
        Set(
            gloWebinarPoints,
            If(
                webinarSum <= glblWebinarsThreshold,
                webinarSum,
                glblWebinarsThreshold
            )
        )
    )
)

See if it helps @Katdragon 

@poweractivate we have a winner with the code!

I'll give it a go on a new screen using the OnStart and let you know how things progress. 
I've accepted your answer with the new code.

 

Thanks again 🙂

Helpful resources

Announcements

Celebrating the May Super User of the Month: Laurens Martens

  @LaurensM  is an exceptional contributor to the Power Platform Community. Super Users like Laurens inspire others through their example, encouragement, and active participation. We are excited to celebrated Laurens as our Super User of the Month for May 2024.   Consistent Engagement:  He consistently engages with the community by answering forum questions, sharing insights, and providing solutions. Laurens dedication helps other users find answers and overcome challenges.   Community Expertise: As a Super User, Laurens plays a crucial role in maintaining a knowledge sharing environment. Always ensuring a positive experience for everyone.   Leadership: He shares valuable insights on community growth, engagement, and future trends. Their contributions help shape the Power Platform Community.   Congratulations, Laurens Martens, for your outstanding work! Keep inspiring others and making a difference in the community!   Keep up the fantastic work!        

Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Check Out the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community.  We can't wait to see what you "cook" up!      

Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!

Community Roundup: A Look Back at Our Last 10 Tuesday Tips

As we continue to grow and learn together, it's important to reflect on the valuable insights we've shared. For today's #TuesdayTip, we're excited to take a moment to look back at the last 10 tips we've shared in case you missed any or want to revisit them. Thanks for your incredible support for this series--we're so glad it was able to help so many of you navigate your community experience!   Getting Started in the Community An overview of everything you need to know about navigating the community on one page!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Ranks and YOU Have you ever wondered how your fellow community members ascend the ranks within our community? We explain everything about ranks and how to achieve points so you can climb up in the rankings! Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Powering Up Your Community Profile Your Community User Profile is how the Community knows you--so it's essential that it works the way you need it to! From changing your username to updating contact information, this Knowledge Base Article is your best resource for powering up your profile. Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Community Blogs--A Great Place to Start There's so much you'll discover in the Community Blogs, and we hope you'll check them out today!  Community Links: ○ Power Apps ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Unlocking Community Achievements and Earning Badges Across the Communities, you'll see badges on users profile that recognize and reward their engagement and contributions. Check out some details on Community badges--and find out more in the detailed link at the end of the article! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio    Blogging in the Community Interested in blogging? Everything you need to know on writing blogs in our four communities! Get started blogging across the Power Platform communities today! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Subscriptions & Notifications We don't want you to miss a thing in the community! Read all about how to subscribe to sections of our forums and how to setup your notifications! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Getting Started with Private Messages & Macros Do you want to enhance your communication in the Community and streamline your interactions? One of the best ways to do this is to ensure you are using Private Messaging--and the ever-handy macros that are available to you as a Community member! Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Community User Groups Learn everything about being part of, starting, or leading a User Group in the Power Platform Community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Update Your Community Profile Today! Keep your community profile up to date which is essential for staying connected and engaged with the community. Community Links: ○ Power Apps  ○ Power Automate  ○ Power Pages  ○ Copilot Studio   Thank you for being an integral part of our journey.   Here's to many more Tuesday Tips as we pave the way for a brighter, more connected future! As always, watch the News & Announcements for the next set of tips, coming soon!

Hear what's next for the Power Up Program

Hear from Principal Program Manager, Dimpi Gandhi, to discover the latest enhancements to the Microsoft #PowerUpProgram, including a new accelerated video-based curriculum crafted with the expertise of Microsoft MVPs, Rory Neary and Charlie Phipps-Bennett. If you’d like to hear what’s coming next, click the link below to sign up today! https://aka.ms/PowerUp  

Top Solution Authors
Top Kudoed Authors
Users online (2,937)