Conditional lookup based on two variables, with approximation

Tacos_Tacos_Tacos

New Member
Joined
Mar 17, 2015
Messages
13
Hello. This one's a doozy. I've had little luck finding answers elsewhere, so perhaps someone here can help.

I have two sheets - Sheet1 and Sheet2. On Sheet1, I have four columns: NAME, CHECK DATE, ELECTION TYPE, and ELECTION AMT. The first two columns are populated, and the latter two are not. The first column will contain names three rows (not merged) at a time. For example, "John Smith" will appear three times in three rows, each with a different date in the DATE column. Our goal is to populate the ELECTION TYPE and ELECTION AMT columns by referencing the data from the first two to find the appropriate data on Sheet2.

Sheet1:

NameCheck DateElection TypeElection Amt.
Phil10/31/14??
Phil11/14/14
Phil11/28/14
Elizabeth10/31/14
Elizabeth11/14/14
Elizabeth11/28/14

<tbody>
</tbody>


On Sheet2 I have four columns: NAME, ELECTION TYPE, ELECTION AMT, and EFFECTIVE DATE. Each name may appear any number of times, with varying data under ELECTION TYPE and ELECTION AMT. This is the data we want to pull to Sheet1.

Sheet2:

NameElection TypeElection AmountEffective Date
PhilPre-tax14%9/13/14
PhilPre-tax17%9/14/14
PhilRoth20%10/28/14
Phil
Roth15%11/7/14
PhilPre-tax10%11/10/14
PhilRoth14%11/21/14
PhilPre-tax12%11/28/14
ElizabethRoth9%10/16/14
ElizabethRoth14%10/31/14
ElizabethPre-tax10%11/1/14
ElizabethPre-tax15%11/2/14
ElizabethPre-tax5%11/26/14
ElizabethPre-tax8%12/15/14

<tbody>
</tbody>

I need to develop a formula for use in Sheet1 that will allow me to look up the corresponding name on Sheet2, then, referencing CHECK DATE on Sheet1, find the EFFECTIVE DATE on Sheet2 that is closest without going over. So, for example, if we are referencing the first row on Sheet1, we would look on Sheet2 for the rows that have "Phil" in the first column, and then the date closest to 10/31/14 that does not exceed that date. Here, it would be "10/28/14." Finally, I want to populate the latter two columns of Sheet1 with the corresponding data from the appropriate row on Sheet2 ("Roth" and "20%" in this example).

I suspect this will require an array function, and I am not as experienced with that as I'd like. We're dealing with about 100,000 rows here, so any help or insight would be immensely appreciated. I've shopped this around other forums, but have found no good solution yet. Thank you in advance for your time and effort.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
A:D of Sheet2 houses the data (not shown here, see above).

Sheet1

Row\Col
A​
B​
C​
D​
E​
1​
NameCheck DateElection TypeElection Amt.Idx
2​
Phil
10/31/2014
Roth
0.15​
4
3​
Phil
11/14/2014
Roth
0.14​
6
4​
Phil
11/28/2014
Pre-tax
0.12​
7
5​
Elizabeth
10/31/2014
Roth
0.14​
9
6​
Elizabeth
11/14/2014
Pre-tax
0.05​
12
7​
Elizabeth
11/28/2014
Pre-tax
0.08​
13

C2, copied to D2 and down:
Rich (BB code):

=IF(ISNUMBER($E2),INDEX(Sheet2!B$2:B$14,$E2),"")

E2, control+shift+enter, not just enter, and copy down:
Rich (BB code):

=MATCH(1,IF(Sheet2!$A$2:$A$14=$A2,IF($B2<=Sheet2!$D$2:$D$14,1)),0)
 
Upvote 0
Thank you from the bottom of my soul for taking a look at this.

Unfortunately, these formulas aren't quite pulling the correct data. The table ​should look as follows:

NameCheck DateElection TypeElection Amt.
Phil10/31/14Roth20%
Phil11/14/14Pre-tax10%
Phil11/28/14Pre-tax12%
Elizabeth10/31/14Roth14%
Elizabeth11/14/14Pre-tax15%
Elizabeth11/28/14Pre-tax5%

<tbody>
</tbody>

It appears the dates being pulled aren't the ones we're looking for. To be clear, I need the formula to reference the CHECK DATE and then find the row with the closest EFFECTIVE DATE that does not exceed the CHECK DATE. For Phil's second row (11/14/14), for example, it should pull data from Phil's fifth row on Sheet2 (11/10/14), as 11/10/14 is the closest date to 11/14/14 that does not go beyond it.

I really appreciate your help.
 
Upvote 0
Thank you from the bottom of my soul for taking a look at this.

Unfortunately, these formulas aren't quite pulling the correct data. The table ​should look as follows:

NameCheck DateElection TypeElection Amt.
Phil10/31/14Roth20%
Phil11/14/14Pre-tax10%
Phil11/28/14Pre-tax12%
Elizabeth10/31/14Roth14%
Elizabeth11/14/14Pre-tax15%
Elizabeth11/28/14Pre-tax5%

<tbody>
</tbody>

It appears the dates being pulled aren't the ones we're looking for. To be clear, I need the formula to reference the CHECK DATE and then find the row with the closest EFFECTIVE DATE that does not exceed the CHECK DATE. For Phil's second row (11/14/14), for example, it should pull data from Phil's fifth row on Sheet2 (11/10/14), as 11/10/14 is the closest date to 11/14/14 that does not go beyond it.

I really appreciate your help.

Modify the formula in E2...

=MATCH($B2,IF(Sheet2!$A$2:$A$14=$A2,Sheet2!$D$2:$D$14),1)

which must be confirmed with control+shift+enter, not just enter, and copied down.
 
Upvote 0
Right about the time you posted I managed to put the following together, which I believe is working correctly:

=MATCH(1,IF(Sheet2!$A$2:$A$14=$A2,IF($B2>=Sheet2!$D$2:$D$14,1)),1)

Again, thank you very much for your help.

If anyone has the time and inclination, I am very curious how the array (ctrl+shift+enter) function is working here. Any kind of explanation would be quite useful.
 
Upvote 0
Modify the formula in E2...

=MATCH($B2,IF(Sheet2!$A$2:$A$14=$A2,Sheet2!$D$2:$D$14),1)

which must be confirmed with control+shift+enter, not just enter, and copied down.

Right about the time you posted I managed to put the following together, which I believe is working correctly:

=MATCH(1,IF(Sheet2!$A$2:$A$14=$A2,IF($B2>=Sheet2!$D$2:$D$14,1)),1)

Again, thank you very much for your help.

If anyone has the time and inclination, I am very curious how the array (ctrl+shift+enter) function is working here. Any kind of explanation would be quite useful.

In:

=MATCH($B2,IF(Sheet2!$A$2:$A$14=$A2,Sheet2!$D$2:$D$14),1)

the IF bit collects all dates from the records where the name range = Phil...

{41895;41896;41940;41950;41953;41964;41971;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE}

FALSE obtains when Fhil not hold.

The MATCH bit matches the criterion/condition date against the foregoing ascending order set of dates with match-type set to 1 (TRUE). The successful match results in a relative rown number which INDEX uses for retrieval.

The version with 1 as look up value creates an array of 1 and FALSE values against which an approximate match is done as above.
 
Last edited:
Upvote 0
This is great information. Thanks.

A follow-up: would it be possible to use embedded MATCH functions to increase the number of variables with which we look up our data?
 
Upvote 0

Forum statistics

Threads
1,215,494
Messages
6,125,137
Members
449,207
Latest member
VictorSiwiide

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