Power Apps User Defined Functions: Write Code Once And Reuse
Power Apps User Defined Functions: Write Code Once And Reuse Posted by - Matthew Devaney on - January 21, 2024 41 Comments With Power Apps user defined funct...
- Published
- Reading time
- 5 min read
Before you start
Is this guide for you?
- Best entry point
- Power Apps
- Time investment
- 5 min read
Imported reference from Matthew Devaney's blog for learning purposes. Original: https://www.matthewdevaney.com/power-apps-user-defined-functions-write-code-once-and-reuse/. Author: Matthew Devaney.
Power Apps User Defined Functions: Write Code Once And Reuse Posted by - Matthew Devaney on - January 21, 2024 41 Comments
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 Function
Enable Power Apps User Defined Functions Feature
Create The User Defined Function In The App Formulas Property
Call The Multiply Numbers User Defined Function
Advanced Example: The Net Work Days Function Define The NetWorkDays Function
Pass Arguments To NetWorkDays Using Input Controls
Error Handling For User Defined Functions
Current 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 .
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
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.
Write this code in the Formulas property of the App object.
MultiplyNumbers(Number1: Number , Number2 : Number ): Number = Number1 * Number2; Code language: JavaScript ( javascript )
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:
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 .
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). Define The NetWorkDays Function
The NetWorkDays function is defined in the Formulas property of the app.
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 ); Code language: JavaScript ( javascript )
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 .
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 ) Code language: CSS ( css )
The result showing in the text control changes as the date pickers are updated.
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.
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 ); Code language: JavaScript ( javascript )
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 Copilot Studio articles sent to your inbox each week for FREE Enter your email address Sign Me Up 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. Matthew Devaney M365 Copilot Power Apps
How To Create Copilot Custom UI Widgets In Power Apps
Tagged
Power Apps
Have a Microsoft 365 topic idea?
Share article suggestions, community session ideas, corrections, or real-world scenarios for future nextM365 learning notes.
Keep learning Microsoft 365
Explore more practical guides for SharePoint, Power Platform, Copilot Studio, migration, automation, governance, and security.
Continue learning
Related tutorials
Related questions
Related comparisons
Next action