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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / My collect and patch i...
Power Apps
Answered

My collect and patch is creating duplicates

(0) ShareShare
ReportReport
Posted on by 301

Hi,

 

I have a gallery on my app that the user looks at a collection and by looking at its unique PID they then give the row a HID and saves via this code. However if i also save the variable to another collection (colFinalHoleData) and press the button twice it creates two entries, how do i get it to look in the collection check if there is an entry already and patch if the PID is already there?

 

 

Set(varItem, 
 {ID: HID.Text,
 Status: "Started",
 PID: PID.Text,
 EL: GalEL.Text,
 Easting: HoleGalEastings.Text,
 Northing: HoleGalNorthings.Text,
 HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
 }
);
Patch(
 colData,
 HOLEIDGallery.Selected,
 {
 HoleID: HID.Text,
 Status: "Started",
 PEGID: PID.Text,
 EL: GalEL.Text,
 Easting: HoleGalEastings.Text,
 Northing: HoleGalNorthings.Text,
 HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
 }
);
SaveData( 
 colHOLEData,
 "SavedHOLEData" 
);
Collect(
 colFinalHOLEData,
 varItem
);SaveData( 
 colFinalHOLEData,
 "SavedFinalHOLEData" 
);

 

 

 

The collection that creates doubles is colFinalHOLEDATA, i take it i use an IF statement and do i use a lookup code? I cant figure it out with the variable varitem?

Categories:
I have the same question (0)
  • v-xiaochen-msft Profile Picture
    Microsoft Employee on at

    Hi @ShaneITAutomate ,

     

    According to your description, the point is to use the if() function, countrows() function and filter() function.

    If the number of records found is 0, then patch the new record, otherwise do nothing.

     

    You could try this formula:

    If(CountRows(Filter(colFinalHOLEData,PID=[@PID].Text))=0,Collect(colFinalHOLEData,varItem))

     

    Instead of

    v-xiaochen-msft_0-1613456931993.png

     

     

    I've made a simple test for your reference:

    1\ Add a textinput control ‘PID’.

     

    2\ Add a button control and set its onselect property to:

    Set(varItem,{PID:PID.Text});
    If(CountRows(Filter(colFinalHOLEData,PID=[@PID].Text))=0,Collect(colFinalHOLEData,varItem))
    
    // For demonstration purposes, the varItem variable has only one column here. PID is a unique value.

     

    3\ The result is as follows:

    AAA.gif

    v-xiaochen-msft_1-1613457007353.png

     

     

    Best Regards,
    Wearsky
    If my post helps, then please consider Accept it as the solution to help others. Thanks.

  • ShaneITAutomate Profile Picture
    301 on at

    Thanks for replying

    Sorry it may be me and i may have a lack of understanding of why you did what you did in your app, but what would happen if the user changes something else and they wanted to go back and change it? doesnt it need a If statment saying if there is no entry then collect if there is already an item in it then patch?

    kinda like below

    ForAll(
     If(
     CountRows(
     Filter(colFinalHOLEData,PID=varItem.PID))=0
     ,
     Collect(colFinalHOLEData,varItem)
     ,
     Filter(colFinalHOLEData,PID=varItem.PID,
    
     Patch(colFinalHOLEData,
     {
     PID:PID,
     })
     )

     

    because what would happen if the user wanted to edit there changes with the same pegid? does it just overright it so you dont need to patch a previous entry?

     

    Sorry if there is something obvious im missing here/

  • Verified answer
    v-xiaochen-msft Profile Picture
    Microsoft Employee on at

    Hi @ShaneITAutomate ,

     

    Could you tell me:

    1. When the user creates a record in colData, do you want to create the same record in colFinalHOLEData?
    2. When the user updates a record in colData, do you want to update this record in colFinalHOLEData?

     

    If so , firstly, If you want to associate two collections, you should not allow users to modify the PID value(Users are not allowed to modify the value of PID control), because it is a unique identifier.

     

    Secondly ,please try this formula:

    Set(varItem, 
     {ID: HID.Text,
     Status: "Started",
     PID: PID.Text,
     EL: GalEL.Text,
     Easting: HoleGalEastings.Text,
     Northing: HoleGalNorthings.Text,
     HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
     }
    );
    Patch(
     colData,
     HOLEIDGallery.Selected,
     {
     HoleID: HID.Text,
     Status: "Started",
     PEGID: PID.Text,
     EL: GalEL.Text,
     Easting: HoleGalEastings.Text,
     Northing: HoleGalNorthings.Text,
     HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
     }
    );
    SaveData( 
     colHOLEData,
     "SavedHOLEData" 
    );
    If(CountRows(Filter(colFinalHOLEData,PID=[@PID].Text))=0,Collect(colFinalHOLEData,varItem),Patch(colFinalHOLEData,LookUp(colFinalHOLEData,PID=[@PID].Text),{ID: HID.Text,
     Status: "Started",
     EL: GalEL.Text,
     Easting: HoleGalEastings.Text,
     Northing: HoleGalNorthings.Text,
     HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
     }))
    ;
    SaveData( 
     colFinalHOLEData,
     "SavedFinalHOLEData" 
    );

     

    Finally, if there is anything in the formula that cannot meet the needs, please let me know.Thanks.

     

    Best Regards,
    Wearsky
    If my post helps, then please consider Accept it as the solution to help others. Thanks.

     

  • ShaneITAutomate Profile Picture
    301 on at

    Yes to one and two, the reason i have two collections displaying the same info is because i only send the data from the 2nd collection as the first collection contains 900 odd entries, so the patch on the second only takes a moment and is displayed in a datatable and the changes in the first collection can be edited and is displayed in a gallery

  • ShaneITAutomate Profile Picture
    301 on at

    Also it is still not recognizing there is already a entry in the colFinalHoleData collection and adding a new one instead of patching?

  • Verified answer
    ShaneITAutomate Profile Picture
    301 on at

    I was able to get it to work by changing yours to this

    Set(varItem, 
     {ID: HID.Text,
     Status: "Started",
     PID: PID.Text,
     EL: GalEL.Text,
     Easting: HoleGalEastings.Text,
     Northing: HoleGalNorthings.Text,
     HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
     }
    );
    Patch(
     colData,
     HOLEIDGallery.Selected,
     {
     HoleID: HID.Text,
     Status: "Started",
     PEGID: PID.Text,
     EL: GalEL.Text,
     Easting: HoleGalEastings.Text,
     Northing: HoleGalNorthings.Text,
     HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
     }
    );
    SaveData( 
     colHOLEData,
     "SavedHOLEData" 
    );
    If(CountRows(Filter(colFinalHOLEData,PID=varItem.PID))=0,Collect(colFinalHOLEData,varItem),Patch(colFinalHOLEData,LookUp(colFinalHOLEData,varItem.PID),{ID: HID.Text,
     Status: "Started",
     EL: GalEL.Text,
     Easting: HoleGalEastings.Text,
     Northing: HoleGalNorthings.Text,
     HoleCompletedDate:Text(Today(), "[$-en]dd/mm/yyyy")
     }))
    ;
    SaveData( 
     colFinalHOLEData,
     "SavedFinalHOLEData" 
    );

     

    Thankyou, however i just need one more part for it to be perfect, i want them to be unable to save if HID is blank>?

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 416 Most Valuable Professional

#2
11manish Profile Picture

11manish 205 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard