Power Apps Variables
I've been building Power Apps for a few years now, and one critical part of writing a Power App is using variables.
Initially, I used global variables for everything, and later realized this caused major bugs and complexity. I then opted for the simpler Local variables and passed them from screen to screen using the Navigate function when necessary. This approach is more intentional and controlled.
In addition, I started using Formulas as much as possible (when this feature became available) and also learned the power of the With function to break down complex formulas.
Formulas
Formulas in Power Apps execute code when referenced. Microsoft recommends using them whenever possible, as Power Apps formulas are optimized and do not require storing variable data in memory.
- Create Formulas in the App.Formulas property.
- Formulas run when they are accessed.
- If the underlying data changes, your formula would reflect that instantly without your intervention.
Global Variables
Global Variables should be used when a variable is referenced across multiple screens and needs to be updated at various stages.
- Global variables are created using the Set function.
- 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, and pass it to the next screen within your Navigate function.
- Use sparingly. Global variables can complicate larger apps (I know from experience).
Local Variables
Use Local Variables when you need a variable only on the current screen. The variable's value is cleared from memory when you leave the screen.
- Create Local Variables by using the UpdateContext function.
- You can pass Local Variables between screens using the Navigate function.
- If you need a temporary result within a single formula, consider the With function instead.
With Function
The Power Apps With Function, while technically not a variable, is a powerful way to break down complex formulas into smaller pieces.
- Here are some examples of how to use With Functions.
- Cannot be reused outside the With function itself.
No spam, no sharing to third party. Only you and me.