PowerApps Pro Tips | Using Variables

by AllisonCaldwell on ‎01-31-2017 10:22 AM - last edited on ‎05-24-2017 01:02 PM by Community Administrator

Like parameters, variables are very helpful, and can be used to increase user communication in your app. A variable in Microsoft PowerApps is used with the function UpdateContext. Below are a few ways you can start using variables immediately in your apps.

 

User Communication

In a scenario where you’re sending data back to a source and doing a Patch, at times the user can be confused about what’s happening (if anything is going on at all).  You might not want to take a user back to another page until the Patch is complete. To more effectively communicate to the user what is happening, try the following:

  1. Create a new app
  2. Add a data source
  3. Add a button that will send a record back to a source
  4. Add the following code before your patch runs: UpdateContext({MyVariable:”Sending Data”});
  5. Add the following code after your patch runs: UpdateContext({MyVariable:” “})
  6. Update the Button Text to: If(MyVariable = “Sending Data”,MyVariable,”Send”)

Now your user will see the button change text to let them know what is happening.

 

Sending Data from Page to Page

There may be times when you want the user to interact with data on screen 1, then pass some information to screen 2 based on an interaction on the first screen. This becomes useful when you have a list of items on the first screen, and you want to show details on the second screen. To try this out, use the following steps:

  1. Create a new app
  2. Add two screens
  3. On the first screen, add a drop down
  4. The OnChange property needs this: 
    Navigate(Screen2,Fade,{MyVariable:dropdown1.Selected.Value})
  5. On the second screen, add a text box
  6. Set the text property to: MyVariable

Now you can pass variables from page to page. Variables will only live on that page that you are on. If you need global variables, then you can use a collection.

 

Written by Power User and Managing Partner Evan Chaki, this post was originally published on the Confluent blog. Browse the blog for additional PowerApps Pro Tips, and let us know what topic you'd like to see next!

Comments
by Super User
on ‎02-01-2017 09:20 PM

Hi Evan Chaki,

 

Can you give an example of how you use a collection for a global variable?

 

I presume you use:

ClearCollect(collection,{var: data})

Do you ClearCollect again to update it, or do you use Patch(collection,First(collection),{var: data})?

 

When you reference the variable, do you use First(collection).var or is there a more efficient way?

 

I'm thinking of switching some of my UpdateContext variables to universal ones so I'm curious how others use them.

by Community Manager
on ‎02-22-2017 07:50 AM

Thank you mr-dang for posting these questions. I normally just reference the variable by it's name, and have never used the Patch() function to update variables. However, to give you an answer in the context of the blog @AllisonCaldwell will be responding shortly. Thank you for your questions, suggestions, and continued feedback!-Audrie

by AllisonCaldwell
on ‎03-09-2017 05:50 AM

Mr-Dang: Here's a response from Evan. Hope this helps! - Allison

---

For a global variable, you would load it into a collection so you can use it for any screen. It would look like this:  Collect(MyGlobals,{var:”data”})  

 

Then you can call this on any page, like this: First(MyGlobals).var 

 

To clear the collection, you could use: Clear(MyGlobals) 

 

To update it, you would use: Update(MyGlobals,First(MyGlobals),{Myvar:"New"}) 

 

You can also use the patch function with collections and data sources. I presume you use:

ClearCollect(collection,{var: data})

 

Do you ClearCollect again to update it, or do you use Patch(collection,First(collection),{var: data})?

 

When you reference the variable, do you use First(collection).var or is there a more efficient way? 

 

I'm thinking of switching some of my UpdateContext variables to universal ones, so I'm curious how others use them.

Meet Our Blog Authors