- Power Apps Community
- Get Help with Power Apps
- Building Power Apps
- Common Data Service for Apps
- AI Builder
- Power Apps Governance and Administering
- Power Apps component framework, ALM & Pro Dev
- Power Apps Portals
- Power Virtual Agents Forum
- Community Blog & News
- News & Announcements
- Galleries
- Community App Samples
- Video Gallery
- Canvas Apps Components Samples
- Microsoft Business Applications Presentations
- Ideas
- Power Apps Ideas

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Power Apps Community
- Forums
- Get Help with Power Apps
- Building Power Apps
- Custom PowerApp Function?

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

khaledhikmat

Level: Powered On

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-07-2016
02:43 AM

Is there a way to write custom functions that can be called from within the formulas?

Solved! Go to Solution.

1 ACCEPTED SOLUTION

Accepted Solutions

CarlosFigueira

Power Apps Staff

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-08-2016
08:22 PM

Yes, but it's not a simple process today. The main process to add a custom function is to create a custom API, where the function would be executed in the server, and in your app you'd add a connection to that API.

Depending on your scenario, there may be an alternative to that. For example, I was working on a PoC app that contained an analog clock. To display the pointers (or in my case, circles), I needed to calculate the sine and cosine of the angle depending on the current time. Those functions (Sin/Cos) are not available yet, I created a collection in the screen's OnVisible handler with the pre-calculated values for the functions:

ClearCollect(Angles, { Angle: 0, Sin: 0.000, Cos: 1.000 }, { Angle: 1, Sin: 0.017, Cos: 1.000 }, { Angle: 2, Sin: 0.035, Cos: 0.999 }, { Angle: 3, Sin: 0.052, Cos: 0.999 }, { Angle: 4, Sin: 0.070, Cos: 0.998 }, { Angle: 5, Sin: 0.087, Cos: 0.996 }, { Angle: 6, Sin: 0.105, Cos: 0.995 }, { Angle: 7, Sin: 0.122, Cos: 0.993 }, { Angle: 8, Sin: 0.139, Cos: 0.990 }, ...

And when you're ready to use the values, you can use the Last/FirstN combination to get the required value, like in the example below (which I used in a timer's OnTimerEnd property):

UpdateContext({ rx: Circle1.Width / 2, ry: Circle1.Height / 2 }); UpdateContext({ cx: Circle1.X + rx, cy: Circle1.Y + ry }); UpdateContext({ now: Now() }); UpdateContext({ h: Mod(Hour(now), 12), m: Minute(now), s: Second(now) }); UpdateContext({ sp: s * 6, mp: m * 6 + RoundDown(s / 12, 0), hp: h * 30 + RoundDown(m / 12, 0) }); UpdateContext({ sa: Last(FirstN(Angles, sp + 1)), ma: Last(FirstN(Angles, mp + 1)), ha: Last(FirstN(Angles, hp + 1)) }); UpdateContext({ sx: cx + sa.Sin * rx - 10, sy: cy - sa.Cos * ry - 10, mx: cx + ma.Sin * rx - 15, my: cy - ma.Cos * ry - 15, hx: cx + ha.Sin * rx - 15, hy: cy - ha.Cos * ry - 15 })

Again, this is a workaround for a specific scenario, which may not apply to your case. Please create a new issue in the PowerApps Ideas board (or vote up if it already exists), if you feel that this is a scenario that is important to you.

2 REPLIES 2

CarlosFigueira

Power Apps Staff

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-08-2016
08:22 PM

Yes, but it's not a simple process today. The main process to add a custom function is to create a custom API, where the function would be executed in the server, and in your app you'd add a connection to that API.

Depending on your scenario, there may be an alternative to that. For example, I was working on a PoC app that contained an analog clock. To display the pointers (or in my case, circles), I needed to calculate the sine and cosine of the angle depending on the current time. Those functions (Sin/Cos) are not available yet, I created a collection in the screen's OnVisible handler with the pre-calculated values for the functions:

ClearCollect(Angles, { Angle: 0, Sin: 0.000, Cos: 1.000 }, { Angle: 1, Sin: 0.017, Cos: 1.000 }, { Angle: 2, Sin: 0.035, Cos: 0.999 }, { Angle: 3, Sin: 0.052, Cos: 0.999 }, { Angle: 4, Sin: 0.070, Cos: 0.998 }, { Angle: 5, Sin: 0.087, Cos: 0.996 }, { Angle: 6, Sin: 0.105, Cos: 0.995 }, { Angle: 7, Sin: 0.122, Cos: 0.993 }, { Angle: 8, Sin: 0.139, Cos: 0.990 }, ...

And when you're ready to use the values, you can use the Last/FirstN combination to get the required value, like in the example below (which I used in a timer's OnTimerEnd property):

UpdateContext({ rx: Circle1.Width / 2, ry: Circle1.Height / 2 }); UpdateContext({ cx: Circle1.X + rx, cy: Circle1.Y + ry }); UpdateContext({ now: Now() }); UpdateContext({ h: Mod(Hour(now), 12), m: Minute(now), s: Second(now) }); UpdateContext({ sp: s * 6, mp: m * 6 + RoundDown(s / 12, 0), hp: h * 30 + RoundDown(m / 12, 0) }); UpdateContext({ sa: Last(FirstN(Angles, sp + 1)), ma: Last(FirstN(Angles, mp + 1)), ha: Last(FirstN(Angles, hp + 1)) }); UpdateContext({ sx: cx + sa.Sin * rx - 10, sy: cy - sa.Cos * ry - 10, mx: cx + ma.Sin * rx - 15, my: cy - ma.Cos * ry - 15, hx: cx + ha.Sin * rx - 15, hy: cy - ha.Cos * ry - 15 })

Again, this is a workaround for a specific scenario, which may not apply to your case. Please create a new issue in the PowerApps Ideas board (or vote up if it already exists), if you feel that this is a scenario that is important to you.

khaledhikmat

Level: Powered On

Re: Custom PowerApp Function?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-09-2016
02:21 AM

Very helpful. Thanks. I appreciate the clear answer.

Announcements

Top Solution Authors

User | Count |
---|---|

118 | |

62 | |

55 | |

44 | |

38 |

Top Kudoed Authors (Last 30 Days)

User | Count |
---|---|

240 | |

106 | |

95 | |

84 | |

83 |