Build Reusable Custom Functions in Power Apps with Power Fx
Create your own functions that accept parameters and return values, reducing duplicate logic and making your app easier to maintain.
Power Apps user‑defined functions (UDFs) let you write a formula once inside the App object and then call it from any screen or control. This reduces duplicate code and makes your logic easier to update. In this article, we'll explore how to enable and use UDFs, with two practical examples: a temperature converter and an invoice number generator.
Enabling the Feature
User‑defined functions are still in preview as of writing. To start using them, go to the Settings menu, then Upcoming features > Preview, and switch on User defined functions. In the Support tab, ensure your Authoring version is set to at least 3.24013. Higher versions also work.
Once enabled, all functions you create will be available in the app.
Defining a Simple Function
The logic for every user‑defined function lives in the Formulas property of the App object. You cannot define functions in any other object.
The syntax pattern is:
FunctionName(Param1:Type, Param2:Type, ...):ReturnType = Expression;
All parameter types must be one of the supported data types: Boolean, Color, Currency, Date, DateTime, GUID, Hyperlink, Image, Label, Media, Number, Password, PenSignal, Text, Time. Current limitations mean that Record and Table types are not available for parameters or return values (you can still use them internally in the expression body).
Let’s create a function that converts a Celsius temperature to either Fahrenheit or Kelvin.
ConvertTemperature(
Celsius:Number,
TargetUnit:Text
):Number =
Switch(
TargetUnit,
"F",
Celsius * 9/5 + 32,
"K",
Celsius + 273.15,
// default fallback
Celsius
);To test it, drop a label on any screen and set its Text to:
ConvertTemperature(100, "F") // Returns 212
You can also pass values from other controls, for example ConvertTemperature(Value(TextInput1.Text), Dropdown1.SelectedText.Value).
Building an Advanced Function with Error Handling
Real‑world functions should be resilient to bad inputs. Power Fx provides the IfError function, which you can wrap around your logic.
The next example generates a formatted invoice number based on a client code, the order date, and a sequence number. It validates the client code and ensures the returned string is always formatted as CLIENT-YYYYMMDD-XXXX.
GenerateInvoiceNumber(
ClientCode:Text,
OrderDate:DateTime,
SequenceNumber:Number
):Text =
IfError(
With(
{
AllowedCodes: ["ALPHA", "BETA", "GAMMA"],
PaddedSequence: Text(SequenceNumber, "0000")
},
If(
ClientCode in AllowedCodes,
ClientCode & "-" & Text(OrderDate, "yyyymmdd") & "-" & PaddedSequence,
"INVALID-CLIENT"
)
),
"ERROR"
);Here’s how you could call it from a label:
GenerateInvoiceNumber("ALPHA", DatePicker1.SelectedDate, 42)
// Returns "ALPHA-20260603-0042"If the client code is not in the allowed list, the function will return "INVALID-CLIENT". If any other error occurs, it returns "ERROR".
Performance and Delegation Considerations
User‑defined functions run on the client based on the expression they contain. They are not delegable; any data source operations inside the function (e.g., Filter or LookUp against a large table) will pull records to the client. Keep functions focused on lightweight calculations that do not depend on external data sets.
Because the Formulas property is evaluated reactively, the results of your functions update automatically when their input parameters change. There is no need to manually refresh.
Common Mistakes to Avoid
- Missing semicolons: Each function definition in the Formulas property must end with a semicolon (
;). - Using unsupported types as parameters: Record and Table are not allowed in the parameter list or return type (though you can still construct tables inside the function body using
ForAllwithSequence). - Referencing controls directly: The function body cannot reference screen controls; all inputs must come through parameters.
- Circular references: If function A calls function B and function B calls function A, a circular reference error occurs.
- Wrong authoring version: If the feature toggles are grayed out, double‑check the authoring version in Settings > Support.
When Should You Use UDFs?
Custom functions shine when you have the same formula repeated across multiple screens—for example, formatting a date in a company‑specific way, calculating a tax or discount, or constructing a display string. They also make maintenance easier: if the logic needs to change, you edit one place instead of dozens.
However, not every calculation needs a UDF. Simple standard formulas (like Sum or Concatenate) are perfectly fine inline. Reserve UDFs for real, repeated complexity.
References
- Original article: Power Apps User Defined Functions by Matthew Devaney
- Microsoft Learn: Power Fx formula reference
- Named formulas: Power Apps named formulas
Learn how small coding practices—like using descriptive names, flattening conditions, and simplifying logic—can make your apps easier to update and less error-prone.
Move past the gallery and discover the hidden patterns for validation, navigation, and smart submission handling in your data entry forms.
PowerShell unlocks admin capabilities that the Power Platform admin center simply doesn’t offer—from recovering deleted apps to blocking trial licenses. Here’s how to wield them safely.