Excel考勤数据匹配:合并签到签出表统计每日工时
Hey there! Let's work through this employee time-tracking challenge—you’re already thinking in the right direction with combining ID and Date for matching, but let’s refine the approach to make it reliable, efficient, and easy to maintain.
首选方案:Power Query(Get & Transform)
If you're using Excel 2016 or later, Power Query is hands down the best tool for this job. It handles data merging, cleaning, and transformation way more flexibly than worksheet functions, and it’s repeatable if your data updates regularly. Here’s how to set it up:
Import both sheets into Power Query
- Go to the Data tab → Get Data → From File → From Workbook
- Select your workbook, then choose both the "Sign-In" and "Sign-Out" sheets to load into the Power Query Editor
Clean up and rename fields
- For the Sign-In table, rename the
Timecolumn toSign-In Time(right-click the column header → Rename) - Do the same for the Sign-Out table: rename
TimetoSign-Out Time - Double-check that both tables have matching
Last Name,First Name,ID, andDatefields (no typos!)
- For the Sign-In table, rename the
Merge the tables
- Select the Sign-In table in the Power Query Editor
- Click Merge Queries → Merge as New
- In the merge window:
- Choose "Sign-Out" as the second table
- Select ID and Date as the matching columns (hold Ctrl to select multiple)
- Set the join kind to Left Outer (this keeps all Sign-In records, even if there’s no matching Sign-Out)
- Click OK
Expand the merged data
- Click the expand icon (🔽) on the new "Sign-Out" column
- Only check the
Sign-Out Timebox, then click OK
Calculate total hours worked
- Click Add Column → Custom Column
- Use this formula to calculate hours (and handle missing sign-outs):
= if [Sign-Out Time] = null then "Missing Sign-Out" else Duration.TotalHours([Sign-Out Time] - [Sign-In Time]) - If you want to format missing entries as
0instead of text, adjust the formula to:= if [Sign-Out Time] = null then 0 else Duration.TotalHours([Sign-Out Time] - [Sign-In Time])
Load back to Excel
- Click Close & Load to bring the merged, calculated table into a new worksheet. You can refresh this table anytime your Sign-In/Sign-Out data updates!
备选方案:优化后的工作表函数
If you prefer sticking to cell functions instead of Power Query, here’s how to refine your original INDEX/MATCH approach (with modern functions like XLOOKUP for simplicity):
Create a unique match key
- In the Sign-In sheet, add a helper column
UniqueKeywith this formula:= [@ID] & "|" & [@Date] - Repeat this for the Sign-Out sheet (same column name and formula)
- In the Sign-In sheet, add a helper column
Pull in matching sign-out times
- In the Sign-In sheet, add a
Sign-Out Timecolumn with XLOOKUP (works in Excel 365/2021):= XLOOKUP([@UniqueKey], Sign-Out[UniqueKey], Sign-Out[Time], "No Sign-Out", 0) - If you’re on an older Excel version, use INDEX/MATCH:
= IFERROR(INDEX(Sign-Out[Time], MATCH([@UniqueKey], Sign-Out[UniqueKey], 0)), "No Sign-Out")
- In the Sign-In sheet, add a
Calculate daily hours
- Add a
Hours Workedcolumn with this formula (converts Excel time difference from days to hours, and handles missing sign-outs):= IF([@[Sign-Out Time]] = "No Sign-Out", "Missing", ([@[Sign-Out Time]] - [@Time])*24)
- Add a
处理特殊情况
Don’t forget edge cases that can break your calculations:
- Multiple sign-ins/sign-outs in one day: Use
MINIFSto get the earliest sign-in andMAXIFSto get the latest sign-out:= MINIFS(Sign-In[Time], Sign-In[ID], [@ID], Sign-In[Date], [@Date]) = MAXIFS(Sign-Out[Time], Sign-Out[ID], [@ID], Sign-Out[Date], [@Date]) - Cross-day sign-outs: If an employee signs out the next day, adjust the hour calculation to account for the 24-hour wrap:
= IF([@[Sign-Out Time]] < [@Time], ([@[Sign-Out Time]] + 1 - [@Time])*24, ([@[Sign-Out Time]] - [@Time])*24)
内容的提问来源于stack exchange,提问作者Justin TriadTechx

