The Big 5: Variables and Parameters in PowerApps
Photo by Ann H on Pexels.com
If you build PowerApps and search for solutions online, you’ll frequently encounter these five functions:
Set()
UpdateContext({})
With()
Navigate(..., ..., PARAMETER)
Param()
Bonus: You can also use collections (Collect
, ClearCollect
) for advanced scenarios.
Below, I’ll briefly explain how I use each and why you should consider them in your own apps.
1. Set()
The most well-known function. It sets the value of a global variable. Global variables are useful for storing values you want to use across multiple screens. Typical use cases:
- Setting style variables in
OnStart
- Storing the logged-in user
- Selecting an item from a gallery to use elsewhere
Set(
varUser,
User()
)
2. UpdateContext({})
The little brother of Set
. It sets the value of one or more context variables for the current screen. I mainly use this to toggle values (true/false) for showing/hiding elements, such as pop-ups or input windows.
UpdateContext({locPopUp: true});
UpdateContext({locPopUp: false})
3. With({})
A more recent discovery for me, With
stores variables exclusively for a function and they can only be referenced within that function. I use this mainly inside functions when I need to reuse a value multiple times.
With(
{locText: TextInput1.Text},
If(
IsBlank(locText),
"noText",
TextInput1.Text
)
)
4. Navigate(…, …, {PARAMETER})
This parameter feature is often overlooked. By adding two more commas after Navigate
, you can pass one or more parameters to the target screen using curly braces. These parameters are only available on the next screen (unless you store them in another variable or pass them again).
Navigate(
Screen1,
UnCover,
{locVariableNextScreen: "Yes we can use it"}
)
5. Param()
Provides access to parameters passed to the app when a user opens it. This is less commonly used, as it’s typically appended to the app’s URL. It allows you to navigate users to a specific page or pass values to the app.
Example: Passing the parameter Admin=true
in the URL. In OnStart
, set the global variable varAdmin
with the parameter value to enable admin settings.
https://apps.powerapps.com/play/543654354453543543?tenantId=dasfdgdsgsfsdfsdf&Admin=true
Set(varAdmin, Param("Admin"))
Bonus Tip: Boolean Toggle
If you use a variable as a boolean and want to always flip its value, use !
in front of the variable. This reverses the value each time it’s executed.
UpdateContext({locPopUp: !locPopUp})
Collections
For more advanced scenarios, consider using collections:
Collect()
ClearCollect()
Collections allow you to store tables of data locally in your app for flexible manipulation and display.
This article highlights the essential functions for managing variables and parameters in PowerApps, helping you build more dynamic and maintainable apps.
Leave a comment