Power Community

Power Community

Power Apps Named Formulas


Last Friday, September 16th, Microsoft introduced Name Formulas to Power FX. Named Formulas, and old concept that has been part of Microsoft Excel for a long time has now been brough to Power FX (currently experimental only). With Named Formulas we can simplify app initialization, reduce load time and logic and improve overall app maintenance.

In Microsoft Excel, you can provide a name to any cell or group of cell, and refer to that name throughout the workbook. In Power FX, this allows us to bind a name to a formula that isn’t necessarily a cell. Today in Power FX, formulas are written for the properties of controls, and these can be referred from other formulas to reuse the results of that particular calculation. We are limited though to the controls and their properties. What can be done in Power FX and Named Formulas is create our own properties and points of reuse. Let’s take a look at a few examples:

AppTheme = {
    ForeColor: Black,
    BackColor: RGBA(98, 100, 107, 1)

UserEmailAddress = User().Email;
UserName = User().FullName;
UserRecord = LookUp(Users, 'Primary Email' = UserEmailAddress);
UserTitle = UserRecord.Title;
UserPhone = If (UserRecord.'Preferred Phone' = 'Preferred Phone (Users)'.'Main Phone',
                UserRecord.'Main Phone',
                UserRecord.'Mobile Phone')

The formulas shown above express how to calculate the User Email Address, User Name, User Title and User Phone Number from other values. This logic is now encapsulated and can be used throughout the app and updated in this one location. It can be changed from using the Dataverse Users table to using the Office 365 Connector without the need of changing the source code throughout the entire app. Changing the code in this one place, will reflect througout the application.

The formulas don’t specify when these values should be calculated, they are only a template.

So what is the difference between using this formulas in the App.Formulas versus the App.OnStart. Couldn’t they accomplish the same thing? We can definitely continue to use the App.OnStart and use the Set statement. There is always a need for that. However Named formulas have their own advantages:

  • Always available: There is no particular timing dependency for Named formulas. When using the App.OnStart, it must be run first prior to having the value set. This is not the situation for Named formulas. Also, Named formulas can refer to each other in any order and calculated in parallel. However, you must avoid circular references of one Named formula calling another Named formula, which in return calls the previous formula.
  • Always updated: Named formulas can perform calculations on control properties or database records, and as the values of these changes, the value of the formula will be updated accordingly. There is no need of manual updates to the formular value (something that is required when using a variable).
  • Immutable: The definition in the App.Formula is the single source of truth and that value cannot be changed somewhere else in the app. Variables, in contradiction to Named formulas can be updated throughout the app.
  • Deferred calculations: Because the value of Named formulas is immutable, it can be calculated when necessary and there is no need to calculate it until it is actually needed. If the value is never used, the formula never needs to be executed or run. This type of deferred calculations can improve application load time and provide system optimization when they are computed.
  • Excel concept: Power Fx leverages Excel concepts whenever possible due to the large number of Excel users. This feature is similar to Named cells in Excel, and are recalculated automatically just like in a spreadsheet.

Currently there are also many cases when the App.OnStart is used for global declarations of properties using Set, using a value that can be reused throughout the app, similar to the code snippet below.

// App.OnStart
Set (AppTheme,
    ForeColor: Black,
    BackColor: RGBA(98, 100, 107, 1)

The same can be done using Named formulas as shown in the following code snippet:

// App.Formulas
AppTheme = {
    ForeColor: Black,
    BackColor: RGBA(98, 100, 107, 1)

This is very similar, and might not be a reasoning to start using App.Formulas, but let’s take this a step further. What if my formulas actually needed to get values from a web service or Rest API. Would that be enough reason Imagine that you need to pull some data from Graph API, and you have a custom connector to get that data. Maybe that would make more sense.

IsUserAdmin = GraphConnector.CheckIsUserAdmin(UserEmailAddress);

There are many use cases why Named formulas can be beneficial. Although this is still experimental, I would recommend giving it a try and giving feedback on what you would like to see in the product. I know that I am thrilled to see this end up in the product in the near future and use it in my projects.

Source: https://powerapps.microsoft.com/en-us/blog/power-fx-introducing-named-formulas/

This post was originally published on this site

- Advertisement -spot_img


Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

Enable Early Access Wave 1 2023 | Power Platform

Like every 6 months, enabling Early Access is one of the best things to look forward to as Microsoft...

More Articles Like This

- Advertisement -spot_img