May 28 2024 11:43 AM - edited Jun 05 2024 03:45 PM
I need an Excel formula to return a lookup value based on date. The lookup dates and values (exchange rates for each date) are arranged in two columns per year.
More detail in the spreadsheet attached below.
Can anyone help?
Thank you in advance!
May 28 2024 11:57 AM
May 28 2024 12:11 PM
That could be like
=LET(
FXrate, VSTACK(A2:B8, C2:D9),
DateInput, A19:A20,
XLOOKUP(DateInput, CHOOSECOLS( FXrate, 1), CHOOSECOLS( FXrate, 2) )
)
May 28 2024 12:12 PM
Solution
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)))
May 28 2024 12:30 PM
May 28 2024 12:52 PM
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)
)
Jun 05 2024 09:48 AM - edited Jun 06 2024 08:05 AM
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.
Jun 05 2024 09:51 AM - edited Jun 05 2024 03:18 PM
see comment in previous message
Jun 05 2024 10:43 AM
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)))
Jun 05 2024 03:04 PM - edited Jun 05 2024 03:10 PM
dscheikey -
1) You are right on, I mixed up the names everywhere (even for "Best Response")- so sorry ... I will try to rectify that!
2) You are right ... there are no columns for 2025 in the table... but I did that on purpose just to ensure I would get a "#N/A" value when appropriate.
I'll take a look at your "XLOOKUP() and OFFSET() instead of INDIRECT() and IF()" variation:
thanks again.
Jun 05 2024 05:52 PM
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?
Jun 06 2024 09:31 AM
Yes, that makes sense! If you copy the formula to another worksheet, it is best to do this via the edit bar. Then you have to adjust the worksheet names and cell C29 once. That was just an example. So adjust the C29 by the cell in which the formula is located. If you use it in C1, you must replace C29 with C1.
But the Indirect() also works.
May 28 2024 12:12 PM
Solution
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)))