Power Apps Variables
An overview of when to use each type of variable (Formula, Global, Local, With) within a Canvas Power App.
I've been building Power Apps for a few years. A key part of writing a Power App is using variables.
Initially, I used global variables for everything, which caused major bugs and complexity. Then, I opted for simpler local variables and passed them between screens using the Navigate function. This approach is more deliberate and controlled.
I started using Formulas when this feature became available and learned the power of the With function to simplify complex formulas.
Formulas
Power Apps formulas execute code when referenced. Microsoft recommends using them because they are optimized and do not need to store variable data in memory.
- Create formulas in the App.Formulas property.
- When accessed, formulas run.
- Your formula would reflect any changes to the underlying data instantly without input.
Global Variables
Global Variables are used when a variable is referenced across multiple screens and needs updates at different stages.
- The Set function is used to create global variables.
- The value of the variable does not change unless you update it yourself using another Set function.
- If you need to use a variable across screens, for example, passing a current record, use a Local Variable instead. Then, pass it to the next screen within your Navigate function.
- Use global variables sparingly. They complicate larger apps.
Local Variables
Use local variables for screen-specific variables. When you leave the screen, the variable's value is cleared from memory.
- Use the UpdateContext function to create local variables.
- You can pass local variables between screens.
- If you need a temporary result within a single formula, consider the With function instead.
With Function
The Power Apps With function is a powerful way to break down complex formulas into smaller pieces, while not a variable.
- Here are examples of using With Functions.
- Cannot be reused outside the With function itself.