# Find the last date/time with criteria

#### sharky12345

##### Well-known Member
I'm trying to find a way I can identify the last date and time in a range but with the condition that it does not fall after another date/time.

So range A2:A1000 has a serial number, range B2:B1000 has a date and time stamp - there will be duplicates of the serial number but each time stamp is unique. On another sheet I have the serial numbers in range C2:C1000 and further date/time stamps in range D2:D1000, I have other data in the range E2:1000.

What I need to do is lookup the value in column A of the first sheet, find it in column C of the 2nd sheet, then go through column D of the 2nd sheet, (where the value in A matches), and find the last date/time without going past the original time in column B of the first sheet, then give me the value from column E on the 2nd.

I've tried to explain as best I can, I know it sound really complicate but maybe this will assist;

So this is sheet 1:

Test workbook.xlsm
AB
1Serial NumberSign Date
2CR200002063401/10/2020 01:22
3CR200002063401/10/2020 01:50
4CR200002063501/10/2020 01:23
5CR200002063501/10/2020 01:51
6CR200002063501/10/2020 02:20
7CR200002063501/10/2020 02:49
8CR200002063501/10/2020 03:13
9CR200002063501/10/2020 03:44
10CR200002063501/10/2020 04:10
Sheet1

And this is sheet 2:

Test workbook.xlsm
CDE
1SerialTimestampValue to return
2CR200002063401/10/2020 01:2015
3CR200002063401/10/2020 01:4930
4CR200002063501/10/2020 01:2160
5CR200002063501/10/2020 01:4515
6CR200002063501/10/2020 02:1030
7CR200002063501/10/2020 02:3915
8CR200002063501/10/2020 03:0360
9CR200002063501/10/2020 03:4360
10CR200002063501/10/2020 04:0930
Sheet2

So if I looked up the time stamp in B2 on sheet 1, the value I'd expect to return is 15, which is the value from E2 on sheet 2. If I looked up the time stamp in B6 on sheet 1 I'd expect the return value to be 30, from E6 on the 2nd sheet.

Does my explanation make sense or have I confused the hell out of everyone?

#### sandy666

##### Banned - Rules violations
you mean like this?
 Serial Last V2R CR2000020634 01/10/2020 01:22:59 15 CR2000020635 01/10/2020 01:23:39 60

### Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

#### sharky12345

##### Well-known Member
There is no date/time in sheet2 less than the date/time in sheet1 for that serial number.
That's odd because there is.....I'll double check the cell formatting to see if that makes a difference

#### sharky12345

##### Well-known Member
you mean like this?
 Serial Last V2R CR2000020634 01/10/2020 01:22:59 15 CR2000020635 01/10/2020 01:23:39 60
Yes that's it - sorry for any confusion!

#### sandy666

##### Banned - Rules violations
Yes that's it - sorry for any confusion!
here is M for this
Power Query:
``````// Query1
let
Tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
Join = Table.NestedJoin(Tbl2,{"Serial"},Tbl1,{"Serial Number"},"Table",JoinKind.LeftOuter),
Expand = Table.ExpandTableColumn(Join, "Table", {"Serial Number", "Sign Date"}, {"Serial Number", "Sign Date"}),
Group = Table.Group(Expand, {"Serial"}, {{"Last", each List.Min([Sign Date]), type datetime}, {"Count", each _, type table}}),
DTT = Table.TransformColumnTypes(Group,{{"Last", type datetime}}),
V2R = Table.AddColumn(DTT, "V2R", each List.First(List.Distinct([Count][Value to return])))
in
V2R``````

#### sharky12345

##### Well-known Member

here is M for this
Power Query:
``````// Query1
let
Tbl1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Tbl2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
Join = Table.NestedJoin(Tbl2,{"Serial"},Tbl1,{"Serial Number"},"Table",JoinKind.LeftOuter),
Expand = Table.ExpandTableColumn(Join, "Table", {"Serial Number", "Sign Date"}, {"Serial Number", "Sign Date"}),
Group = Table.Group(Expand, {"Serial"}, {{"Last", each List.Min([Sign Date]), type datetime}, {"Count", each _, type table}}),
DTT = Table.TransformColumnTypes(Group,{{"Last", type datetime}}),
V2R = Table.AddColumn(DTT, "V2R", each List.First(List.Distinct([Count][Value to return])))
in
V2R``````
Sorry but that makes no sense to me - I don't know what it is.

#### sandy666

##### Banned - Rules violations
this is Power Query (Get&Transform)
You've XL2016 so Power Query is built-in

#### sharky12345

##### Well-known Member

It does
+Fluff v2.xlsm
ABCDE
1SerialTimestampValue to return
2CR200002063401/10/2020 01:2015
3CR200002063401/10/2020 01:4930
4CR200002063501/10/2020 01:2160
5CR200002063501/10/2020 01:4515
6CR200002063501/10/2020 02:1030
7CR200002063501/10/2020 02:3915
8CR200002063501/10/2020 03:0360
9CR200002063501/10/2020 03:4360
10CR200002063501/10/2020 04:0930
11
Sheet2

+Fluff v2.xlsm
ABC
1Serial NumberSign Date
2CR200002063401/10/2020 01:2215
3CR200002063401/10/2020 01:5030
4CR200002063501/10/2020 01:2360
5CR200002063501/10/2020 01:5115
6CR200002063501/10/2020 02:2030
7CR200002063501/10/2020 02:4915
8CR200002063501/10/2020 03:1360
9CR200002063501/10/2020 03:4460
10CR200002063501/10/2020 04:1030
Sheet1
Cell Formulas
RangeFormula
C2:C10C2=INDEX(Sheet2!\$E\$2:\$E\$10,AGGREGATE(14,6,(ROW(Sheet2!\$D\$2:\$D\$10)-ROW(Sheet2!\$D\$2)+1)/(Sheet2!\$C\$2:\$C\$10=A2)/(Sheet2!\$D\$2:\$D\$10<B2),1))
Fluff I've found the issue but can't work out how to quickly resolve it.

The date and time values in sheet 2 are in this format: dd/mm/yyyy hh:mm (General) - if I click in the cell and press the enter key it adds the seconds too in the formula bar and the format changes to a correct date/time format, then the formula works. So is there a quick way I can convert every cell without clicking into each one and pressing enter?

#### Fluff

##### MrExcel MVP, Moderator
It sounds like they are text & not real dates, you can use Text to columns on the Data tab.
Select the column & in text to columns select delimited, next, clear all check boxes, next, select DMY in the date dropdown, Finish.

#### sharky12345

##### Well-known Member
It sounds like they are text & not real dates, you can use Text to columns on the Data tab.
Select the column & in text to columns select delimited, next, clear all check boxes, next, select DMY in the date dropdown, Finish.
That's done it!

Thank you again Fluff.

#### Fluff

##### MrExcel MVP, Moderator
You're welcome & thanks for the feedback.

Replies
1
Views
108
Replies
33
Views
681
Replies
2
Views
52
Replies
6
Views
193
Replies
10
Views
374

1,126,999
Messages
5,622,115
Members
415,878
Latest member
jjj12345

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back