After my introduction post about How to develop your apps in PowerApps. Today a second post about getting started with UpdateContext variables in Power Apps.

Example app

I’m going to start with a few simple buttons and a label in Power Apps.

UpdateContext in Power Apps Microsoft Office 365 buttons

I’m trying to make the reset button set the value in the box to 0 and the Add 1 button will increase the value in the blue box by 1.

I could probably do this without using a Context variable and update the value of the box that I’ve got. However for this example I’m going to update context a variable.

UpdateContext vs Set

To update a variable UpdateContext will help you. But before you can update a variable you will need to create a new variable called MyVariable you could use this example.

Set(MyVariable, 0)

The above will create you a global variable that can be used across screens. If you are after a variable that can be used within your current screen only then you can use UpdateContext instead:

UpdateContext({MyVariable: 0})

After you have done this you can now use UpdateContext to update the variable. The Set also sets the variable to 0.

UpdateContext({MyVariable: 0})

This will update the variable and set it to 0.

Unless you have created the variable you will not be able to reference it anywhere.

Set variable with UpdateContext
UpdateContext in Power Apps Microsoft Office 365 suggestion

To use the variable simply set the value of the blue box to MyVariable. As you type the name of your variable the full variable name will be suggested. If this doesn’t happen then you know that your variable hasn’t been initialized yet.

UpdateContext in Power Apps Microsoft Office 365
UpdateContext in Power Apps Microsoft Office 365 variables

If your variable doesn’t exist yet. Run your app and click the Reset button. 

To verify if your variable has been created and which type of variable was created you can check your variables in  PowerApps Studio.

This is where you can also find the current value of the Variable.

To make your apps in PowerApps document themselves it might be wise to consider proper naming of your variable.

This is where I often prefix my number values with ‘num’ So numMyVariable would be my preferred option.

Then rather than using MyVariable it might make sense to use a more descriptive name for your variables.

Then finally it is important that you keep the type of variable the same throughout your app.

UpdateContext in Power Apps Microsoft Office 365 variablevalue

So for example if you created a number variable with the Set function like this Set(MyVariable: 0) and then later you used UpdateContext({MyVariable: “Some Text”}) then you might expect some issues within your app.

Then now it’s time to implement my Add 1 button. Within Power Apps it is not just possible to set a variable you can use all sorts of functions to set a variable. So in my case I simply want to add one to the existing value. This can simply be done my using the UpdateContext function again. UpdateContext({MyVariable: MyVariable + 1}) will do the trick.

Why use UpdateContext rather than Set?

This is a very important question. I see many people use Set all the time and this approach works, but is it right? No.

Using UpdateContext will be better for your memory usage as the context is forgotten as soon as you navigate to a different page. Especially when you run on mobile devices this can be something to consider.

However, Set is easier and it may also be quicker as you only need to do things once throughout your app.

If you need any further help or if you would like me to extend this post feel free to leave a comment below. Even if you would like me to help you develop your applications just contact me and I’m happy to help.

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

5 thoughts on “UpdateContext in Power Apps”
  1. Buenos días, quisiera saber como puedo crear una variable que me permita restarla de uno en uno

    Translation: Good morning, I’d like to know how I can create a variable that allows me to subtract it one at a time

  2. What should i do to assign a dynamic value to a variable?
    Example : I want to assign a value from cds/sharepoint list , and after my task is completed , i update it using updatecontext , and i want to store the new value to the variable. So , when i login to the app next time , i want to see the updated value not the old one.
    Any help would be appreciated , thank you.

    1. Variables don’t go across sessions. This means that you will need to store your data in your data source ( SharePoint, CDS, or anything else) and when you start your app you will need to go to this data source to collect the value as stored in your data source.
      Most likely you would use filter functions to select the right piece of data form your data source in Power Apps.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue reading