Forum Discussion
Complex (for me!) lookup formula - help please....
- May 28, 2024
Please try this:
=LET(in,XMATCH(YEAR(A19),$1:$1),XLOOKUP(A19,OFFSET($A$1,1,in-1,365),OFFSET($A$1,1,in,365)))
Pretty much the same as SergeiBaklan except that I resurrected the old LOOKUP function
= LET(
FXrate, VSTACK(_2020, _2021),
LOOKUP(dateInput, FXrate)
)
If you do not want to return the n/a then either LOOKUP or XLOOKUP can return a prior available value
= LET(
FXrate, VSTACK(_2020, _2021),
rate, TAKE(FXrate,,-1),
date, TAKE(FXrate,,1)/ISNUMBER(rate),
LOOKUP(dateInput, date, rate)
)
Thanks for everybody's input.
After getting a better understanding of Sergei's and dscheikey's solutions, I used dscheikey's because it avoids the need to add references to specific XLOOKUP arrays for each year.
I added the use of INDIRECT to reference the DateInput cell (for me always 1 or 2 cells left of the formula) to reduce potential for mishaps when copy/pasting the formula. I also changed the name of dscheikey's "in" to "YEARCOL" for my easier understanding.
My inexperience with most of the functions used required quite a bit of digging to understand the suggested solutions, so I documented Sergie's, dscheikey's and my formulas just so I won't be totally confused when I see these formulas a year from now - so I share my pedantic formula explanations (attached "...FINAL.xslx") in case other equally inexperienced users might find this type of formula useful.
BTW Peter - I decided "#N/A" was an adequate result if something when goes wrong when using my "final" formula. Thanks anyway.
- Mike_OBreeJun 05, 2024Copper Contributor
see comment in previous message
- dscheikeyJun 05, 2024Bronze Contributor
Hi Mike, the only two mistakes I can recognize are
1) that you have mixed up the names. The solution with offset where you don't have to name the arrays separately is mine.
2) Also, there are no columns for 2025 in the 'fx rates 2002 to date' table.
I would probably have used XLOOKUP() and OFFSET() instead of INDIRECT() and IF():C29 = LET(InputDate,XLOOKUP(TRUE,OFFSET(C29,0,-2,1,2)>0,OFFSET(C29,0,-2,1,2)),YEARCOL,XMATCH(YEAR(InputDate),'fx rates 2002 to date'!$2:$2),XLOOKUP(InputDate,OFFSET('fx rates 2002 to date'!$A$2,1,YEARCOL-1,365),OFFSET('fx rates 2002 to date'!$A$2,1,YEARCOL,365)))
- Mike_OBreeJun 06, 2024Copper Contributor
dscheikey -
Very slick use of XLOOKUP and OFFSET to reference the InputDate Cell !
However, if you copy/paste the Cell with the formula included in it to another excel file, although the "C29" reference will adjust accordingly, the sheet name 'fx rates 2002 to date will now have the originating file name appended to it.
On the other hand, if you copy the formula from the cell edit window, the paste correctly keeps just the 'fx rates 2002 to date' sheet name (without the wrong file name), but then the 'C29' will not adjust appropriately.
My version using "INDIRECT" did not suffer from that problem.
Does this make any sense?