Home > Net >  Return Value for Most Recent Date Based on Condition in Excel
Return Value for Most Recent Date Based on Condition in Excel

Time:09-18

I am looking to return the value for the most recent/latest date for each type (Type A, Type B). I have tried several formulas and combination of formulas.

Formulas I have tried (Using Type A as an example)

  • DMAX : =IF(A2="Type A",DMAX(A:C,"Value",B:B),0) returns 814
  • VLOOKUP : =IF(A2="Type A", VLOOKUP(MAX(B:B),C:C,1,0),0) returns #N/A
  • MAX IF : {=MAX(IF(A:A="Type A",C:C,""))} returns 703

The values that should be returned are:

  • Type A: 219, the value for 01/05/21 which is the most recent date
  • Type B: 505, the value for 01/05/21 which is the most recent date

This is the data I'm working with:

A B C
1 Type Date Value
2 Type A 01/01/21 29
3 Type A 01/02/21 703
4 Type A 01/03/21 280
5 Type A 01/04/21 154
6 Type A 01/05/21 219
7 Type B 01/01/21 814
8 Type B 01/02/21 638
9 Type B 01/03/21 811
10 Type B 01/04/21 292
11 Type B 01/05/21 505

Dates are in U.S. format MM/DD/YY for reference.

Thank you

CodePudding user response:

If you have the newest version of Excel you can use FILTER. First get the unique Types with =UNIQUE($A$2:$A$11), then use something like:

=FILTER($C$2:$C$11,
(($B$2:$B$11=MAX(FILTER($B$2:$B$11,($A$2:$A$11=E1))))*
($A$2:$A$11=E1)))

For example: enter image description here

CodePudding user response:

I'm sure there are other ways, but I managed to get a similar result by splitting up my formula a little (Note, I randomized my dates some to provide a better test).

I have a column for each type, and one that finds the max date. Then I use a SUMPRODUCT formula for the final look up. This is what it looks like.

Type Date (Formula) Date (Result)
Type A =MAXIFS(B1:B10,A1:A10,E9,B1:B10,"<"&DATE(2021,9,15)) 01/30/21
Type B =MAXIFS(B2:B11,A2:A11,E10,B2:B11,"<"&DATE(2021,9,15)) 01/31/21

And then the final result

Value (Result) Value (Formula)
703 =SUMPRODUCT((A2:A11=E9)*(B2:B11=F9)*(C2:C11))
811 =SUMPRODUCT((A2:A11=E10)*(B2:B11=F10)*(C2:C11))

It's still not perfect but it works using reasonably standard formulas and doesn't rely on anything specific to Excel (which is useful for me).

So my spreadsheet looks like this: enter image description here

  • Related