<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jack Pashayan on Medium]]></title>
        <description><![CDATA[Stories by Jack Pashayan on Medium]]></description>
        <link>https://medium.com/@pashayanjack?source=rss-1f8ec6f08a3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mt8uZw8wigxT2azM.jpg</url>
            <title>Stories by Jack Pashayan on Medium</title>
            <link>https://medium.com/@pashayanjack?source=rss-1f8ec6f08a3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 08:49:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pashayanjack/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Copy and Paste data from an Excel sheet to SQL Server via Power Apps, without Hard-Coding…]]></title>
            <link>https://medium.com/@pashayanjack/how-to-copy-and-paste-data-from-an-excel-sheet-to-sql-server-via-power-apps-without-hard-coding-94845005eef9?source=rss-1f8ec6f08a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/94845005eef9</guid>
            <category><![CDATA[json]]></category>
            <category><![CDATA[excel]]></category>
            <category><![CDATA[sql-server]]></category>
            <category><![CDATA[power-apps-developer]]></category>
            <category><![CDATA[transactions]]></category>
            <dc:creator><![CDATA[Jack Pashayan]]></dc:creator>
            <pubDate>Thu, 03 Apr 2025 02:42:58 GMT</pubDate>
            <atom:updated>2025-04-03T02:56:59.083Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Copy and Paste data from <em>an </em>Excel sheet to SQL Server via Power Apps, without Hard-Coding Columns</h3><p>If you like to edit your data in Excel, and want a simple and intuitive way to write it into a SQL Server database<em>, </em>this article is for you.</p><p>This trick saved me from suffering through SQL Server Management Studio (SSMS)’s cursed “Import Flat File” button, which never guesses the data types correctly, or writing a custom SQLAlchemy script. It builds upon my previous article: <a href="https://medium.com/@pashayanjack/re-writing-power-apps-patch-as-a-stored-procedure-2df82198ec0d">Re-Writing Power Apps Patch() as a Stored Procedure</a>, where JSON is used to make concurrent Patch() transactions safer and more reliable. In this article, we take it one step further, and import our data from Excel instead of a Power Apps collection, via a simple Copy + Paste.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3LyhgIfmlrZ4A8Q57M6EHg.jpeg" /><figcaption>Come to dark side, JSON in Power Apps:)</figcaption></figure><p>You may be familiar with pasting <a href="https://www.youtube.com/watch?v=foGaOrBzVLc">a row of data from an Excel table and uploading to a data source</a>, or <a href="https://www.practicalpowerapps.com/data/excelpatch/">even copying and pasting multiple rows</a>. These two techniques do so by leveraging the fact that cells and rows copied from Excel come neatly separated by Tabs and Carriage Returns, respectively. Here is an example of some Power Fx code doing so:</p><pre>/*<br>Excel copies its data as a Tab &amp; Carriage Return Deliniated string<br><br>Unicode Reference:<br>Char(9) = Tab(&quot; \t &quot;)<br>Char(10) = Carriage Return(&quot; \r &quot;)<br>Char(34) = Double Quote(&quot; &quot; &quot;)<br>*/<br><br>With(<br>  {<br>     headers_txt: First(Split(TextInput_Excel_input.Text, Char(10))).Value<br>  },<br>With(<br>    {<br>      headers_count: CountRows(Split(headers_txt, Char(9))),<br>      body_txt: <br>        Substitute(<br>          Mid(TextInput_Excel_input.Text, Len(headers_txt) + 2), <br>          Char(10), <br>          Char(9)<br>        )<br>    },<br><br>Clear(skinny);<br>ForAll(<br>  Split( body_txt, Char(9) ).Value,<br>  Collect(<br>    skinny,<br>    {<br>        val:      Value,<br>        index:    Max(skinny, index) + 1<br>    }<br>  )<br>);<br><br>Clear(wide);<br>ForAll(<br>  Sequence(Round(Max(skinny, index) / headers_count, 0), 0) As a,<br>  Collect(<br>    wide,<br>    {<br>        company_id:   LookUp(skinny, index=(headers_count*a.Value) + 0+2).val,<br>        customer_id:  LookUp(skinny, index=(headers_count*a.Value) + 1+2).val,<br>        location:     LookUp(skinny, index=(headers_count*a.Value) + 2+2).val,<br>        filter_1:     LookUp(skinny, index=(headers_count*a.Value) + 3+2).val,<br>        filter_2:     LookUp(skinny, index=(headers_count*a.Value) + 4+2).val,<br>        notes:        LookUp(skinny, index=(headers_count*a.Value) + 5+2).val,<br>        created_by:   LookUp(skinny, index=(headers_count*a.Value) + 6+2).val,<br>    }<br>  )<br>);<br>);<br>);</pre><p>However when you want to extend these workflows to another SQL Server table, for example, the Power App developer would have to go back into the app to add new code for that table’s columns! This is mostly because Power Apps <strong>{Tables} </strong>do not allow<em> dynamic key assignment</em>, so you can only type in the exact name for the columns of your collection.</p><p>When we write our data as JSON instead, we do not have this restriction. Here is code that can handle data of any number and name of columns:</p><pre>/*<br>Excel copies its data as Carriage Return-Separated rows of Tab-Deliniated strings<br><br>Unicode Reference:<br>Char(9) = Tab(&quot; \t &quot;)<br>Char(10) = Carriage Return(&quot; \r &quot;)<br>Char(34) = Double Quote(&quot; &quot; &quot;)<br>*/<br><br>With(<br>  {<br>    headers_txt: First(Split(TextInput_txt_input.Text, Char(10))).Value<br>  },<br>  With(<br>    {<br>      headers_count: CountRows(Split(headers_txt, Char(9))),<br>      body_txt: <br>        Substitute(<br>          Mid(TextInput_Excel_input.Text, Len(headers_txt) + 2), <br>          Char(10), <br>          Char(9)<br>        )<br>    },<br>    Clear(headers);<br>    ForAll(<br>      Split(headers_txt, Char(9)).Value,<br>      Collect(<br>        headers,<br>        {<br>          col: Value,<br>          index: Max(headers, index) + 1<br>        }<br>      )<br>    );<br><br>    Clear(skinny);<br>    ForAll(<br>      Split( body_txt, Char(9) ).Value,<br>      Collect(<br>        skinny,<br>        {<br>          val:     Value,<br>          index:   Max(skinny, index) + 1<br>        }<br>      )<br>    );<br>    <br>    UpdateContext({<br>      json_derulo:<br>        &quot;[<br>  {<br>    &quot;&amp;<br>        Concat(<br>          Sequence(Round(Max(skinny, index) / headers_count, 0), 0) As a,<br>          Concat(<br>            headers As x,<br>            Char(34)&amp;<br>              x.col // Each column&#39;s header<br>            &amp;Char(34)&amp;&quot;: &quot;&amp;<br>              // Each cell, with any rules applied<br>              With(<br>                { <br>                    next_val: <br>                        LookUp(<br>                            skinny, <br>                            index=(headers_count*a.Value) + x.index<br>                        ).val<br>                },<br>                If(<br>                  next_val in [ &quot;&quot;, &quot;NULL&quot;],  &quot;null&quot;,<br>                  Char(34)&amp;next_val&amp;Char(34)<br>                )<br>              )<br>            ,<br>            &quot;,<br>    &quot;<br>          ),<br>          &quot;<br>  }, <br>  {<br>    &quot;<br>        )<br>        &amp;&quot;<br>  }<br>]&quot;<br>    });<br>...</pre><p>And here is an example of the code in action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*_xkNEuSlwJs_4_ivxkAvOQ.png" /><figcaption>A simple INSERT of 3 rows to [REF.Error_Kinds]</figcaption></figure><p>By itself JSON is not very useful. ParseJSON(), Power App’s native tool to turn a JSON string into a Collection, requires the column names to once again be hard-coded into the app. But that’s where my other article, <a href="https://medium.com/@pashayanjack/re-writing-power-apps-patch-as-a-stored-procedure-2df82198ec0d">Patch() as a Stored Procedure,</a> comes in handy, allowing you to INSERT or UPDATE this data directly into your database from JSON alone. Here is a review of that:</p><pre>// Preview of the Patch Stored Procedure, via its Power Fx API:<br><br>SP_Patch_Concurrent.dboSPTransactPatchConcurrent({<br>  json_bodies:        Concat(<br>    [ power_apps_collection_01,  power_apps_collection_02 ], <br>    JSON(Value, JSONFormat.IndentFour), &quot;|&quot;<br>  ),<br>  dest_table_names:   Concat(<br>    [ &quot;SQL_Server_Table_Name_1&quot;, &quot;SQL_Server_Table_Name_2&quot; ], <br>    Value,                              &quot;|&quot;<br>  )<br>});</pre><p>This flexibility comes at a cost however, as a mismatch in the Excel table’s headers could lead to a failed transaction, and we know that SQL Server error codes are notoriously hard to parse. For a truly user friendly interface for this <em>Excel to SQL Server uploader, </em>we need a way to validate our headers against our database schema. This time we turn to SQL code, where a helpful T-SQL query can reveal to us full set of columns for all of our tables:</p><pre>SET ANSI_NULLS ON<br>GO<br><br>SET QUOTED_IDENTIFIER ON<br>GO<br><br><br>CREATE VIEW [dbo].[VW.DB_Table_Schemas] AS<br><br>SELECT<br> x.table_name<br> ,(<br>  SELECT STRING_AGG(y.name, &#39;, &#39;) WITHIN GROUP (ORDER BY y.name) AS column_names<br>  FROM sys.columns AS y<br>  WHERE x.OBJECT_ID = y.OBJECT_ID<br>  AND ( y.name &lt;&gt; x.identity_col OR x.identity_col IS NULL )<br> ) AS column_names<br> ,x.identity_col<br>FROM (<br> SELECT<br>  a.name AS table_name<br>  ,a.OBJECT_ID<br>  ,(<br>   SELECT b.name<br>   FROM sys.columns AS b<br>   WHERE a.OBJECT_ID = b.OBJECT_ID <br>   AND b.is_identity = 1<br>  ) AS identity_col<br> FROM sys.tables AS a<br>) AS x<br><br>GO<br><br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ao5PPciQvuluP4KL0EbHtw.png" /><figcaption>My team sets most SQL Server Tables’ (Is Identity) property to “Yes”, so that they are Update-able via Patch() and the Stored Procedure.</figcaption></figure><p>And here we share the remainder of the Power Fx code, which populates the list <em>invalid_headers</em> with any columns in the header of the Excel data that do not exist in the columns of table being written to, unless they are the table’s <em>primary key</em>:</p><pre><br>...       <br>    ClearCollect(<br>      this_table_schema,<br>        Filter(<br>          &#39;VW.DB_Table_Schemas&#39; As a,<br>          a.table_name = TextInput_excel_destination_db_folder.Text<br>        )<br>    );<br><br>    ClearCollect(<br>      this_table_columns,<br>      Split(<br>        First(this_table_schema).column_names,<br>        &quot;, &quot;<br>      ).Value<br>    );<br><br>    UpdateContext({ this_table_pk: First(this_table_schema).identity_col });<br>    <br>    ClearCollect(<br>      invalid_headers,<br>      Filter(<br>        AddColumns(<br>          headers.col As a,<br>          valid,  a.col in this_table_columns.Value || a.col = this_table_pk<br>        ),<br>        valid = false<br>      )<br>    )<br>  )<br><br>)</pre><p>Messing up one of the columns (ie. created_datetime -&gt; creatd_datetime) after pasting now looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vB4GvmAecTPL_tcEJIwqpw.png" /><figcaption>UPDATing 3 rows of [PA.Error_Logs]</figcaption></figure><p><em>Notice how the preview title now says “Data to </em><strong><em>Update</em></strong><em>”?</em> Thanks to [VW.DB_Table_Schemas], this Power App knows which column the table can be updated on, and if that column is included in the Excel data, then the Stored Procedure will UPDATE the table instead of INSERTing to it.</p><p>Here is the full Power Fx code to render Excel Data as JSON w/ validation:</p><pre><br>/*<br>Excel copies its data as Carriage Return-Separated rows of Tab-Deliniated strings<br><br>Unicode Reference:<br>Char(9) = Tab(&quot; \t &quot;)<br>Char(10) = Carriage Return(&quot; \r &quot;)<br>Char(34) = Double Quote(&quot; &quot; &quot;)<br>*/<br><br>With(<br>  {<br>    headers_txt: First(Split(TextInput_Excel_input.Text, Char(10))).Value<br>  },<br>  With(<br>    {<br>      headers_count: CountRows(Split(headers_txt, Char(9))),<br>      body_txt: <br>        Substitute(<br>          Mid(TextInput_Excel_input.Text, Len(headers_txt) + 2), <br>          Char(10), <br>          Char(9)<br>        )<br>    },<br>    Clear(headers);<br>    ForAll(<br>      Split(headers_txt, Char(9)).Value,<br>      Collect(<br>        headers,<br>        {<br>          col: Value,<br>          index: Max(headers, index) + 1<br>        }<br>      )<br>    );<br><br>    Clear(skinny);<br>    ForAll(<br>      Split( body_txt, Char(9) ).Value,<br>      Collect(<br>        skinny,<br>        {<br>          val:     Value,<br>          index:   Max(skinny, index) + 1<br>        }<br>      )<br>    );<br>    <br>    UpdateContext({<br>      json_derulo:<br>        &quot;[<br>  {<br>    &quot;&amp;<br>        Concat(<br>          Sequence(Round(Max(skinny, index) / headers_count, 0), 0) As a,<br>          Concat(<br>            headers As x,<br>            Char(34)&amp;<br>              x.col // Each column&#39;s header<br>            &amp;Char(34)&amp;&quot;: &quot;&amp;<br>              // Each cell, with any rules applied<br>              With(<br>                { <br>                    next_val: <br>                        LookUp(<br>                            skinny, <br>                            index=(headers_count*a.Value) + x.index<br>                        ).val<br>                },<br>                If(<br>                  next_val in [ &quot;&quot;, &quot;NULL&quot;],  &quot;null&quot;,<br>                  Char(34)&amp;next_val&amp;Char(34)<br>                )<br>              )<br>            ,<br>            &quot;,<br>    &quot;<br>          ),<br>          &quot;<br>  }, <br>  {<br>    &quot;<br>        )<br>        &amp;&quot;<br>  }<br>]&quot;<br>    });<br><br>    ClearCollect(<br>      this_table_schema,<br>        Filter(<br>          &#39;VW.DB_Table_Schemas&#39; As a,<br>          a.table_name = TextInput_excel_destination_db_folder.Text<br>        )<br>    );<br><br>    ClearCollect(<br>      this_table_columns,<br>      Split(<br>        First(this_table_schema).column_names,<br>        &quot;, &quot;<br>      ).Value<br>    );<br><br>    UpdateContext({ this_table_pk: First(this_table_schema).identity_col });<br>    <br>    ClearCollect(<br>      invalid_headers,<br>      Filter(<br>        AddColumns(<br>          headers.col As a,<br>          valid,  a.col in this_table_columns.Value || a.col = this_table_pk<br>        ),<br>        valid = false<br>      )<br>    )<br>  )<br><br>)</pre><h3><strong>Final Thoughts</strong></h3><p>Hopefully you find this tool a helpful extension of the Power Apps and Microsoft Office ecosystem. As much as I love Power Automate, some text parsing all within the Power App was all that was needed to give us a lot of flexibility with how we add/update data in SQL Server.</p><p>If you have any questions or stories of how you applied this in your Power App, please do share a comment with me and my readers:)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94845005eef9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Re-Writing Power Apps Patch() as a Stored Procedure]]></title>
            <link>https://medium.com/@pashayanjack/re-writing-power-apps-patch-as-a-stored-procedure-2df82198ec0d?source=rss-1f8ec6f08a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/2df82198ec0d</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[bugs]]></category>
            <category><![CDATA[sql-server]]></category>
            <category><![CDATA[power-apps-developer]]></category>
            <dc:creator><![CDATA[Jack Pashayan]]></dc:creator>
            <pubDate>Tue, 26 Nov 2024 03:22:06 GMT</pubDate>
            <atom:updated>2024-11-26T03:22:06.742Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YQ3KQTV3Sh2GwiVMqwT6Vw.jpeg" /></figure><p><strong>What’s wrong with the Patch() function?</strong></p><p>My organization uses the Power Apps platform extensively to integrate data systems across all aspects of the company. Gone are the days of disjointed Excel spreadsheets or waiting months to approve a new SaaS vendor. Instead we can build our data systems in lock step with the needs of our company and our industry. Across a dozen or so Power Apps we have several thousand transactions a day, each with a few writes to a SQL Server database at a time. These apps are a breeze to develop, and very easy for operators to use, with proper guardrails designed into the app of course.</p><p>However, over the years we have noticed a distinct problem emerge: a blight of “dropped transactions” that come out of nowhere and leave us tracking down the truth in person. We’ll corroborate with operators, check for data inconsistencies with Python “watchdog scripts”, but even when the errors occur months apart there is still the gnawing fear that it could happen again.</p><p>Now, I know my limitations as a developer. There have been plenty of times where the data errors are easily explained by a missing an “on” condition in a SQL JOIN, or two operators hitting the same button at the same time on different computers. I manage to identify and address these issues as they come, but sometimes I <em>specifically </em>see that a few Patch() statements from a single user just don’t make their way to the database. And these are mature functions that works perfectly 99% of the time! What gives??</p><p>Failing to find another user online with this issue, I doubt this is a widespread issue. I’m sure we’re just that unlucky group whose combination of Azure cloud storage, high security tenant, and local ISP don’t play nice together. And that led me to the 5 letters that bring them all together: P-A-T-C-H.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VGep74wuhWuUku_txEqpbA.jpeg" /></figure><p><strong>How can Patch() be improved?</strong></p><p>T-SQL, as we know, is amazing at doing a lot of work with very little code or even coding knowledge. It is so great when you write a simple and fast script and you still know pretty much what is going on under the covers. However, the Power Apps -&gt; SQL Server connector has little documentation on how it is implemented, which makes it hard to optimize. And with the March update for native Stored Procedure calls in Power Apps, that inspired me to turn these adolescent Patch() statements into a battle-tested SQL Stored Procedure.</p><p>The code:</p><p><a href="https://github.com/jackberrypassionfruit/power_apps_patch_as_sproc">GitHub - jackberrypassionfruit/power_apps_patch_as_sproc: A SQL Server Stored Procedure alternative to the Patch() function</a></p><p>After transitioning a portion of our Power Apps to use this Stored Procedure (SProc) approach, we have seen a slight improvement in execution time, no recurrence of our “dropped transaction” issue (although time will tell), and even realized an extra benefit of executing all Patch() statements as one SQL transaction: Termination and Rollback. If the 3rd of 5 Patch()’s fails, then you have a mess to clean up. However with this SProc, everything is cleaned up for you, giving a change to indicate a runtime error instead. This helps immensely with some of the transient network issues that Power Apps doesn’t seem to handle too well.</p><p><strong>Sounds great! I will go ahead and abandon Patch() forever now.</strong></p><p>Not so fast, Patch() still has its place. And even though this article likely makes me seem like I curse Power Apps’ existence daily, I still think the platform’s emphasis on easy usability is hugely important to its/our success.</p><p>Patch() is still great when:</p><ol><li>You need something with simple syntax you can write rapidly</li><li>You find syntax highlighting and error checking very helpful</li><li>You need to use some of Patch() more flexible features like doing a LookUp() inside the Patch()</li></ol><p>The SQL Stored Procedure is a great alternative when:</p><ol><li>You are running into reliability issues at scale</li><li>You want to take advantage of transaction rollback</li></ol><p>I hope you found this article helpful, if you have any ideas of how to improve my Power Apps -&gt; SQL Connector 2.0 (like its somewhat complicated API), I welcome you to submit an issue or PR!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2df82198ec0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>