Constant variables in Power Apps canvas apps – All you need to know about named variables in Power Apps

Welcome to my blog. I am back with another blog on Power Apps and today I am going to talk about a recently launched feature – “Named formulas“.

What is this feature all about? If you look at the documentation of named formulas, your first impression may be – “Is it a new way of declaring global variables in canvas apps?”.

And if you are thinking that way, you are not totally wrong. Yes the syntax differs and it’s also a way to declare a global variable.

But don’t mistake it as a replacement for the Set function. Named formulas are introduced for a specific purpose and have certain features which are not available with variables using the Set keyword. You will continue to use them interchangeably depending on your requirement.

Use named formulas if

  • Immutable formula definition – You want to declare and set a global variable in Power Apps only once. You cannot reset the variable. More of like a single source of truth and immutable.
  • Variable value is always up to date -The variable, if based on formula will always reflect the latest value.
  • You want to defer formula’s calculation -The variable, if based on formula, will not be evaluated until it is being used in one of the Power Apps screen.

Let’s explore these features.

As of time of writing this blog, named formulas are experimental. To enable them you need to navigate to App Settings -> Upcoming features -> Experimental -> Named formulas and enable the feature. Once you have enabled the feature, save the app and reload the app.

named formulas in power apps canvas apps

Once the feature is enabled, you shall see a new property named Formulas in the App object

named formulas in power apps canvas apps

As of the current version, this is the only place where you can define named formulas. Let’s get started with creating some variables.

named formulas in power apps canvas apps
defaultEmail = "placeholder@contoso.com";
BackColor = Color.Gray;
mostRecentContact = First(
    SortByColumns(
        Contacts,
        "createdon",
        SortOrder.Descending
    )
);

The first two variables are set to an implicit value and the second one is a query to contacts table to fetch the most recently created contact record.

Without saying, you can note the difference in syntax. You do not need to use the Set function. It is more aligned to the way we are accustomed to declaring variables in other languages and excel.

Next I set the variables to control properties.

named formulas in power apps canvas apps

Controls and their property mapping as below

  1. defaultEmail – Text property of the email label
  2. mostRecentContact.’First Name’ – Text property of the First name label
  3. backColor – Fill property of both the label controls

So let’s start looking at the behavioral differences from the regular Set function.

As told earlier, the variables set in App.Formulas are immutable and can’t be reset. For example – the below formula now throw an error.

named formulas in power apps canvas apps

The variable “defaultEmail” cannot be reset. Now you have a single source of truth and the variable name is a reserved keyword.

The next feature – The variable value is always updated. And what a feature this is. Let’s take the mostRecentContact variable formula.

mostRecentContact = First(
    SortByColumns(
        Contacts,
        "createdon",
        SortOrder.Descending
    )
);

Every time a contact record is created in the app, the value of the variable will be automatically updated. You do not need to reset the variable.

The video below illustrate the feature. Every time we navigate to the screen where we are using the Named variables, you can see the value in the First name label is updated to the most recently created contact.

Wonderful isn’t it?

Another great advantage of the named formulas is they are not evaluated until you use the variable somewhere in the APP. In the above video illustration, the formula is only getting revaluated when we navigate to the screen where we display App.Formulas variables.

While this change is subtle, the impact is huge. You can now defer formulas from executing in App.Formulas thereby reducing the application load time unlike formulas in App.OnStart. Power Apps know when a named formula is required and it will be evaluated in the nick of time.

While all this worked great in preview, seems like there is a bug with named formulas when you publish and play the canvas app. Below is what I see when I publish and play the app

Bug with named formulas when you publish canvas apps.

Strangely, none of the backColor or the defaultEmail values take effect. Seems like a bug for now. I will update the section when I find a resolution to this.

Hope this helped! You will also like the below posts.

Debajit Dutta
Business Solutions MVP