Solved

# Formula in Excel to display the date for the last occurrence of a value in a table

Posted on 2014-08-31
146 Views
Hello,

In a table with chronologically sorted values, what Excel formula will display the date corresponding to the last time a given value appears in the table?

For example, suppose you have a spreadsheet with values in the range A1:B12 as in the following screenshot (Fig. 1):

Also, suppose a user-entry box is placed at cell B15 (Fig. 2).

What formula in cell C15 will return the date from the table corresponding to the last occurrence of the user entry?

Note that in the above example, the user entry "EAT" occurs in the table only once (06/06/14). However, suppose the user value occurs more than once in the table, as in the following screenshot (Fig. 3).

Here, the entry "DEW" occurs twice (01/25/14 & 07/11/14) but according to the initial question, the formula in C15  should always display the most recent date, in this case 07/11/14.

What formula would accomplish that?

Thanks
0

LVL 22

Accepted Solution

See if this gives you what you want:

=LOOKUP(2,1/(B1:B12=B15),A1:A12)

Flyster
0

LVL 47

Expert Comment

Hi

as an array formula
Enter with Ctrl-Shift-Enter

=MAX((B1:B12=B15)*A1:A12)

Regards
0

LVL 31

Expert Comment

Assuming your real file will also have headers, you can use the DMAX function.

Above your data range put headers Date & Entry (or something more relevant).

Above your User Entry and Date formula put the same headers, B15 & C15 in the example when an extra row has been inserted to accommodate the headers and then use this formula:

=DMAX(\$A\$1:\$B\$13,\$C\$15,\$B\$15:\$B\$16)

Thanks
Rob H
0

LVL 7

Expert Comment

Could you just sort the date column descending and use a simple vlookup?  Wouldn't that pick up the last date for the user?
0

Author Comment

What a great thread — and what a great testimonial to the value of EE! I just got three different effective & accurate solutions, each using a different Excel function. This forum is the best resource I can imagine. Thanks a bunch for the responses!

=LOOKUP(2,1/(B1:B12=B15),A1:A12)

is the easiest to use but the most difficult for me to understand.

I have seen (and I now use) a number of solutions — usually from Barry Houdini — which include a reciprocal argument and an equation as a single term. However, I don't understand their function or how they work.

As a result, I will shortly be opening a couple of new threads to see if I can obtain explanations. The titles are as follows:

• Range of cells equal to a single value in Excel functions
• Using an equation as a term in Excel functions
• Reciprocal arguments in Excel functions

Thanks
0

LVL 22

Expert Comment

Although rarely used, Lookup does have it's advantages over vlookup or hlookup. One of the requirements to use lookup is to have your data in ascending order, which you have. By dividing your array by 1 ("1/(B1:B12=B15)"), you end up with a result like this: {#DIV/0!;1,#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!;1;#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!;#DIV/0!;} If it doesn't find the value (2) or any greater value in the range, it will return the last numerical value. Hope this makes sense!
0

Author Comment

0

Author Comment

tomfarrar
Could you just sort the date column descending and use a simple vlookup?  Wouldn't that pick up the last date for the user?
Tom, thanks for the response.

Yes. Your solution would work perfectly well for the example I included. However, when an example such as mine is used in a question, I think it is implied* that the best solution is one that functions correctly:

• for a list (database) of any size and,
• for a list that may be dynamic (ie getting additional entries or deletions of entries), and
• with as little manipulation as possible.

Therefore, although your solution would display the correct solution for a list of any size, the list would continually require resorting (manipulation) any time anything changes — which in many databases you may not be aware of.

*Someone please correct me if that statement is wrong.
0

LVL 7

Expert Comment

Sure.  Makes sense.  Whether sorting ascending or descending it requires additional steps.
0

Author Closing Comment

Thanks!
0

## Featured Post

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.