Excel find last match from two tables - excel

I'm looking for a way to find the last instance on an entry in the columns A,B and get the corresponding values from columns C,D
In the example below value for Henry is 1374 and value for Amy is 1124
Name1 corresponds to Value1 and Name2 corresponds to Value2.
Is there a formula to find the last entry from both columns Name1 and Name2 and return the corresponding Value1 or Value2
Raw data pasted below:
Name1 Name2 Value1 Value2
Sara Amy 1265 1241
John Sara 1142 1214
Amy Henry 1295 1121
Amy John 1175 1323
Sara John 1085 1251
Sara Henry 1242 1374
Amy Sara 1124 1055

Slightly shorter:
= INDEX($C$1:$D$8,MAX(IF($A$1:$B$8=A10,ROW($A$1:$B$8))),MATCH(A10,
INDEX($A$1:$B$8,MAX(IF($A$1:$B$8=A10,ROW($A$1:$B$8))),0),0))
Note this is an array formula, so you must press Ctrl+Shift+Enter rather than just Enter after typing the formula.
See below for working example.

Assumptions:
Data Grid is in cells A1:D8.
Values "Henry" & "Amy" are in cell A10 & A11 respectively.
Formula implementation:
In cell B10 following formula is implemented.
Alternative 1:
=INDEX($C$2:$D$8,MAX(IFERROR(LOOKUP(2,1/($A$2:$A$8=A10),ROW($A$2:$A$8)),-1),IFERROR(LOOKUP(2,1/($B$2:$B$8=A10),ROW($B$2:$B$8)),-1))-1,IF(IFERROR(LOOKUP(2,1/($A$2:$A$8=A10),ROW($A$2:$A$8)),-1)>IFERROR(LOOKUP(2,1/($B$2:$B$8=A10),ROW($B$2:$B$8)),-1),1,2))
Alternative 2 (slightly shorter than 1):
=INDEX($C$2:$D$8,LOOKUP(2,1/SEARCH(A10&",",$A$2:$A$8&","&$B$2:$B$8&",",1),ROW($A$2:$A$8))-1,IF(IFERROR(LOOKUP(2,1/($A$2:$A$8=A10),ROW($A$2:$A$8)),-1)>IFERROR(LOOKUP(2,1/($B$2:$B$8=A10),ROW($B$2:$B$8)),-1),1,2)).
To be copied down as much needed.
Notice -1 value after MAX() function which is used to adjust row number. It should be always n-1 considering data starts at nth row.

I've been trying to remember another method I've seen for 2d lookup (I can't find the link any more). It's basically like this
=INDIRECT(TEXT(MAX((ROW($A$2:$B$8)*100+COLUMN($A$2:$B$8))*($A$2:$B$8=A10))+2,"R0C00"),FALSE)
entered as an array formula using CtrlShiftEnter.
So the idea is that you generate a number from the row and column where the last occurrence of the name is located (so for Henry it would be 702).
The you format it to give R7C02 and feed this in to an indirect to give the reference to the cell in RC notation. The column plus 2 gives the cell that you want.
You might notice that this would fail if column>99, but you can make the multiplier as big as you want.

Related

Get multiple values in a single cell with array formula

I'm trying to get an array formula to get the multiple results in a single cell. Is that possible?
For example below, I'd like to show in D2 all the names in column B corresponding to rows for values less than 4 in column A.
My current attempt below:
A C
2 Jane
3 John
6 Thomas
1 Michael
2 Mary
7 Jason
3 Gloria
1 Andrea
=CONCAT(INDEX($B$2:$B$9,IF($A$2:$A$9<4,$B$2:$B$9)))
My desired result would be:
Jane, Michael, Mary, Andrea
You need FILTER() then TEXTJOIN().
=TEXTJOIN(", ",TRUE,FILTER(B2:B9,A2:A9<4))

How to extract unique values from one column based on criteria from two other columns, with or statement?

My data set looks something like this:
ID Name1 Name2
1 Jack Tom
1 Tom Tom
1 Lisa Tom
2 Tom
2 Tom
3 Frank Frank
3 John Frank
3 Frank Frank
3 John Frank
4 Tom
4 Tom
5 Lisa
5 Jack
and I want the following output:
Result
1
2
4
Note: I want the unique IDs for Tom if "Tom" shows in one of the two name columns.
I tried to use the following formula:
IFERROR(INDEX(INDIRECT($B$14); MATCH(0; IF($B$10=INDIRECT($B$16); IF($B$10=INDIRECT($B$15); COUNTIF($E$27:E27; INDIRECT($B$14)); "")); 0));"")
The problem is that this only gives me ID nr 1 as output since Tom shows up in both columns in this case. I think I need to implement an OR-statement to the formula.
Explanation of my formula:
Indirect(B14): array for the call IDs. B14 contains a name of this array.
B10: Contains the name I want to match (i.e. "Tom")
Indirect(B16): column Name1
Indirect(B15): column Name2
Good answers will be rewarded:)
I used your formula (without INDIRECT statements) and added ISNUMBER & FIND in order to find "Tom" in a combination of columns B and C:
This is an array formula (Ctrl+Shift+Enter):
=IFERROR(INDEX($A$1:$A$14,MATCH(0,COUNTIF($F$1:F1,IF(ISNUMBER(FIND("Tom",$B$1:$B$14&$C$1:$C$14)),$A$1:$A$14,"")),0)),"")
Result:
I couldn't use INDIRECT references as I'm not sure what exactly they point to (i.e. what are the ranges & column names). I hope it won't be too difficult for you to modify my formula in order to match your references.
Hope it helps! Cheers.

Vlookup from another sheet using multiple columns

I have an excel workbook with 2 sheets.
Sheet 4 and Sheet 5:
Sheet 4 has the following columns:
type model name year
U acura jane 1998
D honda peter 2002
U bmz fred 1993
Sheet 5 also has the same columns but with an additional column sales.
type model name sales
U acura jane 2.3
D honda peter 3.8
U bmz fred 19
IN both the sheets, I created an additional column called "key" concatenating type-model-name (A2&B2&C2)
type model name year key
U acura jane 1998 Dacurajane
D honda peter 2002 Dhondapeter
U bmz fred 1993 Dbmzfred
To get the sales in Sheet1, I am giving the following vlookup.
=VLOOKUP(E2|Sheet5!A2:F4|5|FALSE)
I looked at other similar answers, trimmed the columns and did what the recommendations were, but it still returns #N/A
Can anyone point out what my mistake it?
Thanks In advance.
Use INDEX/MATCH
=INDEX(Sheet5!D:D, MATCH(E2, Sheet5!A:A&Sheet5!B:B&Sheet5!C:C, 0))
Enter with Ctrl-Shift-Enter
Also I am assuming that the sales is in column D of Sheet5. If different, put the appropriate column in as first parameter to the INDEX function.
Also please consider using restricted range references in the match function, instead of full column references - for speed & efficiency.
on cell E1 you will have KEY on cell E2 you will place =CONCATENATE(A2,B2,C2,D2) Combines contents above into a phrase "DFocusManuel2016". That should work for the combine part just paste that and drag down as desired on E2.
You can cover the rest of the cell population by simply assigning links to the cells so on cell A2,B2,C2 and D1 you will have =SHEETNAME!Y10 which Y10 corresponds to the source cell and Sheet name to the source sheet.

Extract part of a list given start value in excel

I want to extract part of a list given the starting value found by means of index/match combinations or the vlookup function. However, hence only the initial value is returned but subsequent values in the list are also of interest and should follow this initial found value. Is there a way of combining functions that return sub-arrays? Thank you in advance for the help. Best Regards, Oliver
Say we have data like:
and we wanted the UK people, VLOOKUP() would only return William. If we want more than just William, then in C1 enter:
UK
and in C2 enter the array formula:
=IFERROR(INDEX($B$2:$B$22, SMALL(IF(C$1=$A$2:$A$22, ROW($A$2:$A$22)-MIN(ROW($A$2:$A$22))+1, ""), ROW(A1))),"")
and copy down:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
1. Incrementing Row/Column Count Options
1a. Start Value, Sub-List Length
Let us assume this sample data and result format:
A B C D
---------------------------------------------
1|John Start Value Amy Amy
2|Jane Sub-List Length 3 Amber
3|Amy Chris
4|Amber
5|Chris
6|Dan
7|Bob
You can achieve this by using the following formula and autofilling down:
=IFERROR(INDEX($A:$A,(MATCH($C$1,$A:$A,0))+IF(ROWS($1:1)<=$C$2,ROWS($1:1)-1,#VALUE!)),"")
1b. Start Value, End Value
Sample data and result format:
A B C D
---------------------------------------------
1|John Start Value Amy Amy
2|Jane End Value Dan Amber
3|Amy Chris
4|Amber Dan
5|Chris
6|Dan
7|Bob
Use this formula and autofill down:
=IFERROR(INDEX($A:$A,(MATCH($C$1,$A:$A,0))+IF(ROWS($1:1)<=(MATCH($C$2,$A:$A,0)-MATCH($C$1,$A:$A,0)+1),ROWS($1:1)-1,#VALUE!)),"")
1c. Start Value, End of list
Sample data and result format:
A B C D
---------------------------------------------
1|John Start Value Chris Chris
2|Jane Dan
3|Amy Bob
4|Amber
5|Chris
6|Dan
7|Bob
Use this formula and autofill down:
=IFERROR(INDEX($A:$A,(MATCH($C$1,$A:$A,0))+IF(ROWS($1:1)<=(COUNTA($A:$A)-MATCH($C$1,$A:$A,0)+1),ROWS($1:1)-1,#VALUE!)),"")
Note: for options 1a - 1c if you wish to have a horizontal list result replace ROWS($1:1) with COLUMNS($A:A) and autofill to the right
2. Using a Predefined Array:
Note: These options requires you to select the range you want the values to be in, and then use ctrl + shift + enter to enter the data.
2a. Vertical Sublist, Fixed Length:
Sample data and result format, with fixed length of 4
A B C D
---------------------------------------------
1|John Start Value Amy Amy
2|Jane Amber
3|Amy Chris
4|Amber Dan
5|Chris
6|Dan
7|Bob
Select cells D1:D4 and enter in this formula:
=INDEX($A:$A,MATCH($C$1,$A:$A,0)+TRANSPOSE({0,1,2,3}))
Then use ctrl + shift + enter
2b. Horizontal Sublist, Fixed Length:
Sample data and result format, with fixed length of 4
A B C D E F G
----------------------------------------------------------
1|John Start Value Amy Amy Amber Chris Dan
2|Jane
3|Amy
4|Amber
5|Chris
6|Dan
7|Bob
Same as last formula without TRANSPOSE and selecting cells D1:G1:
=INDEX($A:$A,MATCH($C$1,$A:$A,0)+{0,1,2,3})
Again, use ctrl + shift + enter
Note: To increment the length add additional values to the array {0,1,2,3, ...}

SUMIF for first 5 cells meeting criteria

Simple Excel Table such as
A B
1 John 5
2 John 7
3 John 9
4 Jill 25
5 John 21
6 John 22
7 Jill 50
8 John 100
9 John 2000
10 Jack 4
Using SUMIF, we can return the total assigned to John.
=SUMIF(A:A,"John",B:B)
Is there a way to return only the first 5 values that match the criteria? Or is there a way to return the 5 smallest values for John? Either would work.
Oh well. I'll go ahead and presume that you have Excel 2010 or later.
With e.g. "John" in D1, enter this formula in E1:
=SUMIFS($B$1:$B$10,$A$1:$A$10,D1,$B$1:$B$10,"<="&AGGREGATE(15,6,$B$1:$B$10/($A$1:$A$10=D1),5))
Copy down to give similar results for names in D2, D3, etc.
Regards
Formula:
=IF(COUNTIF($A$1:A1,A1)<=5,SUMIF($A$1:A1,A1,$B$1:B1),"")
The last value shown for each person will be the sum of the first (up to)5 values for that person. Just copy and paste values then sort.
Your sample data would show the same result for either the first 5 or lowest 5 as John's numbers are in ascending order. If that is not always the case or if you need to provide compatibility to versions of Excel earlier than 2010 I would offer the following. Note that in my sample image, I've resorted the numerical values in descending order to illustrate the difference.
For John's first 5 values (E2 in the sample image):
=SUM(INDEX(($B$2:$B$11)*($A$2:$A$11=D2)*(ROW($1:$10)<=SMALL(INDEX(ROW($1:$10)+($A$2:$A$11<>D2)*1E+99,,), 5)),,))
For John's lowest 5 values (F2 in the sample image):
=SUMPRODUCT(SMALL(INDEX(($B$2:$B$11)+($A$2:$A$11<>D2)*1E+99,,),ROW($1:$5)))
These are standard formulas. Any array processing is supplied by INDEX and/or SUMPRODUCT. Ctrl+Shift+Enter is not required. Some form of error control may be necessary when there are less than 5 matching values; a simple IF(COUNTIF(), <formula>) would suffice. When transcribing these type of formulas it is important to note that ROW(1:10) is the position within B2:B11 or A2:A11, not the actual row on the worksheet.
 
                  
In C1 enter:
=IF(A1="John",1,0)
In C2 enter:
=IF(A2="John",1+MAX($C$1:C1),0)
and copy down. Then use:
=SUMPRODUCT((A:A="John")*(B:B)*(C:C<6))
.
Assuming John in D1 you can get the sum of the 5 smallest values for John with this array formula
=SUM(SMALL(IF(A$1:A$100=D1,B$1:B$100),{1,2,3,4,5}))
confirm with CTRL+SHIFT+ENTER and copy down for to work for all names in the list

Resources