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 / Duplicate Problem Whil...
Power Apps
Answered

Duplicate Problem While Patching my Data Source

(0) ShareShare
ReportReport
Posted on by 25

Hi Everyone.

 

I have i problem getting the right way to patch my data source with my collection. The part that i update my collection works perfectly, but when i try to update my Data Source with my collection using the Patch function, The data existing in my collection is duplicated in my Data Source. This is a picture of the case:

Aleksey24_1-1669997405075.png

 

Categories:
I have the same question (0)
  • Tolu_Victor Profile Picture
    197 Super User 2024 Season 1 on at

    Hi,
    You can use your Patch statement with conditions to check if the record exists. That way it will always update and only creates if no record matches.
    See below. Modify to your specs

     

    ForAll(
    	colltaskupdate,
    	Patch(
    		ExcelTable,
    		Coalesce(Lookup(colltaskupdate, ID = ThisRecord.ID),Defaults(ExcelTable)),
    		//Coalesce returns first non-blank value
    		{
    			ID: ThisRecord.ID,
    			GROUP: ThisRecord.GROUP,
    			add other fields
    		}
    	)
    )

     

    -------------------------------------------------------------------------
    If I have answered your question, please mark your post as Solved.
    If you like my response, please give it a Thumbs Up.

     

  • WarrenBelz Profile Picture
    153,117 Most Valuable Professional on at

    Hi @Aleksey24 ,

    Firstly @Tolu_Victor is correct in the update logic, but your code should actually work if the ID of the record (I assume this is a SharePoint list) is in the Collection. I think your issue lies here (note the ID value in the top Patch)

    If(
     ThisItem.ID in colltaskupdate.ID, 
     Patch(
     colltaskupdate,
     {
     ID: Value(Textlnputl.Text),
     GROUP: Textlnput2.Text, 
     DESCRIPTION: Textlnput3.Text, 
     FIX: Textlnput4.Text,
     HIGH: TextInput5.Text,
     WIDE: TextInput6.Text,
     LENGTH: TextInput7.Text
     }
     ),
     Collect(
     colltaskupdate,
     {
     ID: Value(Textlnputl.Text),
     GROUP: Textlnput2.Text, 
     DESCRIPTION: Textlnput3.Text, 
     FIX: Textlnput4.Text,
     HIGH: Textlnput5.Text,
     WIDE: TextInput6.Text,
     LENGTH: Textlnput7.Text
     }
     )
    )

    If this still does not work, the alternative is to head down the second Patch road as @Tolu_Victor described, I will offer some fundamental advice here that may assist with this and future ForAll() exercises. It is not designed to be a Loop, although it can work this way with considerable performance penalty as it does an individual Patch for each record. ForAll() creates a Table, which can be patched in one action provided its content is correct. For new records, this is simply a Table with field names and field types matching the list. For existing records, including the SharePoint record ID in the table causes it to update that record for each of the Table records. Therefore you would do something like this

    Patch(
     ExcelTable,
     ForAll(
     colltaskupdate As aPatch,
     With(
     {
     wID:
     Lookup(
     colltaskupdate, 
     ID = aPatch.ID
     ).ID
     },
     { 
     ID: 
     If(
     !IsBlank(wID),
     wID
     ),
     GROUP: aPatch.GROUP,
     DESCRIPTION: aPatch.DESCRIPTION,
     FIX: aPatch.FIX,
     HIGH: aPatch.HIGH,
     WIDE: aPatch.WIDE,
     LENGTH: aPatch.LENGTH
     }
     )
     )
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    Visit my blog Practical Power Apps

  • Aleksey24 Profile Picture
    25 on at

    Hi! @WarrenBelz 

    ¡Thank you so much for your answer!. i still have some issues, although i tried the "patch solution" and "update solution" alongside the "Value()" addition, it still doesn't work, the rows in the collection duplicates in the data source, The data source is an excel table in one drive, so i think that is the problem, the "Data source". To apply your solution "Value()", i had to change the format type of the ID column in the excel table to number for it to work in powerapps, otherwise it shows an error "type error" , but even when the code work's with your sugestion, it still duplicates the rows as if it doesn't underenstand that the id value is the one to grab and update. In a Nutshell, do i have to migrate my data source? or there is any way i can fix this problem as it is? This is an image of the current situation:

     

    Aleksey24_1-1670257660339.png

    As for example, if i change the first two cells of the fix column in my gallery, this is what the collection "colltaskupdate" looks like:

    Aleksey24_2-1670258024879.png

    So far so good, then i want to patch just those two rows (only rows currently existing in my collection) in my excel table "ExcelTable" in one drive as my data source, with a bulk update using "patch(ExelTable,colltaskupdate)", it duplicates as you can see below:

    Aleksey24_3-1670258424304.png

    Thank you again for your attention.

     

     

     

  • Verified answer
    WarrenBelz Profile Picture
    153,117 Most Valuable Professional on at

    Hi @Aleksey24 ,

    It is also very beneficial to state your data source in your initial post as this will often affect any solutions offered. The Value() was only because the ID in SharePoint (which I assumed as your data source) is a number. For your code to work, you need a unique identifier in each list record (such as the ID in SharePoint) created when the record is created. If you have this (now I know this is Excel), you can do this

    If(
     ThisItem.ID in colltaskupdate.ID, 
     Patch(
     colltaskupdate,
     LookUp(
     colltaskupdate,
     ID = Textlnputl.Text
     ),
     {
     GROUP: Textlnput2.Text, 
     DESCRIPTION: Textlnput3.Text, 
     FIX: Textlnput4.Text,
     HIGH: TextInput5.Text,
     WIDE: TextInput6.Text,
     LENGTH: TextInput7.Text
     }
     ),
     Collect(
     colltaskupdate,
     {
     ID: Textlnputl.Text,
     GROUP: Textlnput2.Text, 
     DESCRIPTION: Textlnput3.Text, 
     FIX: Textlnput4.Text,
     HIGH: Textlnput5.Text,
     WIDE: TextInput6.Text,
     LENGTH: Textlnput7.Text
     }
     )
    )

    However, if you have access to SharePoint, then I highly recommend you migrate to it now - Excel will cause you a lot more grief than this in the future if you want anything other than very basic functionality.

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    Visit my blog Practical Power Apps

     

     

     

     

  • Aleksey24 Profile Picture
    25 on at

    Thank you so much for your assistance, this is the right solution, though it has something extra to be done in order for it to work properly, in this situation, as you can see in the pictures that i add above, there are some "clear()" in the OnStart section of the app, and in the apply button, i made those changes so the collection stayed black till i fill it with new information, but that causes the "patch" command in the button "apply" to fail the update of the data source, im guessing it is becauce i wanted to do a  bulk update, so the data source has to be exactly the same as the collection (At least with the one drive excel table as my data source), but because i was delivering just 2 to 3 rows to update the data source, the condition just wasn't satisfied, So i just errased the clear() commands and the code started to work as planed. 

     

    Thank you so much for your answer, i let this comment to left the complete solution.

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 765 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 272

Last 30 days Overall leaderboard