excelexcel-formula

Formula to Return the Most Recent Entry where Price was Lower than the Given Entry and Include = Y


Looking for the most efficient formula to return the most recent entry (by the Date field) where Price was < the given entry's Price and Include = Y.

I think I could use some combination of INDEX MATCH and MINIFS but I'm stumped at how to incorporate the most recent date piece. Values in yellow are what I expect the formula to return.

Update

The dates may not always be in descending order - most of the time they will be but not all the time.

This gets me the closest but it doesn't work if dates are NOT in descending order:

=IF(C2="Y",IFERROR(INDEX(A3:A68,AGGREGATE(15,6,(ROW(A3:A68)-ROW(A2))/(C3:C66="y")/(D3:D68<D2),1)),"Lowest to Date"),"N/A")

enter image description here


Solution

  • Because the dates are in descending order, I don't feel that you have to include them in the calculation. Assuming that the word "Entry" is in cell A1 the following could be pasted into cell E2 and copied down: =IFERROR(INDEX(A3:A101,AGGREGATE(15,6,(ROW(A3:A101)-ROW(A2))/(C3:C21="y")/(D3:D101<D2),1)),"")

    As the date may not always be in descending order try:

    =IFERROR(INDEX($A$2:$A$34,AGGREGATE(15,6,
     (ROW($A$2:$A$34)-ROW($A$2)+1)/($C$2:$C$34="Y")/($D$2:$D$34<D2)/
    ($B$2:$B$34=AGGREGATE(14,6,$B$2:$B$34/($C$2:$C$34="Y")/($D$2:$D$34<D2),1)),1)),"")
    

    As the request now includes displaying "N/A" in rows corresponding to cells in column C not containing "Y":

    =IF(C2="y",IFERROR(INDEX(A3:A$100,AGGREGATE(15,6,(ROW(A3:A$100)-ROW(A2))/(C3:C$100="y")/(D3:D$100<D2)/(B3:B$100=AGGREGATE(14,6,B3:B$100/(C3:C$100="y")/(D3:D$100<D2),1)),1)),""),"N/A")

    Change 100 the largest row number in the range.

    Taking another stab at this, incorporating the suggestion to lock row references:

    =IF(C2="Y",IFERROR(INDEX(A$2:A$100,XMATCH(AGGREGATE(14,6,B$2:B$100/(C$2:C$100="Y")/(D$2:D$100<D2)/(B$2:B$100<B2),1),B$2:B$100)),""),"N/A")