Building Canvas Apps in PowerApps presents a challenge. Microsoft has provided great built-in functions, but what if your App needed something other than the built-in functions? You would have to create workarounds—repeating formulas, juggling nested logic, and spending extra time managing it all. It worked, but it wasn’t ideal.
Now, Microsoft is upgrading the process by introducing the User-Defined Functions (UDFs)! This upcoming feature will let you create your own custom functions, perfectly tailored to your App’s needs.
Here’s why it’s useful:
Reuse Your Logic: Write it once, use it everywhere. No more repetitive code!
Simple Updates: Fix or tweak your function in one place, and it updates across your App.
Imagine you are building a Tax Calculator feature for Business and Salary income in your App. Previously, you might have to write the same formula on different buttons to calculate taxes for salary and business income.
With User-Defined Functions (UDFs), you can simply create a function like TaxCalculator() and reuse it across forms, pages, and buttons. Efficient and consistent, right?
Moreover, UDFs can be combined with user-defined types (UDTs) to handle complex data like records or tables in one go.
User-Defined Types (UDTs) are advanced features in PowerApps that enable App developers to create custom data types.
Prerequisites
Launch your application in Power Apps, then go to Settings in the command bar and select Updates. Next, within the Experimental tab, turn on both the User-Defined Functions and User-Defined types options to start adding UDFs and UDTs in your App.
Example Use Case of User-Defined Function
Let’s explore a simple example to see how User-Defined Functions (UDFs) can make your app smarter and cleaner.
Imagine you have a tax calculator over two forms: one for business owners and another for salaried employees. Each form has similar fields that are used to calculate tax for business and salary.
Depending on which button is clicked, the app should calculate the tax based on the user’s input fields like income, tax rate, deductions, etc.
Before UDFs, you might have written separate logic for each button on every form, making your app cluttered and hard to manage. But with UDFs, you can create a single reusable function and simply adjust the parameters based on the button clicked and the form it’s used on.
Here’s how it works:
Step-by-Step Process to Set Up UDFs:
Open your Canvas App in Power Apps.
Navigate to the Variables section in the left panel.
Click on the + New button.
Choose {x} Named Formula.
Once the Formula Bar appears, type in the definition of the UDFs using Power Fx syntax.
Below is a simple function that is added to calculate the tax based on the income and other details given by the user.
UDF Function:
TaxCalculator2(
Income: Number,
TaxRate: Number,
Deductions: Number,
Allowances: Number,
TDS: Number
): Number =
(Income – Deductions – Allowances) * (TaxRate / 100) – TDS;
Add this UDF function inside the PowerFx formula after selecting Named Formula.
Now I will be calling this function on multiple events, and in our scenario, I will be calling it on the click of Calculate buttons for business and salary income.
Here is my Salary tax calculator on the Canvas App form:
Here is another Business income tax calculator on another Canvas App form:
I have added a function on click of these buttons as you can see below, OnSelect is my event which will call my function named TaxCalculator()
Notify($”{TaxCalculator(
business_profit.Text,
business_taxRate.Text,
business_deductions.Text,
business_allowance.Text,
business_tds.Text)}”
)
Similarly, if the user clicks the Button added to the Salary Tax Calculator form then the same function will be triggered :
Notify($”{TaxCalculator(
salary_income.Text,
salary_taxRate.Text,
salary_deductions.Text,
salary_allowance.Text,
salary_tds.Text)}”
)
The different parameter is passed on the click of both buttons but the function is the same which is TaxCalculator().
In the current example for Salary Tax Calculation, we are taking inputs like Income, Tax Rate, Allowance, TDS, and Deductions from the form fields that the user will fill in.
Based on these inputs, the app calculates the tax and displays the result to the user, ensuring accurate and dynamic calculations.
Similarly, to Calculate the Business Income Tax, we have added a few fields on form page like, Gross revenue, COGs, Operating Expenses, Net Profit, Income, Deductions, Allowance, and TDS.
Example Use Case for User-Defined Types
Let’s dive into an example that shows the use case of User-Defined Types (UDTs) in simplifying how we work with custom or structured data type.
Imagine you’re building an App which is a product inventory system that handles orders. Each order has details like the Order ID, Customer Name, Status, Delivery Date, and more. Before UDTs, you’d manage these fields individually, which could lead to messy code and repeated work.
With UDTs, you can define a custom type to group all these fields into a single, reusable structure.
The basic syntax to define a UDT looks like this:
Step 1: Get the Data
Products =[
{ProductID:”P001″,ProductName:”Laptop”,Price: 1000,StockQuantity:50},
{ProductID: “P002”, ProductName: “Smartphone”, Price: 500, StockQuantity: 100},
{ProductID: “P003”, ProductName: “Tablet”, Price: 300, StockQuantity: 200 }
];
Here, ProductType is the custom type, and it contains the fields:
ProductID: The unique identifier for each product (Text type).
ProductName: The name of the product (Text type).
Price: The price of the product (Number type).
StockQuantity: The number of items available in stock (Number type).
Step 2: Define the UDT Type
We can define types for a single product as a special named formula that uses the := assignment operator and Type function.
ProductType := Type( { ProductID: Text, ProductName: Text, Price: Number, StockQuantity: Number } );
Step 3: Using the User-Defined Types inside User-Defined Functions
Now that we have a ProductType which is a custom data type, to use this data type we will be creating a UFD inside which we will be using the product type data type.
Inside the UFD, it will take in a product and calculate its total value based on the stock quantity and price of the product.
Here is the User-defined function:
TotalValue( Product: ProductType ): Number = Product.Price * Product.StockQuantity;
This function which is TotalValue, takes in a Product (data type ProductType) and returns the total value of that product in stock.
Here is How you could define the data and the custom data type inside the power fx formulas as shown below:
Let’s say we have a table of products, and we want to calculate the total value of each product in stock. Here’s how we can do it:
TotalValue(First( Products ))
Here, we call the TotalValue function, passing the first product from the Products table, and then the function calculates and returns the total value (Price * StockQuantity).
We will be calculating the first product and the data inside that product is Price = 1000 and StockQuantity = 50.
As per the TotalValue function, It should return the value which is (Price * StockQuantity) i.e: 1000* 50 = 50000 as you can see in the screenshot below.
Conclusion
In this blog, we explored the capabilities of User-Defined Functions (UDFs) and User-Defined Types (UDTs) in PowerApps, showcasing how they enable more structured and reusable app logic.
By defining your own data types and leveraging UDFs, you can simplify complex processes, improve maintainability, and enhance the flexibility of your applications. The button-click example shows how you can easily change parameters to create flexible solutions for different needs.