Power Apps User Defined Functions: Write Code Once And Reuse (2025)

With Power Apps user defined functions we can write a formula once and reuse the logic many times throughout an app. To do this we choose a function name, determine the inputs and their data types and write a formula to evaluate. Then we can call the function from anywhere in the app. I love this new extension of the Power Apps named formulas feature and I’m looking forward to using it in all of my builds.

Table of Contents• Basic Example: The Multiply Numbers FunctionEnable Power Apps User Defined Functions FeatureCreate The User Defined Function In The App Formulas PropertyCall The Multiply Numbers User Defined FunctionAdvanced Example: The Net Work Days FunctionDefine The NetWorkDays FunctionPass Arguments To NetWorkDays Using Input ControlsError Handling For User Defined FunctionsCurrent Limitations Of User Defined Functions

Basic Example: The Multiply Numbers Function

The first Power Apps user defined function we will create together is the MultiplyNumbers function. It takes two numbers as inputs and outputs their product.

Power Apps User Defined Functions: Write Code Once And Reuse (1)

Enable Power Apps User Defined Functions Feature

Power Apps user defined functions are currently an experimental feature and must be enabled. Open the advanced settings and perform the following steps:

  • Go to the Support tab and update the authoring version 3.24013 or greater
  • Browse to the Upcoming Features tab and toggle on new analysis engine
  • Then toggle on the user defined functions feature
Power Apps User Defined Functions: Write Code Once And Reuse (2)

Create The User Defined Function In The App Formulas Property

The MultiplyNumbers function is created as a Power Apps named formula with additional syntax for inputs and outputs.

Power Apps User Defined Functions: Write Code Once And Reuse (3)
MultiplyNumbers(Number1:Number, Number2:Number):Number = Number1 * Number2;

The syntax for a Power Apps user defined function is:

FunctionName(Parameter1:DataType1, Parameter2:DataType2):OutputDataType = Formula
  • FunctionName – used to invoke the function
  • Parameter – the name of the input. One or more inputs are allowed
  • DataType – argument passed into the function must match this data type
  • OutputDataType – output of the function will be in this data type
  • Formula – the result of this formula is the output of the function

Here is the list of available data types:

Power Apps User Defined Functions: Write Code Once And Reuse (4)

Call The Multiply Numbers User Defined Function

Add a text control to the app and call the MultiplyNumbers function just as you would any other Power Apps function.

Power Apps User Defined Functions: Write Code Once And Reuse (5)

Use this code in the Text property of the text control. The result of multiplying 3 x 5 = 15.

MultiplyNumbers(3,5)

Advanced Example: The Net Work Days Function

The second Power Apps user defined function we will create is the NetWorkDays function from Microsoft Excel. It takes a start date and and end date as arguments and returns the number of days between them excluding weekends (Saturday and Sunday).

Power Apps User Defined Functions: Write Code Once And Reuse (6)

Define The NetWorkDays Function

The NetWorkDays function is defined in the Formulas property of the app.

Power Apps User Defined Functions: Write Code Once And Reuse (7)

Copy and paste this code in the Formulas property of the app. It creates a single column table of all the days between the start date and the end date, adds another true/false column named IsWeekday which checks if the date is between Monday-Friday and then sums the number of true/false values. We can sum the true/false values because true is represented as 1 and false is represented by 0.

NetWorkDays(StartDate:DateTime, EndDate:DateTime):Number = Sum( AddColumns( ForAll( Sequence( DateDiff( StartDate, EndDate, TimeUnit.Days )+1 ), StartDate + Value ), "IsWeekday", Weekday(Value) in [2,3,4,5,6] ), IsWeekday);

Pass Arguments To NetWorkDays Using Input Controls

We can pass arguments to the function using Power Apps input controls. Insert two date picker controls onto the screen. One to select the start date and the other to capture the end date.

Power Apps User Defined Functions: Write Code Once And Reuse (8)

Then add a text control to the screen and use this code in the Text property to call the NetWorkDays function.

NetWorkDays(DatePicker_StartDate.SelectedDate, DatePicker_EndDate.SelectedDate)

The result showing in the text control changes as the date pickers are updated.

Power Apps User Defined Functions: Write Code Once And Reuse (9)

Error Handling For User Defined Functions

User defined functions can result in an error so it is important to include error handling in their definition. For the NetWorkDays function, an error appears when the end date is earlier than than the start date.

Power Apps User Defined Functions: Write Code Once And Reuse (10)

Wrap the NetWorkDays function with the Power Apps IfError function to output 0 whenever an error occurs.

NetWorkDays(StartDate:DateTime, EndDate:DateTime):Number = IfError( Sum( AddColumns( ForAll( Sequence( DateDiff( StartDate, EndDate, TimeUnit.Days )+1 ), StartDate + Value ), "IsWeekday", Weekday(Value) in [2,3,4,5,6] ), IsWeekday ), 0);

Current Limitations Of User Defined Functions

Power Apps user defined functions are an experimental feature and have the following limitations. Please leave any other limitations you find in the comments section and I will add them to the list.

  • All parameters are required parameters. There is no way to set optional parameters.
  • Data types for record and table are missing. They cannot be used as inputs/ouputs.

Did You Enjoy This Article? 😺

Subscribe to get new Power Apps articles sent to your inbox each week for FREE

Questions?

If you have any questions or feedback about Power Apps User Defined Functions: Write Code Once And Reuse please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Power Apps User Defined Functions: Write Code Once And Reuse (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6332

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.