How to use Named Formulas in Power Apps

Named Formulas were recently introduced into Power Apps. They can be great, but they do have a bit of a user guide.

The principal ideas of Named Formulas

Within the Formulas property you can specify as many Named Formulas as you want. As shown below this can then be used to take complex formulas away from the code in your app and put more of the complex code in a single place in formulas.

How to use Named Formulas in Power Apps
How to use Named Formulas in Power Apps 1

Notice that each named formula has to end with a semicolon ( ; ). Without this the named formula will not be valid. Even the last Named Formula has to have to closing semicolon.

Now for example if you wanted to do a complex calculation or filter a collection of records in multiple places in your app, Named Formulas means that you only need to do this in one place.

In this post I want to take one element of the earlier referenced post:

The formula’s value is always up to date.  The formula can perform a calculation that is dependent on control properties or database records, and as they change, the formula’s value automatically updates.  You don’t need to manually update the value as you do with a variable.  

Well, is it really?

Named Formula Example 1 – Data sources

I’m going to have a look at the following example:

nf_Now = Now();

If I wanted to create a named formula that depends on something that changes like time, then this named formula will run once but not ever again. Power Apps will not know that the Now function returns a different time every moment of the day.

And of course we should be glad that Power Apps loses track of time, otherwise my Named Function would permanently run and slow down my app.

In a similar way if other users update a data source, the app will not be aware of this and named formulas will not update until the data source is refreshed.

Named Formula Example 2 – Controls

Next example. I’m setting a Named Formula to the value of a Text Input box.

nf_ValueInInput = TextInput1.Text;

Nothing exiting here.

Named Formula used to collect the value of an Text Input box
Named Formula used to collect the value of an Text Input box

As a type the named formula is always up to date.

How to use Named Formulas in Power Apps
How to use Named Formulas in Power Apps 2

So that is great.

Named Formulas Example 3 – Forms

Now when I have a form on a screen and create a named formula like this (DataCardValue3_1 is my Email control in the screenshot below):

nf_EmailSupplied = !IsBlank(DataCardValue3_1.Text);

We will find that the named formula may return false when the value is set as a default value. While !IsBlank(DataCardValue3_1.Text); will return true.

How to use Named Formulas in Power Apps
How to use Named Formulas in Power Apps 3

If I then run my app and edit the content of my Email field, the named formula is updated as I would expect.

How to use Named Formulas in Power Apps
How to use Named Formulas in Power Apps 4

Is this a bug? Maybe, or maybe we just need to use the Named Formulas in the right way.

Remember Named Formulas optimize your app. So they are worth the investment, but do make your app use them the right way. Especially in combination with the New Analysis Engine and forms on other screens are a bit of a problem. But then we should really look at how we use the Named Formulas.

Triggering Named Formulas

Hey, didn’t we say they are always up to date? So why do we need to trigger refreshes of Named Formulas?

When we look at the following Named Formulas example:

nf_SortedMainForms = Sort(
    Filter(
        'Main Forms', 
        'Scheme Name'.Scheme = cmbSchemename_Home.Selected.Scheme
    ), 
    'Form ID', 
    SortOrder.Descending
);

There are a few elements in the above code that will trigger an update of the Named Formula.

  • Main Forms – The datasource
  • cmbSchemename_Home – a control on my screen

And that is it. So if we want the named formula to update we will need to update either of the above triggers. Now that will mean that whenever a user selects a value in the cmbSchemename_Home control my data will be up to date.

What triggers a Named Formula?

Now the important question, what updates a Named Formula?

Named Formulas are updated whenever something’s value in the expression changes. This could be:

  • A Datasource refresh
  • A user selecting a value in a drop down, typing text in a Text Input or any updates in any other control
  • Global variables that are used in Named Formula expressions
  • A form becoming visible as a user navigates to a screen.

Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.