Return Last But One Value from a range with specific conditions

deLockloire

Board Regular
Joined
Apr 4, 2008
Messages
110
Office Version
  1. 365
Platform
  1. Windows
Hey,

I have a table named ECB that contains exchange rate listings for a series of dates. Something like this:

DATESUSDGBPSEKTRY
2024.01.28X
2024.01.29
2024.01.31
2024.02.01
2024.02.03Y
2024.02.04
2024.03.01
2024.03.02


Of course, it is filled up with values. In another table (called Transactions), there are dates, values, currencies, etc. of transactions. Something like this:


DATEAMOUNTCURRENCYEXCHANGE RATE
2024.02.2826,000USDX
2024.03.0219,000GBPY


What I need is a formula for the EXCHANGE RATE column. What it needs to do is to pull in the second to last (i.e., last but one) listing from the previous month of the DATE of the current transaction for the currency indicated in the CURRENCY column exchange rate from the ECB table. As marked with X and Y. For example, for each transaction that takes place in march, the ECB exchange rate of 2024.02.03 should be used, because that is the last but one listing (the last being 2024.02.04). Similarly, for each transaction in February, the 2024.01.29 listing should be used.

Now I do have a solution, but it feels way too complicated and I was just wondering if there is a simpler way of doing it. My solution is this (using it from the Exchange Rate column of the Transactions Table):

VBA Code:
=XLOOKUP(MAX(FILTER(ECB[Date],(ECB[Date] >= DATE(YEAR([@Date]),MONTH([@Date])-1,1)) * (ECB[Date] <= EOMONTH(DATE(YEAR([@Date]),MONTH([@Date])-1,1),0))))-1,ECB[Date],INDIRECT("ECB["&[@Currency]&"]"),,-1)

Basically with a FILTER function I create an array of dates from the ECB table that corresponds to the previous month, and get the latest date by putting it through a MAX formula, then I subtract 1 from it , so the result will be one day smaller than the latest day, and this is what I put as a search value in an XLOOKUP, using a -1 match mode and then return the corresponding exchange rate by putting the return array together with an INDIRECT that uses the CURRENCY values to indicate the column.

So I might be wrong here, but there surely must be a simpler way of doing this. :) Any ideas?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
Excel Formula:
=CHOOSEROWS(FILTER(XLOOKUP([@CURRENCY],ECB[[#Headers],[USD]:[TRY]],ECB[[USD]:[TRY]]),ECB[DATE]<=EOMONTH([@DATE],-1)),-2)
 
Upvote 0
Solution
Hey Fluff, I like your solution. :) I have never used CHOOSEROWS so far, so I think I'm going to go with this hoping it will stick. Thanks! Your formula also made me realize that I had a couple of unnecessary calculations in my solution, and could be simplified to this:

VBA Code:
=XLOOKUP(MAX(FILTER(ECB[Date];(ECB[Date] <= EOMONTH([@Date];-1))))-1;ECB[Date];INDIRECT("ECB["&[@Currency]&"]");;-1)
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top