{"id":21707,"date":"2022-09-16T11:07:21","date_gmt":"2022-09-16T18:07:21","guid":{"rendered":"https:\/\/www.microsoft.com\/en-us\/power-platform\/blog\/power-apps\/power-fx-introducing-named-formulas\/"},"modified":"2022-09-16T11:07:21","modified_gmt":"2022-09-16T18:07:21","slug":"power-fx-introducing-named-formulas","status":"publish","type":"power-apps","link":"https:\/\/www.microsoft.com\/en-us\/power-platform\/blog\/power-apps\/power-fx-introducing-named-formulas\/","title":{"rendered":"Power Fx: Introducing Named Formulas"},"content":{"rendered":"\n
I am thrilled to introduce you to an old concept. A powerful concept that has been in Excel for a very long time, that we are now bringing to Power Fx. With it, you can simplify your app’s initialization, reduce app load time, reuse logic, and improve the maintainability of your apps.<\/p>\n\n\n
Welcome Named Formulas. A funny name, that derives from how this feature appears in Excel, in the “Name Manager.” In Excel, you can name any cell and refer to that name throughout the workbook. It adds a level of indirection that allows you to move that cell reference by name rather than by location. And inspiring the introduction into Power Fx, it also allows you to bind a name to a formula that isn’t in a cell. <\/p>\n\n\n
Today in Power Fx, you write formulas for the properties of the controls in your app. You can refer to those properties from other formulas to reuse the result of calculation. But you are limited by the controls and their properties. What if you could effectively create your own properties, create your own points of reuse? <\/p>\n\n\n
Enough theoretical preamble, what if you could write this in an App.Formulas<\/strong> property:<\/p>\n\n\n These are formulas in the truest sense of the word. They express how to calculate the UserEmail<\/strong>, UserInfo<\/strong>, UserTitle<\/strong>, and UserPhone<\/strong> from other values, much like F = m * a<\/em> in Physics calculates force. This logic is now encapsulated and can be used throughout the app and can be updated in this one location. It can be changed from using the Dataverse Users table to using the Office 365 connector without needing to change formulas in the rest of the app.<\/p>\n\n\n These formulas don’t say anything about when these should be calculated or how these should be calculated. They are truly declarative. They provide a recipe only.<\/p>\n\n\n Ok, you are probably wondering, why use this? Why not just use Set<\/strong> in App.OnStart<\/strong> to accomplish the same thing? You certainly can and we have no intention to ever take that ability away. State variables and Set<\/strong> will always have their place. <\/p>\n\n\n Named formulas have advantages:<\/p>\n\n\nUserEmail = User().Email;\nUserInfo = LookUp( Users, 'Primary Email' = User().Email );\nUserTitle = UserInfo.Title;\nUserPhone = Switch( UserInfo.'Preferred Phone',\n 'Preferred Phone (Users)'.'Mobile Phone', UserInfo.'Mobile Phone',\n UserInfo.'Main Phone' );\n<\/pre>\n\n\n
Implications for OnStart<\/h2>\n\n\n