Microsoft recently released User Defined Functions in Preview. In this post I’m going to compare User Defined Functions with Named Formulas.

What are Named Formulas?
Table of Contents
In my recent posts about named formulas I’m already looking at some of the uses of Named Formulas.
How to use Named Formulas in Power Apps
Fast performing nested galleries with Named Formulas in Power Apps
In short Named Formulas make data available throughout the app and as its dependencies are updated the named formulas are updated as well.

Using Named Formulas
Now you can use Named Formulas info example a gallery and as the values in the dropdowns are selected the gallery will always show up to date data, as it is using the named formula to do all the filtering.

Using named formulas don’t only simplify the code across your app, the performance of the app will also improve an awful lot.
User Defined Functions
Now User Defined Functions are a different programming structure available in Power Apps. But there are some similarities.

First of all User Defined Functions as specified within the Formulas properties of your app. Just like Named Formulas.
In the above example we can see that the structure is fairly similar. The noticeable difference of course the typing of the parameters and the output or return value of the function.
nf_FilteredFunction(parFruit: Text, parColour:Text): Number = CountRows(Filter(nf_Data, Fruit = parFruit && Colour = parColour));
Parameter types of the User Defined Function
As you type, the various types of data can be selected:

And for the output we can user the same options.

Most of the data types are obvious, and will not need much of an explanation. But there are two types that may need an explanation
UntypedObject type
The UnTypedObject type is used for tables and records. We saw that in the past with the ParseJSON function in Power Apps.
Void type
The Void type is when nothing is being returned. Hmm, pretty useless. This reminds me a bit of programming in languages like C# and Pascal where you have functions/procedure/methods that return data or they don’t return data but do things.
As Named functions however we can’t use these two types yet.
Back to the example
So we can pass in parameters and then get some simple data back.
nf_FilteredFunction(parFruit: Text, parColour:Text): Number = CountRows(Filter(nf_Data, Fruit = parFruit && Colour = parColour));
So this is still very useful. I’m seeing so many apps that repeat similar code and make the apps look complicated. By adding User Defined Function to your app your app becomes almost self documenting (if you name your user defined function properly). And of course all complexity is moved into the the function rather than you gallery or your controls on the screens.
Where we saw that Named Formulas update themselves, for User Defined Functions we just call them and then they return their values. So for example if we use the above function in a label, the label will show us the number of records returned by the filter. So once again we are getting current values back, without any need for resetting controls or data sources.
Once this feature is out of Preview, it will be interesting to update all your existing apps.
Some thoughts
As you create many Named Formulas and User Defined Functions all your app complex pieces of code will appear in the App Formulas property. It would be great if in the future we could modularise this property better.
Currently I add comments in my code to manage the Formula property. But it can be difficult, as all code now appears in one place. How do you manage your Formulas, please feel free to copy your ideas in the comment below.
Discover more from SharePains
Subscribe to get the latest posts sent to your email.