Using Named Formulas Instead of Global Variables and Context Variables in Power Apps
When building applications in Power Apps, makers (people who create applications with Power Apps) often store reusable formulas or expressions for control properties such as Text, Color, and Fill, which are written using Power Fx, a low-code programming language used in Microsoft Power Platform. Storing these formulas enable makers to define simple, complex, and frequently used expressions once and reuse them throughout the application. For example, when building an application, a maker might need to display the full name of the currently logged in user on multiple screens or controls. Instead of writing an expression that returns the full name of the user multiple times, the maker can just define the expression once and reuse it on the multiple screens or controls that need the full name displayed. This encourages consistency and lowers the possibility of encountering errors that might arise when repeating expressions, especially with complex ones.
Power Apps lets you store formulas inside of global variables or context variables. Expressions or formulas defined with global variables can be accessed and used from anywhere within the Power Apps application, while expressions or formulas defined with context variables can only be used from within the specific screen in which the context variable was initialized.
You use the Set function to initialize a global variable in either the App.OnStart property or the OnSelect property of controls. You initialize context variables, on the other hand, using the UpdateContext function in either the Screen.OnVisible property or the OnSelect property of controls in a specific screen.
Downsides to Global Variables and Context Variables in Power Apps
When you initialize global variables with either the App.OnStart property or the OnSelect property of controls, the performance of your application might be implicated. With variables initialized in the App Onstart property, the app may take longer to initially load because all the variables defined must be initiated first before the application becomes fully loaded. In addition, using the OnSelect property to define global variables impacts the performance of the application as redundant initializations will be required for every variable defined in the controls. For example, assuming you have multiple buttons with a global variable defined on the OnSelect property in your application. This will mean that each button will have to initialize its own variable differently every time it is loaded and therefore causing repeated initializations.
When you initialize multiple context variables on different screens, you increase the complexity of your application. This complexity results from the necessity of having to manage and keep track of multiple variables used on different screens. Multiple context variables on different screens also interferes with the consistency of how the expressions are used. This arises when you define similar variables with different values on different screens, thus making it difficult to keep your application's logic flow clear and consistent. In addition, the usage of similar context variables differently will eventually cause confusion and understanding the expressions will be difficult therefore making it hard to maintain the application.
Minimizing the Downsides with Named Formulas
To help solve the performance problems encountered when using global variables and context variables, Microsoft has introduced a new feature called named formulas, an idea adopted from Microsoft Excel. In an Excel spreadsheet, you can name formulas with the Name Manager and use them from anywhere within the spreadsheet. A similar feature is currently in the preview stage, and it is going to be used with the Power Fx language in Power Apps.
Named formulas allow app makers to name the formulas or expressions that defines the values returned by the control properties used in the application with the normal low-code Power Fx language. Like global variables and Excel formulas, named formulas can also be used from anywhere within the application. I will describe the benefits of named formulas and show you how to use them in the next section of this article.
Using named formulas instead of global or context variables is a much better way to define formulas and expressions to be used within an application in various aspects:
- Reduced application load time: Using named formulas in your application does not add to your app’s initial loading time as expressions used are evaluated after the application has been initialized fully.
- Easier maintenance: With named formulas, you only define the expressions once and use it anywhere hence in case there is a need to make changes on some part of the expression, it is done on one location and then effected throughout the app. On the other hand, with context variables you must make changes on every screen that has the context variable initialized. If a change is made on one screen, it will only be affected on that specific screen. This also helps minimize errors during updates.
- Reusability: Named formulas can be used anywhere in the application unlike context variables which can only be used in a specific screen.
- The value of the formula is always accessible: With named formulas, there is no app.OnStart property required to run first for the value to be accessible, so you can always access the values in named formulas anytime for the application.
- Simple to understand and define: With named formulas, you can give descriptive and meaningful names to complex expressions, making them easy to comprehend.
Enable the Named Formulas Preview
As mentioned earlier, named formulas is a new feature that is currently in the preview stage and therefore it is not directly available to be used within an application in Power Apps. You must enable the feature before using it. I highly recommend trying it before the official release.
To enable Named Formulas, inside the application you are building, from the menu at the top click the three dots, (just to the right of New screen) and from that menu select Settings > Upcoming features > Preview and then scroll down to Named Formulas, and set the feature to ON to enable it (Figure 1). Now you can try using named formulas to define expressions in addition to using global variables or context variables.
How to Initialize and Use Named Formulas
Named formulas are defined in the App.Formulas property of the application with a clear, simple, and easy-to-understand syntax. To help in understanding this, I will talk through an example.
Assume that you want to store the values of the currently logged-in user’s Username, the background color, and the user’s email, because you want to use the values in other parts of the application. This code snippet shows how you will initialize it with named formulas. The named formulas are on the left side of the equal sign.
Background_Color = Color.White;
Username = User().FullName;
UserEmail = User().Email;
I will demonstrate how to use the above-mentioned named formulas in your application from Power Apps. Assume that you have an application that you are building, and that you have turned on the named formulas feature as shown in Figure 1.
Open the Power Apps design studio and select App under the tree view (Figure 2). Scroll down the App properties to locate the Formulas property. Next to the property is the expressions bar where you will write your expressions. Copy the above-mentioned formulas into the expressions bar and exit.
By doing this, you have initialized three named formulas that will return values that can be reused anywhere within the application.
To use the named formulas initialized above in Figure 2, I will use a text label to display the username of the currently logged-in user. Add a text label control to any screen that you want and locate the control’s Text property. Next to the property is the expressions bar, copy the name assigned to the formula that will return the user’s username, as shown in Figure 3.
From looking at the previous code snippet, the syntax used when defining a named formula is amazingly simple and the user can easily understand what value will be returned by a certain expression.
The code snippet below shows how to define the same expressions with global variables.
Set(‘User Email’, User().Email);
The syntax used with global variables is different from the one used in named formulas and it is slightly complex compared to named formulas.
To initialize a global variable in Power Apps, you will use the same approach to that of named formulas. The only difference is that you will define it in the OnStart property of the app (Figure 4) and not in the Formulas property.