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 / Patch function to Upda...
Power Apps
Unanswered

Patch function to Update existing records and Create new Records

(0) ShareShare
ReportReport
Posted on by 6

Hi Everyone,

I am creating a Power App that tracks the Attendance status of the Users.

Have 2 Excel sheets or tables for the data. 

1. User_Data

First_NameLast_NameFull_NameMail_Id
JohnSmithJohn Smithjohn.smith@email.com
EmilyJohnsonEmily Johnsonemily.j@email.com
DavidBrownDavid Browndavid.brown@email.com
SarahLeeSarah Leesarah.lee@email.com
MichaelTaylorMichael Taylormichael.t@email.com

 

2. Attendance_Tracker

Full_NameMail_IdAttendance DateAttendance Status
John Smithjohn.smith@email.comApr-01-2024Attended
Emily Johnsonemily.j@email.comApr-01-2024Not Attended
David Browndavid.brown@email.comApr-01-2024Attended
John Smithjohn.smith@email.comApr-02-2024Not Attended

 

The basic idea here is to bulk update records with the Attendance Date and Attendance Status.

The User_Data is the data source for a multi-select gallery that collects the selected records in a collection 'colSelectedData'.

I have a separate section that takes date from a Date Picker and Dropdown for the Attendance Status having 2 options - 'Attended' & 'Not Attended'.

 

Once I click on Update button, I want the records to be added to the Attendance_Tracker data source/table.

I am using the below query to do so

 

Set(
 varTimeStart,
 Now()
);
ForAll(
 colSelectedData,
 Patch(
 Attendance_Tracker,
 If(
 !IsBlank(
 LookUp(
 Attendance_Tracker,
 'Mail_Id' = ThisRecord.'Mail_Id' && 'Attendance Date' = ThisRecord.'Attendance Date'
 )
 ),
 LookUp(
 Attendance_Tracker,
 'Mail_Id' = ThisRecord.'Mail_Id' && 'Attendance Date' = ThisRecord.'Attendance Date'
 );
 {
 'Full_Name': ThisRecord.'Full_Name',
 'Mail_Id': Lower(ThisRecord.'Mail_Id'),
 'Attendance_Status': attendanceStatusDropdown.Selected.'Attendance_Status',
 'Attendance Date': Text(
 attendanceDatePicker.SelectedDate,
 "mmm-dd-yyyy"
 )
 },
 {
 'Full_Name': ThisRecord.'Full_Name',
 'Mail_Id': Lower(ThisRecord.'Mail_Id'),
 'Attendance_Status': attendanceStatusDropdown.Selected.'Attendance_Status',
 'Attendance Date': Text(
 attendanceDatePicker.SelectedDate,
 "mmm-dd-yyyy"
 )
 }
 )
 )
);
Set(
 varTimeDiff,
 DateDiff(
 varTimeStart,
 Now(),
 TimeUnit.Seconds
 )
);
Clear(colSelectedData);
Notify(
 "Attendance Details were updated successfully in : " & varTimeDiff & " seconds",
 NotificationType.Success
)

 

But this creates a new record every time leading to duplicate records for the same date. What I want to achieve is to

1. Update the existing record if for a user the Attendance status has already been populated for the selected Date!

2. Create a new record if Attendance hasn't been marked for the selected Date.

 

Please provide your valuable insights on this use case.

Categories:
  • IAmIntractable Profile Picture
    280 on at

    I have zero experience using Excel as a database:

     

    I don't see any reference to Defaults which indicates a new row when patching:Defaults('Attendance_Tracker'). Perhaps using something like this to set the datasource:

     

        if(   IsBlank(

                    LookUp(

                        Attendance_Tracker,

                        'Mail_Id' = ThisRecord.'Mail_Id' && 'Attendance Date' = ThisRecord.'Attendance Date'

                    ).'Mail_Id’

              ),

                    defaults(‘Attendance_Tracker’)

        , //ELSE

                    LookUp(

                        Attendance_Tracker,

                        'Mail_Id' = ThisRecord.'Mail_Id' && 'Attendance Date' = ThisRecord.'Attendance Date'

                    )

        )

     

    The presumes the criteria for the Lookup will actually return a matching row. You might want to test the Lookup alone with criteria that should find a match and criteria that will not find a match to be sure that Lookup works as expected.

  • WarrenBelz Profile Picture
    156,774 Most Valuable Professional on at

    Hi @Anonymous_Dev ,

    Firstly Excel is a terrible data source where things that should work simply do not (this would be easy to do with SharePoint). There is nothing technically wrong with what you have that I can see - I am posting a variation below that might work for you.

    Set(
     varTimeStart,
     Now()
    );
    Patch(
     Attendance_Tracker,
     ForAll(
     colSelectedData As _Data,
     With(
     {
     _Record:
     LookUp(
     Attendance_Tracker,
     'Mail_Id' = _Data.'Mail_Id' && 
     'Attendance Date' = _Data.'Attendance Date'
     )
     },
    		 Coalesce(
     _Record,
     Defaults(Attendance_Tracker)
     ),
     {
     
     'Full_Name': _Data.'Full_Name',
     'Mail_Id': Lower(_Data.'Mail_Id'),
     'Attendance_Status': attendanceStatusDropdown.Selected.'Attendance_Status',
     'Attendance Date': 
     Text(
     attendanceDatePicker.SelectedDate,
     "mmm-dd-yyyy"
     )
     }
     )
     )
    );
    With(
     {
     _TimeDiff,
     DateDiff(
     varTimeStart,
     Now(),
     TimeUnit.Seconds
     )
     },
     Notify(
     "Attendance Details were updated successfully in : " & _TimeDiff & " seconds",
     NotificationType.Success
     )
    );
    Clear(colSelectedData);

     

    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.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • Anonymous_Dev Profile Picture
    6 on at

    Hi @WarrenBelz & @IAmIntractable ,

    Thank you for your inputs.

    I did try both the variations that you suggested but unfortunately none of them seems to work as in its still creating new records instead of updating an existing record.

    So, I am moving the data from Excel to a SharePoint list and once that is done, I'll try again with the suggestions you both have provided.

     

    Will post an update here and accept the solution that works best for my use case.

  • WarrenBelz Profile Picture
    156,774 Most Valuable Professional on at

    Thanks @Anonymous_Dev ,

    You will find SharePoint much more accommodating.

  • Anonymous_Dev Profile Picture
    6 on at

    Hi Everyone,

    So I moved the lists User_Data and Attendance_Tracker to SharePoint.

    Unfortunately, I am facing the same issue where it's able to create new records but then not able to update an existing record.

    Tried using Update function as well which surprisingly is not even allowing me to run the query as it keeps asking for Field: ID which I cannot supply from the Power App cause IDs are unique, but attendance tracker will contain multiple rows for same Name and Mail ID so I can't even use Mail ID as a primary key or ID.

    Structure for my lists are as follows

    1. User_Data

    Column NameType
    First_NameSingle line of Text (Required)
    Last_NameSingle line of Text (Required)
    Full_NameSingle line of Text (Required)
    Mail_IdSingle line of Text (Required)

     

    2. Attendance_Tracker

    Column NameType
    Full_NameSingle line of Text (Required)
    Mail_IdSingle line of Text (Required)
    Attendance_StatusChoice (Required) - Attended, Not Attended
    Attendance_DateDate & Time (Required) - Only Date no time

     



    Would really love to know if there is another way to implement this or refine the query. 

     

    FYI: queries are pretty much same what I added in the initial post and tried what were suggested by @WarrenBelz & @IAmIntractable as well.

  • Anonymous_Dev Profile Picture
    6 on at

    @WarrenBelz any suggestions on how to tackle this issue.

  • WarrenBelz Profile Picture
    156,774 Most Valuable Professional on at

    @Anonymous_Dev ,

    Some code (in Text) and the results of this would be a good start.

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

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 342 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 258

#3
Mohsin Ali Profile Picture

Mohsin Ali 231

Last 30 days Overall leaderboard