Search code examples
powerappspowerapps-canvaspowerapps-formula

Reusable Function in PowerApps


We have 'chunks' of identical formulas in different parts of our app. In light of PA lacking actual named functions, are there any workarounds that would allow us to reuse a function/formula to prevent duplication of 'code' (i.e. formula)?


Solution

  • One option would be to create a hidden button or other selectable item and then assign the formula to this button. Whenever you need to rerun the formula, instead of recreating the formula, use Select(Button1).

    For instance, if you need to have a formula that adds to a collection:

    1. Add a button called Button1.
      1. Set the Visible property to False
      2. Set the OnSelect property to something like Collect(YourCollection,"A Value")
    2. Anywhere that needs to call this function set the OnSelect property to Select(Button1)

    If you need to set a dynamic value to this collection, you can set a context variable first

    1. Change the OnSelect property for Button1 to Collect(YourCollection,yourVariable)
    2. Set the OnSelect property of you control to UpdateContext({yourVariable,"A Value"});Select(Button1)

    Of course this is a very simple example and can be expanded upon.

    Of course you can also go the Power Automate route, calling a Flow from within your PowerApp control to do your work, but that may require additional licensing if you do complex work within the Flow and return values then have to be parsed.

    Here's hoping Microsoft eventually allows us to create custom functions that can be called within PowerApps.