Hi,
If I understand correctly, you want a system for signing in one record and signing out the same record.
Below is your formula:
Patch(Table2, First(Filter(Table2, SignIn = "")),
{SignIn: Text( Now(),"[$-en-US]mm/dd/yyyy hh:mm:ss" )
}
)This means, "Save a timestamp in the first record in Table2 whose SignIn field is blank."
The problem you might be facing is that two users sign in; their sign in is logged, User1 then User2. User2 logs out first, but they override User1's sign-in because it is the first blank in Table2.
The solution is to reconfigure your table then add more conditions to the Filter.
Reconfigure your table
I'm not sure what your table looks like, but adding more columns can help identify which person you want to log out.
I assume you have these columns:
- LastName: last name of the user
- FirstName: first name of the user
- UserID: id number of the user to distinguish users who have the same first and last name
- SignIn: time signed in
- SignOut: time signed out
- __PowerAppsId__: a unique id created by PowerApps that identifies each record
I presume that you are keeping a growing list and not replacing old records for sign-in and sign-out. You could keep the first and last name in another table as a lookup--I kept them in for convenience though.
Add more conditions to the Filter
I have two solutions for rewriting the formula: one has fewer changes from your original, the other uses context variables.
Method 1: Revise the Patch formula with more conditions
For signing in:
Patch(Table2, Defaults(Table2),
{LastName: [however you get the last name],
FirstName: [however you get the first name],
UserID: [however you get the id],
SignIn: Text( Now(),"[$-en-US]mm/dd/yyyy hh:mm:ss" )
}
)This means write a new record to Table2 that includes the LastName, FirstName, UserID, and SignIn timestamp.
Assuming they do not sign in again or they do not forget to sign out, the following formula can pick out which record to write for signing out:
Patch(Table2, First(Filter(Table2,LastName=[reference the control/variable with the last name] && IsBlank(SignOut))),
{SignOut: Text( Now(),"[$-en-US]mm/dd/yyyy hh:mm:ss" )
}
)This means, "Find the first record in Table2 that matches the user's last name and has a blank SignOut time, then write a timestamp to the SignOut field."
Method 2: Use a context variable
The first method can fail if the same user had forgotten to sign out the first time and as a result they have two records in the table with their name and blank SignOut fields. To resolve this, I use the unique PowerAppsId. I set a context variable equal to the SignIn record. When it comes time to sign out, I can write straight to the variable I had set.
UpdateContext({signinrecord:
Patch(Table2, Defaults(Table2),
{LastName: [however you get the last name],
FirstName: [however you get the first name],
UserID: [however you get the id],
SignIn: Text( Now(),"[$-en-US]mm/dd/yyyy hh:mm:ss" )
}
)
})It's the same as Method 1, but I wrapped the formula for writing a variable around it.
To sign out, recall the variable signinrecord:
Patch(Table2, signinrecord,
{SignOut: Text( Now(),"[$-en-US]mm/dd/yyyy hh:mm:ss" )
}
)or
Patch(Table2, First(Filter(Table2,__PowerAppsId__=signinrecord.__PowerAppsId__)),
{SignOut: Text( Now(),"[$-en-US]mm/dd/yyyy hh:mm:ss" )
}
)Both formulas mean locate the record that is identical to the variable signinrecord, then write the SignOut time.
Let me know if this works.