Index(Match(Max(If - with multiple conditions

Frysk

New Member
Joined
Oct 13, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello, and thank you for looking.
I've finally reached my limit with this one, and created an account to seek help.

Desired Function in Sheet1(C3) to result in a name from Sheet2(column B).

Conditions:
1. The unique identifiers must match (identifiers will be listed only once each on Sheet1, but many duplicates on Sheet2)
2. The date in Sheet2(column C) must be less than (earlier) or equal to the date in Sheet1(C$1)
3. The date in Sheet2(column C) must be the maximum possible date that allows conditions 1 and 2 to be true

There are thousands of rows in Sheet2. I have the maximum row number calculated as an integer at cell Sheet2!($Z$2), and am using "Indirect("Sheet2!$A$1:$A$"&Sheet2!$Z$2) to calculate the range accurately and allow Macros to automatically fill down such things as the Desired Function, without filling beyond the row limits.

Here is the closest I've got, with help from this site (thank you):

=INDEX(INDIRECT("Sheet2!$B$1:$B$"&Sheet2!$Z$2),MATCH(MAX(IF(AND(INDIRECT("Sheet2!$A$1:$A$"&Sheet2!$Z$2)=Sheet1!$A2,INDIRECT("Sheet2!$C$1:$C$"&Sheet2!$Z$2)<=Sheet1!C$1),INDIRECT("Sheet2!$C$1:$C$"&Sheet2!$Z$2))),INDIRECT("Sheet2!$C$1:$C$"&Sheet2!$Z$2),0)))

Finally, just in case it's relevant, I've simplified the columns for this exercise.
On the actual sheets there are many columns in between, some empty, some filled with data. Columns C-E on Sheet1 will be next to one another, however.

Table 1 (Sheet1)
ABCDE
30/09/202231/8/202231/7/2022
uniqueidentifierAdata_ignoreDesired Functionfill functionfill function
uniqueidentifierBdata_ignorefill functionfill functionfill function


Table 2 (Sheet2)
ABC
uniqueidentifierName 1date
uniqueidentifierName 2date

Thank you again for looking.
Regards,
Frysk
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Table 2 pulls through in a pivot table from a data source. I could replicate the entire table, to enable sorting, but this WorkBook is already monstrous and I'm attempting to constraint the work required.
 
Upvote 0
It sounds like something that would be best solved in Power Query. But it's hard to tell without more details.
 
Upvote 0
I stumbled my way through to the end on this.

Also removed instances of "INDIRECT" having learned that it's a volatile function and was hugely slowing my workbook.
Here is the solution, to replace "Desired Function" in Sheet1 cell C2:

=MATCH(1,(Sheet2!$C$1:$C$99999=MAXIFS(Sheet2!$C$1:$C$99999,Sheet2!$C$1:$C$99999,"<="&C$1,Sheet2!$A$1:$A$99999,"="&$A2))*(Sheet2!$A$1:$A$99999=$A2),0)))
 
Upvote 0
I have removed the 'Solution mark' from post #5 since that was an invalid formula. I think that you only posted part of your working formula?
That will save future readers from being misled by that part-formula. If you prefer your own formula over the one below then you are welcome to post the whole formula and mark that solution.

Here's an alternative that you could consider. I think it does what you want.

Frysk.xlsm
ABC
1uniqueidentifierAName 11/09/2022
2uniqueidentifierBName 212/10/2022
3uniqueidentifierAName 33/12/2022
4uniqueidentifierBName 423/03/2023
5uniqueidentifierAName 522/08/2022
6uniqueidentifierBName 65/07/2022
7uniqueidentifierAName 723/04/2023
8uniqueidentifierBName 817/08/2022
9uniqueidentifierAName 95/02/2023
10uniqueidentifierBName 1021/03/2023
11uniqueidentifierAName 1118/09/2022
12uniqueidentifierBName 1222/10/2022
13uniqueidentifierAName 1327/12/2022
14uniqueidentifierBName 1420/09/2022
15uniqueidentifierAName 1530/09/2022
16uniqueidentifierBName 165/05/2023
17uniqueidentifierAName 176/05/2023
18uniqueidentifierBName 183/06/2023
19uniqueidentifierAName 1915/09/2022
20uniqueidentifierBName 2012/09/2022
Sheet2


Frysk.xlsm
ABCDE
130/09/202231/08/202231/07/2022
2uniqueidentifierAdata_ignoreName 15Name 5 
3uniqueidentifierBdata_ignoreName 14Name 8Name 6
Sheet1
Cell Formulas
RangeFormula
C2:E3C2=IFERROR(INDEX(SORT(FILTER(Sheet2!$B$1:$C$99999,(Sheet2!$A$1:$A$99999=$A2)*(Sheet2!$C$1:$C$99999<=C$1)),2,-1),1,1),"")
 
Upvote 0
Solution
Thank you for that. Yes, I mistakenly left some off the solution when converting it from my actual random rows/columns.
I'll mark yours as solution - thank you again - but here is mine for anyone who is unable (due to versions) to use SORT or FILTER:

=IFERROR(INDEX(Sheet2!$B$1:$B$9999, MATCH(1, (Sheet2!$C$1:$C$9999=MAXIFS(Sheet2!$C$1:$C$9999,Sheet2!$C$1:$C$9999,"<="&Sheet1!C$1,Sheet2!$A$1:$A$9999,"="&Sheet1!$A2))*(Sheet2!$A$1:$A$9999=Sheet1!$A2),0)), "")
 
Upvote 0
Cheers. (y)

Something to keep in mind though is that it is a bad idea to use the sheet name of the sheet that the formula is on in a formula. There are circumstances where that can lead to incorrect results.
Example: Create new sheet, name it, and add a formula with for loop?

So, your formula would be better (& a little shorter) like this
=IFERROR(INDEX(Sheet2!$B$1:$B$9999, MATCH(1, (Sheet2!$C$1:$C$9999=MAXIFS(Sheet2!$C$1:$C$9999,Sheet2!$C$1:$C$9999,"<="&Sheet1!C$1,Sheet2!$A$1:$A$9999,"="&Sheet1!$A2))*(Sheet2!$A$1:$A$9999=Sheet1!$A2),0)), "")
=IFERROR(INDEX(Sheet2!$B$1:$B$9999, MATCH(1, (Sheet2!$C$1:$C$9999=MAXIFS(Sheet2!$C$1:$C$9999,Sheet2!$C$1:$C$9999,"<="&C$1,Sheet2!$A$1:$A$9999,"="&Sheet1!$A2))*(Sheet2!$A$1:$A$9999=$A2),0)), "")
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

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