We can now add User-Defined Functions to our Power Apps. Functions allow us to separate some of our business logic from the UI. It also allows us to reuse functions across the app.

We can now add User-Defined Functions to our Power Apps. Functions allow us to separate some of our business logic from the UI. It also allows us to reuse functions across the app. User-Defined Functions are now a part of Components. While Components will enable you to create reusable UI controls that are used across your apps, Functions allow you to create reusable logic used across your app.

Create a Component.

  1. Under Tree View, open the Components Tab.
  2. Click New Component.
  3. Rename the Component to something useful.
  4. Likely, this component will not have any UI. You can change the screen size to something small, say 40x40.

Enable User-Defined Functions in your Components.

Currently, the User-Defined Functions functionality of Components is experimental and needs to be enabled.

  1. From your app, go to File > Settings > Advanced Settings.
  2. Search for `component` and find Enhanced component Properties and enable it.

Create a Component Property.

Power Apps Component Property
  1. Within the Component, click New custom property.
  2. Display Name to something descriptive. E.g., “Calculate Mileage Reimbursement.”
  3. Set the Name to a short value with no spaces (or leave the default).
  4. The Description can be anything that describes your function.
  5. Set the Property Type to Output.
  6. Se the Data type. In this example, we are returning Currency.
  7. Next, create a new parameter.
  8. The Parameter Name must be short and have no spaces. E.g., “Mileage.”
  9. The Description can be anything that describes the parameter.
  10. The Data Type is the type of value passed. E.g., “Number.”
  11. Check the Required box if this parameter will be required for your function.

Power Apps Component Property Parameter

Define your Functions Formula.

Example formula for calculating mileage with a User-Defined function.

Now we need to take our input parameters and do something with them.

  1. Select your component.
  2. From the Property Dropdown, select the Function that you created.
  3. In the Formula Bar, you can add your formula. In this example, we’re simply multiplying the Mileage provided from the input parameter by .06 and rounding it to two decimal places.

Use your function in the app.

Example User-Defined Function in Power Apps

Now we can use our function across our app.

  1. Open the screen where you want to use the function.
  2. From the Insert menu > Custom > Select your Component to add it to the screen.
  3. Rename the component that you added.
  4. I added a textbox for entering the mileage. Then I added a label that calls my function.
  5. I set the Text property of that label to `comHomeUDF.CalculateMileageReimbursement(Value(txtMileage.Text)) `

Final Thoughts

I have been using User-Defined functions in a complicated app that I have been building for a client. It really helps to separate the logic out of the controls. Especially when you need to reuse that logic in multiple places.  This is another step in the right direction in enabling power users to build robust applications with low-code.