What are Named Formulas in Canvas Apps?

Hi Folks,

Most of us know how to declare variables in our program…declaring a Var variable type is simplest one possible either in C#, Javascript or any scripting language.

Do you know that we can declare variables similarly in Canvas Apps using PowerFx…? A feature which was Generally available now..it’s none other than Named formulas.

With the named formulas, we can easily define and declare variables and only they were run when required, you don’t need to initialize it before hand, thus improving performance. Here you don’t even need to use Var while declaring the variable, you just name it…Also it offers below advantages.

  • The formulaโ€™s value is always available.ย ย There is no timing dependency, noย App.OnStartย that must run first before the value is set, no time in which the formulaโ€™s value is incorrect. ย Named formulas can refer to each other in any order, so long as they donโ€™t create a circular reference. ย They can be calculated in parallel.
  • 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.  
  • The formulaโ€™s definition is immutable.  The definition in App.Formulas is the single source of truth and the value canโ€™t be changed somewhere else in the app.  With variables, it is possible that some code unexpectedly changes a value, but this is not possible with named formulas. That doesnโ€™t mean a formulaโ€™s value needs to be static โ€“ it can change โ€“ but only if dependencies change.
  • The formulaโ€™s calculation can be deferred.ย ย Because its value it immutable, it can always be calculated when needed, which means it need not actually be calculated until it is actually needed. If the value is never used, the formula need never be calculated. ย Formula values that arenโ€™t used untilย screen2ย of an app is displayed need not be calculated until screenย screen2ย is visible. ย This can dramatically improve app load time and declarative in nature.
  • Named formulas is an Excel concept.ย Power Fx leverages Excel concepts where possible since so many people know Excel well. ย 

Tip: Use App.Formulas instead of App.OnStart

The best way to reduce loading time for both Power Apps Studio and your app is to replace variable and collection initialization inย App.OnStartย withย named formulas inย App.Formulas.

Example without Named Formulas:

ClearCollect(
MySplashSelectionsCollection,
{
MySystemCol: First(
Filter(
Regions,
Region = MyParamRegion
)
).System.'System Name',
MyRegionCol: First(
Filter(
Regions,
Region = MyParamRegion
)
).'Region Name',
MyFacilityCol: ParamFacility,
MyFacilityColID: LookUp(
FacilitiesList,
Id = GUID(Param("FacilityID"))
).Id
}
);

Example with Named Formulas:

MyRegion = LookUp(
Regions,
Region = MyParamRegion
);
MyFacility = LookUp(
FacilitiesList,
Id = GUID(Param("FacilityID")
);
MySplashSelectionsCollection =
{
MySystemCol: MyRegion.System.'System Name',
MyRegionCol: MyRegion.'Region Name',
MyFacilityCol: ParamFacility,
MyFacilityColID: MyFacility.Id
};

You see the difference between the above two, the one with named formulas is more readable while improving your App performance. Isn’t great…?

References:

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

https://learn.microsoft.com/en-gb/power-apps/maker/canvas-apps/working-with-large-apps?WT.mc_id=5004279#use-appformulas-instead-of-apponstart

Cheers,

PMDY