Excel: Find the earliest/latest values for each date in a list of dates & times
I have list of sequential arrival & departure times like this:
| IN/OUT | DATE/TIME |
|--------|-----------------------|
| IN | 1/2/2019 8:25:59 AM |
| OUT | 1/2/2019 11:51:59 AM |
| IN | 1/2/2019 12:48:59 PM |
| OUT | 1/2/2019 4:45:59 PM |
| IN | 1/3/2019 9:08:59 AM |
| OUT | 1/3/2019 12:38:59 PM |
| IN | 1/3/2019 3:23:59 PM |
| OUT | 1/3/2019 4:59:59 PM |
| IN | 1/4/2019 8:49:59 AM |
| OUT | 1/4/2019 5:11:59 PM |
From this list I need to extract the first and last entries for each day (which will then be used to calculate average arrival and departure times, duration, etc).
For example, from the above table I'd want to extract the following:
| IN/OUT | DATE/TIME |
|--------|-----------------------|
| IN | 1/2/2019 8:25:59 AM |
| OUT | 1/2/2019 4:45:59 PM |
| IN | 1/3/2019 9:08:59 AM |
| OUT | 1/3/2019 4:59:59 PM |
| IN | 1/4/2019 8:49:59 AM |
| OUT | 1/4/2019 5:11:59 PM |
I know I can use a VLOOKUP to get all the 'IN' or 'OUT' values, but the problem is that most days in the list have more than one. I need only the first and the last, but I'm not sure how to do that.
Is there a way I can accomplish this using VLOOKUP, with or without a helper table? Or perhaps another method might work better?
1. Remember to search before asking
Using MAX() in Excel against a list of dates using multiple criteria
See under 'Related'?
2. MIN
/MAX
CSE recipe
MIN
for 'IN':
=MIN(IF($A$2:$A$99=F2,IF($B$2:$B$99=G2,$C$2:$C$99)))
This is what they call an array or 'CSE' formula - enter it in the formula bar then press Ctrl Shift Enter to enter it as an array formula, then copy down.
This makes intuitive sense, right? Get the lowest value from C
where F
matches A
and G
matches B
.
That only gets your minimum IN values, though... swapping MAX
for MIN
in every other row gets the maximum OUT values... But who has time for that? Let's smash them together with a simple IF
:
3. FINAL FORMULA
=IF(F2="IN",MIN(IF($A$2:$A$99=F2,IF($B$2:$B$99=G2,$C$2:$C$99))),MAX(IF($A$2:$A$99=F2,IF($B$2:$B$99=G2,$C$2:$C$99))))
Again, enter that formula in F2, press Ctrl Shift Enter and then copy down.
NB:
- Adjust cell references as necessary
- You didn't explicitly say much about the shape of your data inputs; I separated each argument using Excel's Text to Columns so I could operate against dates and times independently.
- There is NO error catching included and the solution assumes you already have your desired test values set out as shown. This should be easy to copy your input IN/OUT and DATE columns and use 'Remove Duplicates' to generate your unique list to match.
- There's no special formatting on what I have in column
C
. It's simple text as shown. You probably will have to use the Format Painter (from that column) on your final output column! - Ignore my column
D
which I used as simple error checking while putting this together and forgot to delete before taking screenshots.