7 Ways To Use The PATCH Function In Power Apps (Cheat Sheet)
Power Apps PATCH function is the hardest function to understand. Why? The Patch function is the only function you can use 7 different ways. Yeah, you heard me right. Seven. That’s why I built a no-nonsense cheat sheet that you can use to quickly figure out how the patch function works.
Note: in all of the examples below the datasource called Employees where records are being created/updated is SharePoint list.
Table Of Contents:
PATCH A Single Record To A Table
• Create A New Record
• Update An Existing Record
• Get The Result Of The Patch Function
PATCH Multiple Records To A Table
• Create Multiple New Records
• Edit Multiple Existing Records
• Upsert Multiple Records
PATCH Changes To A Record Variable
• Change Values In A Record Variable
Bonus Content:
• More Power Apps Patch Function Tips And Tricks
1. Create A New Record With Power Apps Patch Function
Syntax
Patch(Datasource, BaseRecord, NewRecord)
Input
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
Code
Patch(
Employees,
Defaults(Employees),
{
FullName: "Sarah Green",
EmployeeNumber: 1002,
HireDate: Date(2018,3,14),
Active: true
}
)
Output
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Green | 1002 | 03/14/2018 | true |
2. Update An Existing Record Using Power Apps Patch Function
Syntax
Patch(Datasource, BaseRecord, ChangeRecord)
Input
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Green | 1002 | 03/14/2018 | true |
Code
Patch(
Employees,
LookUp(
Employees,
ID=4
),
{
FullName: "Sarah Brown",
EmployeeNumber: 1003
}
)
Output
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Brown | 1003 | 03/14/2018 | true |
3. Get The Result Of The Patch Function
Syntax
Set(VariableName, Patch(Datasource, BaseRecord, ChangeRecord))
Input
Employees Table In SharePoint
ID | FullName | EmployeeNumber | Created | Created By | … |
1 | Matthew Devaney | 1050 | 05/08/2022 | Matthew Devaney | … |
2 | Alice Lemon | 0958 | 05/10/2022 | Matthew Devaney | … |
3 | David Johnson | 0563 | 05/13/2022 | Matthew Devaney | … |
4 | Sarah Green | 1002 | 05/13/2022 | Matthew Devaney | … |
Code
Set(
varEmployeeCurrent,
Patch(
Employees,
Default(Employees),
{
FullName: "Kelly Smith",
EmployeeNumber: 1066
}
)
)
Output
Employees Table In SharePoint
ID | FullName | EmployeeNumber | Created | Created By | … |
1 | Matthew Devaney | 1050 | 05/08/2022 | Matthew Devaney | … |
2 | Alice Lemon | 0958 | 05/10/2022 | Matthew Devaney | … |
3 | David Johnson | 0563 | 05/13/2022 | Matthew Devaney | … |
4 | Sarah Green | 1002 | 05/13/2022 | Matthew Devaney | … |
5 | Kelly Smith | 1066 | 05/29/2022 | Matthew Devaney | … |
varEmployeeCurrent record in Power Apps
{
ID: 5,
FullName: "Kelly Smith",
EmployeeNumber: 1066,
'Created By': Date(2022, 05, 29),
Created: Matthew Devaney,
Modified: Date(2022, 05, 29),
Modified By: Matthew Devaney
}
4. Create Multiple New Records With Power Apps Patch Function
Syntax
Patch(Datasource, BaseRecordsTable, NewRecordsTable)
Input
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Green | 1002 | 03/14/2018 | true |
5 | Kelly Smith | 1066 | 05/20/2022 | true |
Code
ClearCollect(
colNewEmployees,
Table(
Employees@{
FullName: "Mary Baker",
EmployeeNumber: 0798,
HireDate: Date(2022, 06, 06),
Active: true
},
Employees@{
FullName: "John Miller",
EmployeeNumber: 1203,
HireDate: Date(2022, 06, 11),
Active: true
},
Employees@{
FullName: "Susan Wright",
EmployeeNumber: 0590,
HireDate: Date(2022, 06, 23),
Active: true
}
)
);
Patch(
Employees,
ForAll(
Sequence(CountRows(colNewEmployees)),
Defaults(Employees)
),
colNewEmployees
);
Output
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Green | 1002 | 03/14/2018 | true |
5 | Kelly Smith | 1066 | 05/20/2022 | true |
6 | Mary Baker | 0798 | 06/06/2022 | true |
7 | John Miller | 1203 | 06/11/2022 | true |
8 | Susan Wright | 0590 | 06/23/2022 | true |
5. Edit Multiple Existing Records Using Power Apps Patch Function
Syntax
Patch(Datasource, BaseRecordsTable, UpdateRecordsTable)
Input
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Green | 1002 | 03/14/2018 | true |
5 | Kelly Smith | 1066 | 05/20/2022 | true |
Code
ClearCollect(
colUpdateEmployees,
Table(
Employees@{
ID: 2,
FullName: "Alice Henderson",
EmployeeNumber: 1001
},
Employees@{
ID: 4,
Active: false
},
Employees@{
ID: 5,
HireDate: Date(2022, 08, 01)
}
)
);
Patch(
Employees,
ShowColumns(
colUpdateEmployees,
"ID"
),
colUpdateEmployees
);
Output
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Henderson | 1001 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Green | 1002 | 03/14/2018 | false |
5 | Kelly Smith | 1066 | 08/01/2022 | true |
6. Upsert Multiple Records With Power Apps Patch Function
Syntax
Patch(Datasource, BaseRecordsTable, UpsertRecordsTable)
Input
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Lemon | 0958 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
Code
ClearCollect(
colUpsertEmployees,
Table(
Employees@{
ID: 2,
FullName: "Alice Henderson",
EmployeeNumber: 1001
},
Employees@{
ID: Blank(),
FullName: "Sarah Green",
EmployeeNumber: 1002,
HireDate: Date(2018, 03, 14),
Active: false
},
Employees@{
ID: Blank(),
FullName: "Kelly Smith",
EmployeeNumber: 1066,
HireDate: Date(2022, 05, 20),
Active: true
}
)
);
Patch(
Employees,
colUpsertEmployees
);
Output
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
2 | Alice Henderson | 1001 | 11/03/2015 | true |
3 | David Johnson | 0563 | 08/15/2013 | false |
4 | Sarah Green | 1002 | 03/14/2018 | false |
5 | Kelly Smith | 1066 | 08/01/2022 | true |
7. Change Values In A Record Variable Using Power Apps Patch Function
Syntax
Patch(Record1, Record2)
Input
Record stored in a global variable named gblEmployee
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1050 | 05/28/2010 | true |
Code
Patch(
gblEmployee,
{EmployeeNumber: 1063}
);
Output
Employees Table In SharePoint
ID | FullName | EmployeeNumber | HireDate | Active |
1 | Matthew Devaney | 1063 | 05/28/2010 | true |
More Power Apps Patch Function Tips And Tricks
Want to learn more about the Patch function? Check out these awesome otherarticles I’ve written:
Everything You Need To Know About Power Apps Patch Forms
A full tutorial on how to build a Power Apps patch form including the topics: form submissions, data validation, error-handling and updating a previously submitted record.
Patch Multiple Records In Power Apps 10X Faster
A nifty trick I discovered to submit multiple records at once really really quickly.
Power Apps Patch Function Examples For Every SharePoint Column Type
Example of how to patch every SharePoint column type in an easy to read format.
Learn how to check a form for errors on submission and eliminate the possibility to losing entered data.
Power Apps Excel-Style Editable Table
Make an excel-style table in Power Apps you users will love by using the Patch function
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps articles sent to your inbox each week for FREE
Questions?
If you have any questions about 7 Ways To Use The PATCH Function In Power Apps please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.
Hi Matthew, as usual another great article. On this topic… I’d like some examples for patching more complex fields like a “person” field or a “persons” field from a combobox. In addition it would be nice to know the correct way to blank out person and persons columns.
Have you already covered these topic before?
Hello Richard,
Awhile back I wrote an article called “Power Apps Patch Function Examples For Every SharePoint Column Type.” Take a look using this link and let me know if its what you were looking for (https://www.matthewdevaney.com/power-apps-patch-function-examples-for-every-sharepoint-column-type/)
Here’s a direct link to the one about ComboBox + Person column:
https://www.matthewdevaney.com/power-apps-patch-function-examples-for-every-sharepoint-column-type/patch-a-sharepoint-person-column-in-power-apps/
And here’s for multiple people:
https://www.matthewdevaney.com/power-apps-patch-function-examples-for-every-sharepoint-column-type/patch-a-sharepoint-person-column-with-multiple-people-in-power-apps/
Hello
Thanks for this good cheat sheet.
I found 2 little errors:
In code of section 2 : “EmployeeNumber: 1003” –> “EmployeeNumber: 1002”
In code of section 6 : You Patch the colUpdateEmployees collection and declare colUpsertEmployees collection.
Thanks for your quality content.
Pierre,
Thank you for helping with quality control. I have made the requested corrections and they will appear once my site cache refreshes.
Typo: In example 2, shouldn’t it read ID=4 rather than ID=2?
Andrew,
Fixed now. Thank you for the helpful comment!
Grand master of the patch function.
Thank you Matthew.
Kylz,
Many thanks 🙂
Great article! We can also use Patch for mix Forms.
Andres,
What is a mix form?
I was referring to this. Just an example 🙂
Hi Matthew,
Not sure if I’m asking this in the right place, but I’m looking for a way to patch into a SharePoint list in the following way;
In Power Apps I’m looking to build a form that allows me to select an event name (sourced from a SharePoint List), date and start time and another input for number of participants.
This needs to patch into another SharePoint list where a row is created depending on the number of participants given. So if for example I have an “introduction” event, with a number of participants of 7, the SharePoint list should have 7 rows where Introduction event and the start date and time is patched in. This list would have two additional columns for name and transport required which is information that is collected later and is entered into the SharePoint list directly.
I looked at your post on “Power Apps Excel-Style Editable Table” which will let me build a way to input the information on the form but I would then have to enter the name of the event and the start date and time multiple times which would become an issue if there would be a few dozen attendees.
Any suggestions are greatly appreciated!
Micha,
This question is complex, sorry, I’m not able to answer it in a comment on my blog 🙁
Suggest you check out this wonderful video by my good friend Reza Dorrani who also has content on editable tables. I think you will find it very helpful: https://www.youtube.com/watch?v=wI6SHGQ9ATg.
Hi Matthew,
Thanks for the reference, it was really helpful for creating editable tables.
I was able to get it to create the number of rows in a different list using power automate so i have a working solution!
Thanks again!
Great article, thanks, and thanks to the other eagle eyed readers that spotted colUpsertEmployees too.
Sarah,
Agreed. The readers of this blog are a huge asset and always tell me where I can make improvements.
Hi Matthew, I was working with Bulk Patch using collection, but I am not able to handle errors in this, after patch I have a success screen but if there is any error it should not go to success screen. Can you help me with this? Thanks
Sapna,
Turn on the experimental error-handling feature. Then wrap the patch function code in an IFERROR function.
IfError(Patch(datasource, colPatchRecords), Set(varError, true));
If(varError, Notify(“There was an error”, NotificationType.Danger), Navigate(‘Success Screen’));
Thanks
Hello Matthew, thanks for this great article.
Section 6 results seem like different behavior in my lab.
If the ID column is blank (such as Sarah, Kelly in the collection), a new record will be created.
However, in this article section 6, Sarah, Kelly already exist (ID:4,5) and the records are updated after executing the patch function even though IDs are blank in the collection.
I guess the ID:4,5 rows in the input isn’t necessary.
Takashi,
I made an error. The input table for example #6 should had been only items 1, 2, 3. Then the PATCH function creates items 4 and 5. I’ve fixed it now.
Thanks
Hi Matthew,
When you execute ClearCollect(<colName>: Table(<records>)), the colon character instead of a comma generates an error right ?
Or am I missing something with formula experimental features that I should tick in the studio settings?
PS: I guess also that Table( ) function is only mandatory in a table type variable declaration with Set( ) but not in a Collect( ) function when you can directly stack the records in a JSON fashion
Matthieu,
Good point. I’ve corrected examples #4, 5 and 6.
Can we be best friends. Just saying. Your articles make my life so much easier.
Meg,
If you enjoy Coffee ☕, Cats 🐱 & Code 👩💻 we can definitely be best friends.
Well, I think we just became best friends. Can’t wait to keep seeing articles from you!
Hi Matthew,
Thanks for the great examples! Maybe I’m just looking at this incorrectly, but is “upsert” not a typo? If not, then I’m confused and you can ignore this comment 🙂
Steve,
Believe it or not “upsert” is actually a thing. An upsert will update the record if it exists, otherwise, it will create a new record.
Thank you for all of the articles that you post, Matthew.. They are all very helpful!
Is it possible to upsert multiple Sharepoint items using example 6 above if a choice column is one of the columns that needs to be updated? When I include the choice column in the collection using ColorChoice: {Value: “Green”} the Patch function has an “Invalid argument type (Table). Expecting a Record value instead” error.
Ferguson,
I’ve done some testing and it looks like there was a regression in the recent version of Power Apps. Today I sent a message to the product team and I’m hoping they will respond with a fix soon. This is supposed to work. And it did work, until recently.
Hello – I am trying to patch to Salesforce using #4 above.. I can patch with a ForAll(collection,Patch(source,…)).It takes forever for a larger collection (i.e. ~10,15,20-30 or more)..My goal is to patch faster than I currently am but failing miserably.
I have also reviewed your “PATCH Multiple Records In Power Apps 10x Faster” but nothing seems to be working. Do you have any recommendations on bulk patching to Salesforce?
AzAh,
I think the Power Apps PATCH function is broken and will not work for Step #4. I will email the product team to report the bug.
Azah,
Please check my updated code. Fixed.
Great article and very helpful examples. Is it a typo where the collection names on 5 and 6 are mixed up? update on the creating and upsert on the patching and vice versa?
Johan,
Yes sir. I made an error. Thanks for reporting. It’s now fixed 🙂
Hello Matt! Awesome article, the bulk patch with a collection is a lifesaver. I have a question though. I am dealing with Offline canvas apps using Dataverse as the datasource. I have defined a new primary key to be an offline GUID that is created inside the application. I am wondering if there is a way to do an Upsert using bulk patch method with a collection but using my new primary key as the “ID”? Please let me know if you have any ideas and thank you again for this excellent content.
–Tim
Tim,
I’m not aware of any way to use a different key than the unique identifier for bulk imports.
Have you tried defining your offline ID as a “Key” in the Dataverse table settings? I wonder if that would do it.
Hi Matthew – thanks for a super-helpful article. Patch is such a confusing function.
I have a question about one usage of Patch which seems not to be covered here. I have an app which contains the following code:
Here, tRegistrations is a SharePoint list to which I am adding a bunch of new registrations. The new registrations are assembled in the collection collNewRegistrations, and then added to tRegistrations all at once by the Patch statement. collNewRegistrations has columns corresponding exactly to those in tRegistrations except that it does not have the ID column or other read-only columns.
This use of Patch seems not to be covered in your article (in all your examples, the Patch function has 3 arguments) or in MS’s documentation (unless I’ve overlooked it).
It’s a bit of a surprise to me to find that this usage works. Are you able to explain it?
Andrew,
This undocumented usage of the PATCH function you mentioned is an interesting one:
Patch(tRegistrations, collNewRegistrations);
It’s effectively an UPSERT. If the record does exist in the database, it gets updated. If the record does not exist in the database, it gets created. Records are identified by their unique identifier. In SharePoint, this is the ID column.
I loved this usage for quite awhile. But it has one major drawback. If any record in the collection fails to create/update, it’s impossible to tell which one. That’s probably why Microsoft has not documented it.
Hi Matthew,
Thank you for the detailed examples on using the Patch function. In #7 the gblEmployee variable references what exactly?
Similarly, I’m trying to update the value in a record but I cannot get it to work.
Ryan,
Excellent point. gblEmployee refers to the input show above the code. I said it was a SharePoint list. But instead I meant it to be a global variable where a record where a variable is stored. Fixed 🙂
Hello Matthew,
Thank you for the prompt update. Just tried it out again and I got it to work. Nice work!
Thanks for this article! This finally made me understand the differences and I found where I went wrong in my app.
Kevin,
I’m glad you found it useful. There are so many ways to write a darned PATCH function. It’s nice to see them all in one place.
Hello! This is better than MS articles!
I just want to ask, do you have something like the real Upsert?
Like Update if found, Insert if not on only a single record?
AerisLives,
I love your name. Is it a reference to Aries Gainsborough in Final Fantasy VII? That game was awesome.
Example #6 in this article does a real Upsert. Did you have a chance to look at it?
oh yes, from FFVII! <3
As for #6, I saw it but I think the update and inserts are separate.
I would like first to check if, let’s say, Character Name: Aeris is present in the List, then update Died: Yes from Died: No.
If not, Insert New Character Name: Aeris, Died: Yes to List instead.
I could do an IF statement but is there a more efficient one?