web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Patch existing records...
Power Apps
Answered

Patch existing records and add new records

(0) ShareShare
ReportReport
Posted on by 87

I have a collection in Powerapps for timesheet.

I get a selected week's timesheet and update exisiting rows with new values and on top of this i add new rows.

How do I patch the new values in existing records and add the new records ?

Categories:
I have the same question (0)
  • MasterB Profile Picture
    58 on at

    Hi,

     

    you can update a row in a collection via a combination of Patch and Lookup . The second parameter of patch is the record for update. Like in a real table you need some kind of predicate you can uniquely identify the record for update in the DB this would be usually the PrimaryKey
    So something like this:

     

    Patch(
    // DataSource
     coll_YourCollection, 
    // Record to update
     LookUp(
     coll_YourCollection,
     pk_filed = "whatever_id_that_fits"
     ),
    // Values for the cells to update 
     {
     input: "NewValue"
     }
     )

     

     

    To add a new record to the Collection you will just use the Collect function to append a new row...

     

    Best

    Jan ala MasterB

  • R2Power Profile Picture
    18 on at

    Something like this should do the trick, probably a little bit different if your Collection is not the same format as your table:

    ForAll(YourCollection,
     If(IsBlank(LookUp(YourTable, ID=ThisRecord.ID)), Patch(ThisRecord, {....}), Patch(Defaults(YourTable), {...}))
    )

     You probably need to put 

  • CU-18081211-6 Profile Picture
    9,270 Moderator on at

    @rahulswimmer 

    Like @MasterB, but also to include new records if is the case:

    ForAll(your collection,

    Patch(datasource, Coalesce(

    LookUp(datasource, column_id = collection_column_id), 

    Defaults(datasource)),{column_name1:value, column_name2:value}))

     

    Hope it helps !

  • rahulswimmer Profile Picture
    87 on at

    @gabibalaban  @R2Power  @MasterB  I will definitely try  that? 


    How do I find out the difference between existing collection and the new collection where new rows have been added, because I need to add the new records using defaults. So how do I detect if a record in collection is newly added ?

  • Verified answer
    RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @rahulswimmer 

    If your original collection is based off of your datasource (i.e. you did a ClearCollect(yourCollection, YourDataSource))

    Then it is even easier...

    If you want to write back changes and new items:

    Collect(yourDataSource, yourCollection)

    will take care of new and existing records and updates all in one.

     

    I hope this is helpful for you.

  • rahulswimmer Profile Picture
    87 on at

    I was able to find a solution. It goes as follows:

    // Update existing items in SharePoint
    ForAll(
     Filter(Col_PatchBack, Not(IsBlank(pbID))),
     UpdateIf(
     SharePointList,
     ID = pbID,
     {
     Hours: If(IsBlank(pbHours),0,pbHours),
     Date: pbDate,
     }
     )
    );
    
    // Finally add new items to SharePoint
    ForAll(
     Filter(Col_PatchBack, IsBlank(pbID)),
     Patch(
     SharePointList,
     Defaults(SharePointList),
     {
     Title: pbTitle,
     Hours: If(IsBlank(pbHours),0,pbHours),
     Date: pbDate,
     }
     )
    );

    You basically retrieve the ID of row for exisiting data and Patch it using UpdateIf, if ID exists it means you are updating existing item, if not its a new item.

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @rahulswimmer 

    Since ForAll returns a Table, you are kind of wasting the Output of the table with that formula.

    Consider the following that will execute quicker and will utilize ForAll for what it was intended for:

    // Update existing items in SharePoint
    Patch(SharePointList,
     ForAll(
     Filter(Col_PatchBack, !IsBlank(pbID)),
     {
     ID: pbID, 
     Hours: Coalesce(pbHours, 0),
     Date: pbDate,
     }
     )
     )
    );
    
    // Finally add new items to SharePoint
    Collect(SharePointList,
     ForAll(
     Filter(Col_PatchBack, IsBlank(pbID)),
     {
     Title: pbTitle,
     Hours: Coalesce(pbHours, 0),
     Date: pbDate,
     }
     )
    );

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard