INDEX/MATCH with multiple criteria - excel

Need assistance with an Excel formula.
I am trying to get a summary of a range. I have been playing with the Match, Index, Countif functions and can create a list of unique dates and then using a vlookup to return the persons name.
I need something to take the persons name into account so if the same date appears for 2 people, it shows as 2 different lines in the output.
Please see table below - A&B is the input, D&E is what I can currently get, G&H is the output I want.
Thanks in advance.
A B C D E F G H
1 13/01/15 RYAN 13/01/15 RYAN 13/01/15 RYAN
2 13/01/15 RYAN 14/01/15 RYAN 14/01/15 RYAN
3 14/01/15 RYAN 15/01/15 RYAN 14/01/15 FRANK
4 14/01/15 RYAN 15/01/15 RYAN
5 14/01/15 RYAN
6 14/01/15 RYAN
7 14/01/15 FRANK
8 14/01/15 FRANK
9 14/01/15 FRANK
10 15/01/15 RYAN
11 15/01/15 RYAN
All knowledge I have of Excel in self taught thanks the forums like stackoverflow so any help is greatly appreciated.

Much the easiest way to achieve this is with a PivotTable. Add labels to Columns A and B (say Date and Name) then select A:B, INSERT, Tables - PivotTable, chose where in Existing Sheet or New Sheet and drag Date and then Name (below) into ROWS.
Reformat as desired.

The style of array formula I prefer requires that there be an unused cell directly above the first formula to avoid circular references. This unused cell can be used as a column header label.
The array formulas¹ used in D2:E2 are,
=IFERROR(INDEX(A:A, MATCH(0, COUNTIFS(D$1:D1,A$1:INDEX(A:A, MATCH(1E+99, A:A))&"",
E$1:E1,B$1:INDEX(B:B, MATCH(1E+99, A:A))&""), 0)), "")
=IFERROR(INDEX(B:B, MATCH(0, COUNTIFS(D$1:D1,A$1:INDEX(A:A, MATCH(1E+99, A:A))&"",
E$1:E1,B$1:INDEX(B:B, MATCH(1E+99, A:A))&""), 0)), "")
Fill down as necessary. These array formulas have their most active lookup ranges dynamically truncated at the row containing the last date in column A to cut calculation cycles down to the minimum necessary.
    
I have added an extra entry for Frank at the bottom of the list of your sample data to further demonstrate the double unique lookup.
¹ Array formulas need to be finalized with Ctrl+Shift+Enter↵. If entered correctly, Excel with wrap the formula in braces (e.g. { and }). You do not type the braces in yourself. Once entered into the first cell correctly, they can be filled or copied down or right just like any other formula. Try and reduce your full-column references to ranges more closely representing the extents of your actual data. Array formulas chew up calculation cycles logarithmically so it is good practise to narrow the referenced ranges to a minimum. See Guidelines and examples of array formulas for more information.

With helper columns this could be achieved without any array formulas. The idea is to get first a concatenation of date and name, then get only the line number of the first occurrence of date&name in the list, get those line numbers without duplicates and finally get the INDEXes of those line numbers from A:A and B:B.
Formulas:
D1:D13 =A1&B1
E1:E13 =IF($D1<>"",MATCH($D1,$D:$D,0),"empty")
F1 =MIN($E:$E)
F2:F13 =LARGE($E:$E,COUNTIF($E:$E,">"&$F1))
H1:H13 =INDEX($A:$A,$F1)
I1:I13 =INDEX($B:$B,$F1)

Related

Mode Text 2nd most common text value

IFERROR(INDEX($I$7:$I,MODE(IF($I$7:$I<>"",MATCH($I$7:$I,$I$7:$I,0)))),"No data")
With this formula, which calculates the most common text value, I need to have the 2nd most common.
Column I content:
Apple
Orange
Apple
Apple
Orange
In this example, I need to get Orange. How is that possible? I can't figure how.
A PivotTable might suit:
and copes with ties for rank.
You can extract the most frequent item in the list with an array formula.
=INDEX(MyList,MATCH(MAX(COUNTIF(MyList,MyList)),COUNTIF(MyList,MyList),0))
Note that an array formula must be confirmed with Shift+Ctl+Enter instead of the customary singular Enter required for normal formulas. When entered wrongly it will display a #NUM! error.
For simplicity's sake I have used a named range MyList in the formula. However, if you prefer, you can replace the name with something like $I$7:$I$1000.
To extract the second-most frequent expression in the list you could use a formula constructed analogue to the above.
=INDEX(MyList,MATCH(LARGE(COUNTIF(MyList,MyList),MAX(COUNTIF(MyList,MyList))+1),COUNTIF(MyList,MyList),0))
This formula is built on the logic that n equals the highest number of occurrences. Therefore the second highest must rank as n + 1, being MAX(COUNTIF(MyList,MyList))+1) in the above formula. By the same method the third ranked could be extracted.
You can embed these formulas in an IFERROR() function.
I found this on Mr Excel
Return most common, 2nd most common, 3rd most common, etc. text string in an array
Spreadsheet Formulas
Cell ___ Formula 'Notice that the cells are B2, D2, E2. Column C is blank
B2 =IF(A2="","",IF(COUNTIF(A$2:A2,A2)=COUNTIF($A$2:$A$100,A2),COUNTIF($A$2:$A$100,A2)+(ROW()/1000),""))
D2 =IF(ROWS($1:1)>COUNT(B:B),"",INDEX(A:A,MATCH(LARGE(B:B,ROWS($1:1)),B:B,0)))
E2 =IF(D2="","",COUNTIF($A$2:$A$100,D2))<br><br>
Results
___ A ________ B ___C ___D _________E
1 Data Set:___Helper ____ Name ____ Occurrences
2 Harmon _____________ Williams ______4
3 Smith _______________ Smith ________3
4 Smith _______________ Harmon ______2
5 Harmon_____ 2.005
6 Williams
7 Williams
8 Smith _______3.008
9 Williams
10 Williams ____4.010
you can try to tie this all together in a single formula but it's simpler and more agile in a spreadsheet environment to just break out the problem in a few separate steps.
take a given column of values you're wanting to count/rank - i'll call it RankList in examples below.
if you're not setting named ranges (do yourself a favor and use named ranges) you'll want this to be your column range - i.e. A:A
now in another column use
=unique(RankList)
there's your list of unique values, now we just need to count the instances of each unique value in the original RankList - this is simple - in the next column over simply use
=countif(RankList,B1)
B1 above represents the cell adjacent to the formula, wherever that might be on your sheet. now autofill the formula in with the relative cell value for each item. now all of your items are counted by instance.
now we want to sort them by value, highest to lowest. create another named range, selecting the two columns containing the =unique(RankList) and =countif(RankList,B1) formulas that were just created, i'll refer to it as UniqueCount
use the following
=sort(UniqueCount, 2, false)
that's it. again you can accomplish this by stacking formulas like in the above examples, but in practice i've found that you won't know what you'll want to do additionally with your data/sheet later on. keeping it broken up in discrete steps like this makes it much easier to make adjustments.

Excel, append one range to the end of another in one column

I have two columns of data in Excel. I would like to add a third column which combines the first and second. How can I do this with a formula such that I can add or remove data from columns A and B without ever having to touch column C?
Column A Column B Column C
Bob Mary Bob
Joe Melissa Joe
Jim Jackie Jim
Mary
Melissa
Jackie
The question explicit mention Microsoft Office Excel but I think would be good to add that if you are using Google Sheets a simpler solution is to use the curly brackets function/operator as mentioned by Lake at https://stackoverflow.com/a/14151000/1802726.
Here is a simple solution using FILTERXML and TEXTJOIN that can append MULTIPLE RANGES OF ANY SIZE, ARRAY FORMULAS AND REGULAR FORMULAS. Just replace YOUR_RANGES with the ranges or dynamic arrays you wish to join:
Simple version that ignores empty cells:
=FILTERXML("<A><B>" & TEXTJOIN("</B><B>",TRUE,YOUR_RANGES) & "</B></A>", "//B")
This one includes empty cells:
=IFERROR(FILTERXML("<A><B>" & TEXTJOIN("</B><B>",FALSE,YOUR_RANGE) & "</B></A>", "//B"), "")
If your input data contains the "<" character, the formulas above will return an error, so use this one instead:
=IFERROR(SUBSTITUTE(FILTERXML("<A><B>" & SUBSTITUTE(SUBSTITUTE(TEXTJOIN("ΨΨ",FALSE,YOUR_RANGE),"<","ЉЉ"),"ΨΨ","</B><B>")&"</B></A>","//B"),"ЉЉ","<"),"")
Note: you can change the FALSE to TRUE to ignore empty cells.
Note 2: You can replace the characters ЉЉ and ΨΨ by any character(s). I used these specific characters because it is very unlikely that your input data will contain ЉЉ or ΨΨ, which would cause errors.
NOTES:
Tested on:
Excel 365
EXAMPLE:
Using the simple version of the formula:
=FILTERXML("<A><B>" & TEXTJOIN("</B><B>",TRUE,A1:A3,B1:B3,C1:C3) & "</B></A>", "//B")
As a result you will get a dynamic array with the joined/appended ranges:
You can then apply any dynamic array formula (like UNIQUE) to the result.
HOW THIS WORKS:
The JOINTEXT function grabs your ranges and joins them as a text with the delimiter "</ B >< B >". Then, after adding "< A >< B >" to the beginning and "</ B ></ A >" to the end, we have an XML formatted text:
<A><B>1</B><B>2</B><B>3</B><B>A</B><B>B</B><B>C</B><B>!</B><B>#</B><B>#</B></A>
Finally, the FILTERXML will separate the tags into a dynamic array which will be the joined/appended ranges.
Enter the following formula into cell C1
=IF(ROW()>COUNTA(A:B),"",IF(ROW()<=COUNTA(A:A),INDEX(A:A,ROW()),INDEX(B:B,ROW()-COUNTA(B:B))))
Then copy down as far as you need.
Here's a nice way of interleaving the two rows.
In other words, turning this:
A X
B Y
C Z
into this:
X
A
Y
B
Z
C
Say the above table is in columns one and two, you'd do:
=IF(MOD(ROW(),2)=0,INDIRECT(ADDRESS(INT(ROW()/2), 1)),
INDIRECT(ADDRESS(INT(ROW()/2)+1, 2)))
Explanation
Let's break that down a little. The first part is MOD(ROW(), 2) which returns a zero if the current row is even, and a one if it's odd.
So the IF goes FALSE/TRUE/FALSE/TRUE as we go down the column.
Next, the ADDRESS(INT(ROW()/2), 1) returns us a string representation of the address of the cell at column 1 and at half the current row. (Rounded down). This piece on its own looks like:
#VALUE!
$A$1
$A$1
$A$2
$A$2
$A$3
$A$3
(That first #VALUE error is because 1/2 = 0.5 which rounds down to zero. There's no row zero!)
The INDIRECT function returns whatever value is found at that address.
The rest is pretty clear.
NOTE: There may be a slicker way than using INDIRECT and ADDRESS. Suggestions welcome.

Summing numeric portion of cell when alpha portion of cell is the same

I have a spreadsheet that has columns for dates and the values can be either "1v, .5v, 1p, .5p, 1s, .5s"
I have 3 columns in each row one for each letter "v, p and s". I want to be able to add the total of all cells in the range grouped by letter and then display the sum for each letter in it's respective column (v, p or c).
Here is an example of the sheet:
Name Vacation Personal Sick 1/5/15 1/6/15 1/7/15 1/8/15
Billy 1.5 1 0 .5v 1v 1p
It is the formula that goes in the vacation/personal/sick cell that I just can't figure out.
I went down the array formula route and came up with essentially the same formula as #Sancho.s :-
=SUM(LEFT($E2:$H2,LEN($E2:$H2)-1)*(RIGHT($E2:$H2)="v"))
You could modify it to take account of blanks:-
=SUM(IF($E2:$H2<>"",LEFT($E2:$H2,LEN($E2:$H2)-1)*(RIGHT($E2:$H2)="v")))
Perhaps this would be better, to ignore any mis-formatted cells:-
=SUM(IFERROR(LEFT($E2:$H2,LEN($E2:$H2)-1)*(RIGHT($E2:$H2)="v"),0))
These all have to be put in with Ctrl-Shift-Enter.
Assuming the range you posted starts at A1, use
=SUMPRODUCT((RIGHT($E2:$G2,1)="v")*LEFT($E2:$G2,LEN($E2:$G2)-1))
in B2. Change "v" and the range to use suitably.
Pro:
It is not an array formula. See why this may be important
Con:
I could not make it work with blank cells.
This "array entered" version will also allow blanks
=SUM(IF(RIGHT(E2:G2)="v",SUBSTITUTE(E2:G2,"v","")+0))
confirmed with CTRL+SHIFT+ENTER

Formula to find match in two-dimensional range

I need a formula that will look up a value in a 2-dimensional range and return the coordinates or cell address of the matching cell. For example:
R A B C
1 John Matt Pete
2 Sara Bret Chad
3 Lila Maya Cami
I want to search the range A1:C3 for Chad and return C2 or 2,3. How can I accomplish this using Excel formulas? (I'll actually end up applying this to Google Sheets).
Thanks!
Old question, but I thought I'd share a much simpler and elegant answer here that doesn't involve helper columns or complicated formulas, so that more people will get things done easier. Assuming that the table contains unique values and that you use E1 to store your search string Chad and E2 to display the result:
if you want the row and column result of 2,3 in E2:
=SUMPRODUCT((A1:C3=E1)*ROW(A1:C3)) & "," & SUMPRODUCT((A1:C3=E1)*COLUMN(A1:C3))
if you want the R1C1 style cell address string of C2 in E2:
=ADDRESS(SUMPRODUCT((A1:C3=E1)*ROW(A1:C3)),SUMPRODUCT((A1:C3=E1)*COLUMN(A1:C3)))
if you want the found cell's contents of Chad in E2:
=INDIRECT(ADDRESS(SUMPRODUCT((A1:C3=E1)*ROW(A1:C3)),SUMPRODUCT((A1:C3=E1)*COLUMN(A1:C3))))
How things work:
SUMPRODUCT returns in this case the sum of the products between a boolean array of TRUE (searched value found in cell) and FALSE (searched value not found in cell) for every cell in the table and the corresponding row/column (absolute) numbers of those cells in the sheet; thus, the result is essentially the row/column (absolute) number of the cell where the value has been found, since TRUE=1 and FALSE=0 in mathematical terms
ADDRESS returns a cell's address as text (not as reference!)
INDIRECT returns the reference corresponding to a cell's text address
Source and credit goes to: this answer by XOR LX. Could have added the link in a comment, mentioning the duplicate question, but I wanted to expand and explain the answer a little bit, therefore more characters were needed.
Assuming you're using Excel 2007 and above.
You will need a helper column. If your table looks like in your example, in cell D1 write:
=IFERROR(MATCH($E$1,$A1:$C1,0),0)
And drag it down. Then in cell E1 write your search value ("Chad" for instance). Then you have your search result in cell E2 with this formula:
=IF(MAX($D:$D)=0,NA(),MATCH(MAX($D:$D),$D:$D,1)&","&MAX($D:$D))
If you want a simpler solution, it is possible to use only one helper (or not at all, at the cost of a complicated formulae).
Let's say I take your example. I will use the D column to display result :
In D1, I put the name I want to find : Chad
In D2, I put the helper that will return an Index of the value searched (-1 if not found) : =IFERROR(MATCH(D1,SPLIT(TEXTJOIN(";",TRUE,A1:C3),";"),0),-1)
In D3, I put the formulae to get the row,column value (FALSE if not found) : =IF(D2<>-1,ROUNDUP(DIVIDE(D2,COLUMNS(A1:C3))) & "," & IF(MOD(D2,COLUMNS(A1:C3))=0,COLUMNS(A1:C3),MOD(D2,COLUMNS(A1:C3))))
If you really want to use only one formulae, it is possible in D3 to replace all references to D2 by the formulae used in D2.
This formula returns the row and column number of a given value in a two-dimensional array.
=LET(
array, B2:D4,
findvalues, C7,
arrayrows, ROWS(array),
arraycols, COLUMNS(array),
rowindex, SEQUENCE(arrayrows*arraycols,,1,1/arraycols),
colindex, MOD(SEQUENCE(arrayrows*arraycols,,0),arraycols)+1,
flatarray, INDEX(array,rowindex,colindex),
valueflatindex, MATCH(findvalues,flatarray,0),
valuerow, ROUNDUP(valueflatindex/arraycols,0),
valuecol, MOD(valueflatindex-1,arraycols)+1,
absvaluerow, MIN(ROW(array))+valuerow-1,
absvaluecol, MIN(COLUMN(array))+valuecol-1,
CHOOSE({1,2},absvaluerow,absvaluecol)
)
A B C D E
1
2 John Matt Pete
3 Sara Bret Chad
4 Lila Maya Cami
5
6
7 find: Chad
8 formula: 3 4
More precisely, this formula scans a given array row by row and returns the address of the first occurrence of a given value.
If you need the row and column numbers relative to the array's top left cell, then in CHOOSE(...), instead of absvaluerow/absvaluecol, use valuerow/valuecol.
If you want the values to be comma separated and in one cell, instead of CHOOSE(...), use absvaluerow & "," & absvaluecol
If your Excel version does not support the latest functions, such as LET, the formula should still work if you rewrite it so that it does not use the LET variables.
Find Multiple Values
You can also find multiple values in an array using this formula as explained in my answer in this thread.

Combining COUNT IF AND VLOOK UP EXCEL

I have multiple spreadsheets in a workbook and I would like the following in basic English talk:
IF worksheet1(cell)A3, appears in 'worksheet2' column B - count how many times it appears in column b 'worksheet 2'
So in other words - Lets say A3 = BOB smith - in work sheet 1
and appears 4 times in worksheet 2 - I want the formula to count the fact that A3 'Bob smith' is in worksheet 2 4 times, and come back and tell me 4.
I have attempted to do separate calculations - with use of Vlookups - then in another cell to count/do if statement
for example
=COUNTIF(VLOOKUP(A9,'To retire'!J:J,9,1))
=IF(J228=O233, 'worksheet2'!F440,0)
=VLOOKUP(A3,'worksheet2'!A:A,1,1)
Help would be very much appreciated, I am very stuck - I am unsure if I am looking into this too deeply or not enough! Thank you in advance
This is trivial when you use SUMPRODUCT. Por ejemplo:
=SUMPRODUCT((worksheet2!A:A=A3)*1)
You could put the above formula in cell B3, where A3 is the name you want to find in worksheet2.
=COUNTIF() Is the function you are looking for
In a column adjacent to Worksheet1 column A:
=countif(worksheet2!B:B,worksheet1!A3)
This will search worksheet 2 ALL of column B for whatever you have in cell A3
See the MS Office reference for =COUNTIF(range,criteria) here!
You can combine this all into one formula, but you need to use a regular IF first to find out if the VLOOKUP came back with something, then use your COUNTIF if it did.
=IF(ISERROR(VLOOKUP(B1,Sheet2!A1:A9,1,FALSE)),"Not there",COUNTIF(Sheet2!A1:A9,B1))
In this case, Sheet2-A1:A9 is the range I was searching, and Sheet1-B1 had the value I was looking for ("To retire" in your case).
Try this:
=IF(NOT(ISERROR(MATCH(A3,worksheet2!A:A,0))),COUNTIF(worksheet2!A:A,A3),"No Match Found")
If your are referring to two worksheets please use this formula
=COUNTIF(Worksheet2!$A$1:$A$50,Worksheet1cellA1)
In case referring to to more than two worksheets please use this formula
=COUNTIF(Worksheet2!$A$1:$A$50,Worksheet1cellA1)+=COUNTIF
(Worksheet3!$A$1:$A$50,Worksheet1cellA1)+=
COUNTIF(Worksheet4!$A$1:$A$50,Worksheet1cellA1)

Resources