Excel Formula: Count Matches Where 2 IDs have a Common Tag - excel

I'm trying to figure out the most efficient way to do this in one cell's formula and would appreciate some assistance.
SHEET1
ID TAG
123 Flowers
123 Sports
135 Sports
456 Flowers
456 Cars
123 Clouds
456 Sports
SHEET2
ID1 ID2 RESULT
123 456 2 [WANT TO CALCULATE THIS]
135 246 0 [WANT TO CALCULATE THIS]
The way the formula should work is look in SHEET1 where both ID1 and ID2 have a tag in common and count them.

Here's one approach which should work for you.
Setup assumed:
Sheet1 data is in range A1:B8
Sheet2 data is in range A1:B3
Then formula that you should insert in Sheet2!C2 shall be:
=SUM((FREQUENCY(IFERROR(MATCH(IF(Sheet1!$A$1:$A$8=Sheet2!A2,Sheet1!$B$1:$B$8,"z"),IF(Sheet1!$A$1:$A$8=Sheet2!B2,Sheet1!$B$1:$B$8,"a"),0),"a"),IFERROR(MATCH(IF(Sheet1!$A$1:$A$8=Sheet2!A2,Sheet1!$B$1:$B$8,"z"),IF(Sheet1!$A$1:$A$8=Sheet2!B2,Sheet1!$B$1:$B$8,"a"),0),"b"))>0)+0)
NOTE: This is an array formula and shall be inserted by committing CTRL+SHIFT+ENTER and not just ENTER. If entered correctly, Excel put {} braces around it.

Related

Excel find last match from two tables

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.

Excel: How to add a column from other sheet based on a key column?

I have 2 list. Both have a lot of column. I would like to insert a column from Sheet2 to Sheet1 based on a ceratin key column. Also sheet2 have much more rows than sheet1 so it ll be inserted only partly and still there ll be elements with no matches. For an example:
Sheet1:
Names ID Car Color
John 1 Audi Empty
Andy 4 Toyota Empty
Mike 3 BMW Empty
Tony 2 Suzuki Empty
Sheet2:
ID Cost Color
6 200 Blue
3 200 Red
4 300 Green
5 100 Red
1 50 Black
I would like to get the "color" from Sheet2 to Sheet1 by using the "ID". Using Excel 2010. I suspect I need INDEX+MATCH combination, but the examples I can find are not detailed and more simple so I coudn't figure out how to use them.
How about inserting this formula on Column D in the first row, then dropping the formula down:
=VLOOKUP(B1,Sheet2!$1:$1048576,3,FALSE)
Or to find the column that contains "Color", use Index Match Match, as follows:
=INDEX(Sheet2!$1:$1048576,MATCH(Sheet1!B2,Sheet2!A:A,0),MATCH("Color",Sheet2!$1:$1,0))
This will find the value in Column B in Sheet2 and give you the row number, then it will find the Column that contains "Color" and return the Column number, with those two numbers Index will return your color.

Need help in generating a formula for nextID excel column

I have a an excel sheet with the following columns
ID, City, Record no.
I need to generate a column called Next ID based on the record number.
i.e if the city is same I need to populate the Next ID column with the ID of the previous record number.
Is there a way to do this in excel?
Here is the sample data, with the Next Column populated manually for reference:
ID City Record no. **Next ID
121 Paris 3
122 Paris 2 121
123 Paris 1 122
124 New York 2
125 New York 1 124
Please see attached image
I've edit the question as I was not able to add a image in the comments. My data is not sorted so I used the second formula. This works for some cases but doesn't work for others. see the pink colored cells which should have a value but do not.
If your data is always sorted use this:
=IF(B2=B1,A1,"")
in D2 and copy/drag down
If not sorted then use this:
=IFERROR(INDEX(A:A,AGGREGATE(15,6,ROW($C$2:$C$6)/(($C$2:$C$6=C2+1)*($B$2:$B$6=B2)),1)),"")

how to find non unique combinations of two variables in Excel?

I have two columns in Excel that identify individual records (ID and code). Some of these may occur multiple times. For some records, the code may be missing. And some IDs belong to multiple codes. I need to find these IDs that have non unique associations with a given code, and show what those are.
Minimal example:
ID code
K151 ABC
K152 BCD
K153 EFG
K154
K151 ABC
K154 HDG
K153 EFF
K151 ABC
K153 EFG
So I need to have a list (possibly with the number of occurences):
ID code freq
153 EFG 2
153 EFF 1
154 1
154 HDG 1
It is fairly easy to do something similar using a Pivot Table, but note that e.g. K151 - ABC occurs 3 times, and it should not be listed, just those IDs that have multiple codes. Also, in the pivot table the codes are collapsed under the ID as parent category and they are not shown side by side.
It is also OK, if the non-uniquely coded IDs are flagged in the original table in a new variable, and then these records can be filtered manually using the flag.
ID code flag
K151 ABC 0
K152 BCD 0
K153 EFG 1
...
K153 EFF 1
...
I need to find a solution in Excel (2013), not VBA or anything else, and ideally the solution should also be compatible with LibreOffice Calc.
The flagging will be simple.
Formula in C2 downwards:
=COUNTIF($A:$A,"="&A2)<>COUNTIFS($A:$A,"="&A2,$B:$B,"="&B2)
Formula in D2 downwards:
=IF(C2,COUNTIFS($A:$A,"="&A2,$B:$B,"="&B2),0)
For OpenOffice we need SUMPRODUCT because COUNTIF will not count if blank.
Formula in C2 downwards:
=COUNTIF($A$1:$A$20,"="&A2)<>SUMPRODUCT(($A$1:$A$20=A2)*($B$1:$B$20=B2))
Formula in D2 downwards:
=IF(C2,SUMPRODUCT(($A$1:$A$20=A2)*($B$1:$B$20=B2)),0)
Copy and paste the first two columns in a different location.
Select the data and Use Remove Duplicates tool in the Data tab.
Start third column and use COUNTIFS for multiple column criteria to get the count of each row.

How to SUM parts of a column which have same text value in different column in the same row

I have a column with names and a column with numbers:
FirstName Name Number
John Smith 17
John Smith 26
Peter Smith 116
Peter Smith 25
Franck Black 17
Luke Peterson 17
Luke Peterson 37
Names with same FirstName and Name represent the same person. I need to sum the numbers associated with them. I prefer not to use VBA.
A PivotTable might suit, though I am not quite certain of the layout of your data:
The bold numbers (one of each pair of duplicates) need not be shown as the field does not have to be subtotalled eg:
This can be done by using SUMPRODUCT as well. Update the ranges as you see fit
=SUMPRODUCT(($A$2:$A$7=A2)*($B$2:$B$7=B2)*$C$2:$C$7)
A2:A7 = First name range
B2:B7 = Last Name Range
C2:C7 = Numbers Range
This will find all the names with the same first and last name and sum the numbers in your numbers column
If your data has the names grouped as shown then you can use this formula in D2 copied down to get a total against the last entry for each name
=IF((A2=A3)*(B2=B3),"",SUM(C$2:C2)-SUM(D$1:D1))
See screenshot

Resources