{"id":34048,"date":"2023-02-20T16:24:32","date_gmt":"2023-02-20T10:54:32","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=34048"},"modified":"2025-01-16T11:40:12","modified_gmt":"2025-01-16T06:10:12","slug":"with-function-in-power-appspower-fx","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2023\/02\/with-function-in-power-appspower-fx\/","title":{"rendered":"&#8216;With&#8217; Function in Power Apps(Power Fx)"},"content":{"rendered":"<p>The Power Fx\/Canvas Apps include a variety of functions for carrying out operations. In this blog, we\u2019ll look at the \u201cwith\u201d function in the Power Fx command\/<a href=\"https:\/\/www.inogic.com\/services\/microsoft-power-platform\/microsoft-power-apps-canvas-app\/?utm_source=inogic-t-blog&amp;utm_medium=canvasapp&amp;utm_campaign=itblog&amp;utm_id=canvasapp\" target=\"_blank\" rel=\"noopener\">Canvas App<\/a>. Using\u00a0the <strong>With<\/strong>\u00a0function, you may split up lengthy formulas into smaller ones, or \u201csub-formulas,\u201d making them simpler to understand. Similarly, it is decreasing the number of data requests and improving app performance.<\/p>\n<p>Let\u2019s see the\u00a0<strong>With<\/strong>\u00a0function along with the patch function.<\/p>\n<p>The argument you provided retains the value returned as a record, and you can refer to it once again inside of that formula.<\/p>\n<p><strong>Scenario<\/strong>:<\/p>\n<p>Let\u2019s consider a scenario with a custom entity \u2018<strong>Registration\u2019<\/strong>. The entity has many records. Also, the entity has one lookup field with the name\u00a0<strong>Manager<\/strong>. Here, I am required to set the logged-in user as the manager of the records whose city is Mumbai. So that logged-in users can handle \/verify the records or perform the actions as per their needs.<\/p>\n<p>To achieve the above, I have to store all the records that fit the above condition in a Global variable using\u00a0<strong>SET\u00a0<\/strong>or in a collection using\u00a0<strong>ClearCollect<\/strong>. Further, we will use that variable\/collection in the\u00a0<strong>Patch<\/strong>\u00a0function and update all the records.<\/p>\n<p>Below is the screenshot where after clicking the assign button we set the current user as a manager to the records whose city is \u201cMumbai\u201d and disable it.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-34050\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/1Power-FX.jpeg\" alt=\"Power FX\" width=\"1450\" height=\"800\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/1Power-FX.jpeg 1450w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/1Power-FX-300x166.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/1Power-FX-1024x565.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/1Power-FX-768x424.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/1Power-FX-660x364.jpeg 660w\" sizes=\"(max-width: 1450px) 100vw, 1450px\" \/><\/p>\n<p>For achieving the above, we have used the below query on the <strong>OnSelect<\/strong> of the assign button. Below is the screenshot of the same.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-34051\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/2Power-FX.jpeg\" alt=\"Power FX\" width=\"1438\" height=\"710\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/2Power-FX.jpeg 1438w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/2Power-FX-300x148.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/2Power-FX-1024x506.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/2Power-FX-768x379.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/2Power-FX-660x326.jpeg 660w\" sizes=\"(max-width: 1438px) 100vw, 1438px\" \/><\/p>\n<p>The above can also be achieved using <strong>ClearCollect<\/strong> as seen below \u2013<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-34052\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/3Power-FX.jpeg\" alt=\"Power FX\" width=\"1449\" height=\"766\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/3Power-FX.jpeg 1449w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/3Power-FX-300x159.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/3Power-FX-1024x541.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/3Power-FX-768x406.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/3Power-FX-660x349.jpeg 660w\" sizes=\"(max-width: 1449px) 100vw, 1449px\" \/><\/p>\n<p>Query:<\/p>\n<pre class=\"lang:css gutter:true start:1\">Set(\r\n\r\nregistrationfromMumbai,\r\n\r\nFilter(\r\n\r\nregistration,\r\n\r\nCity = \"Mumbai\"\r\n\r\n)\r\n\r\n);\r\n\r\nForAll(\r\n\r\nregistrationfromMumbai,\r\n\r\nPatch(\r\n\r\nregistration,\r\n\r\nThisRecord,\r\n\r\n{\r\n\r\nManager: LookUp(\r\n\r\nUsers,\r\n\r\n'Primary Email' = User().Email\r\n\r\n)\r\n\r\n}\r\n\r\n)\r\n\r\n);<\/pre>\n<p>Now, the above approach certainly looks very straightforward to achieve, but it has a major drawback &#8211; storing all the records in a global variable on the loading of the screen can cause an adverse effect on the performance of the canvas app.<\/p>\n<p>Instead, we can use <strong>with<\/strong> function to achieve the same as shown below \u2013<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-34053\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/4Power-FX.jpeg\" alt=\"Power FX\" width=\"857\" height=\"545\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/4Power-FX.jpeg 857w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/4Power-FX-300x191.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/4Power-FX-768x488.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/4Power-FX-660x420.jpeg 660w\" sizes=\"(max-width: 857px) 100vw, 857px\" \/><\/p>\n<p>The query is shown in table below:<\/p>\n<pre class=\"lang:css gutter:true start:1\">With(\r\n\r\n{\r\n\r\nmumbaiRecords: Filter(\r\n\r\nregistration,\r\n\r\nCity = \"Mumbai\"\r\n\r\n)\r\n\r\n},\r\n\r\nForAll(\r\n\r\nmumbaiRecords,\r\n\r\nPatch(\r\n\r\nregistration,\r\n\r\nThisRecord,\r\n\r\n{\r\n\r\nManager: LookUp(\r\n\r\nUsers,\r\n\r\n'Primary Email' = User().Email\r\n\r\n)\r\n\r\n}\r\n\r\n)\r\n\r\n)<\/pre>\n<p>Also, we can use \u2018<strong>With\u2019 <\/strong>in the Power Fx buttons,<\/p>\n<p>Below is the query which sets the Action of the button<\/p>\n<pre class=\"lang:css gutter:true start:1\"><strong>With<\/strong>(\r\n\r\n{\r\n\r\nRegistrationRecords: Filter( Registration, City = City (Registration)'.Mumbai ) },\r\n\r\n\r\nForAll( RegistrationRecords,\r\n\r\nPatch( Registration, ThisRecord, { Manager : LookUp('User\u2019, 'Primary Email \u201cLeoTech@inotech12.onmicrosoft.com\u201d) })\r\n\r\n)\r\n\r\n);<\/pre>\n<p>I have set the above query on the command button hence the <strong>With <\/strong>function holds all selected records. Here we set the variable as \u2018RegistrationRecords\u2019 in that we get the collection of all records whose city is Mumbai and use the same in the further query instead of setting different variables separately so we can easily update all records as set to manager value with the help of the ForAll Function.<\/p>\n<p>For further understanding, now, will check nested <strong>With,<\/strong> with the other scenario.<\/p>\n<p><strong>Scenario: <\/strong>While working in the sales process in the organization, consider the following situation. An annual subscription-based system is used in order to determine the product&#8217;s pricing based on the user and the year. The example we take into account in our scenario is the one below.<\/p>\n<p>If there are less than 50 users, the pricing is set at 50 USD. If there are 51 to 100 users, the price is set at 100 USD. If there are 100 to 300 users then the price is set to 150. If there are 301 to 500 users, the price is set at 200 USD. If there are more than 500 users, the price is set at 300 USD.<\/p>\n<p>Below is the query for getting the required result:<\/p>\n<pre class=\"lang:css gutter:true start:1\"><strong>With<\/strong>(\r\n\r\n{\r\n\r\nUser:UserSlider.Value,Price:PriceSlider.Value,SuscriptionYear:YearSlider.Value},\r\n\r\n<strong>With<\/strong>({\r\n\r\nActualPricing:If(\r\n\r\nUserSlider.Value &lt;= 50, 50,\r\n\r\nUserSlider.Value &gt; 50 &amp;&amp; UserSlider.Value &lt;= 100 , 100,\r\n\r\nUserSlider.Value &gt;100 &amp;&amp; UserSlider.Value&lt;= 300,150,\r\n\r\nUserSlider.Value &gt;300 &amp;&amp; UserSlider.Value&lt;=500, 200,\r\n\r\nUserSlider.Value &gt; 500, 300\r\n\r\n)\r\n\r\n},\r\n\r\nActualPricing*SuscriptionYear)\r\n\r\n);<\/pre>\n<p>Below is the screenshot where we get the grand total of the subscription.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-34054\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/5Power-FX.jpeg\" alt=\"Power FX\" width=\"1344\" height=\"725\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/5Power-FX.jpeg 1344w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/5Power-FX-300x162.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/5Power-FX-1024x552.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/5Power-FX-768x414.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/5Power-FX-660x356.jpeg 660w\" sizes=\"(max-width: 1344px) 100vw, 1344px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p><strong>With<\/strong> function helps you make your code more readable and efficient, thus, increasing app performance.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/services\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone size-full wp-image-34038\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/Microsoft-Power-Platform-3-1.png\" alt=\"Microsoft Power Platform (3)\" width=\"800\" height=\"200\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/Microsoft-Power-Platform-3-1.png 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/Microsoft-Power-Platform-3-1-300x75.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/Microsoft-Power-Platform-3-1-768x192.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/02\/Microsoft-Power-Platform-3-1-660x165.png 660w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Power Fx\/Canvas Apps include a variety of functions for carrying out operations. In this blog, we\u2019ll look at the \u201cwith\u201d function in the Power Fx command\/Canvas App. Using\u00a0the With\u00a0function, you may split up lengthy formulas into smaller ones, or \u201csub-formulas,\u201d making them simpler to understand. Similarly, it is decreasing the number of data requests\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2023\/02\/with-function-in-power-appspower-fx\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1954,18,38,44],"tags":[1337,2672],"class_list":["post-34048","post","type-post","status-publish","format-standard","hentry","category-canvas-apps","category-dynamics-365-v9-2","category-microsoft-powerapps","category-power-apps","tag-power-apps","tag-power-fx"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/34048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=34048"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/34048\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=34048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=34048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=34048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}