EXCEL: count combinations for unique values - excel

I'm trying to count the following in excel:
I have a number of unique values for X (say 4), each appearing twice (in random order) in the data set. I want to count the number of each combination (regardless of order) of values on a second column Y for each value of X.
Example (here in order):
X Y
1 A
1 D
2 A
2 C
3 B
3 C
4 A
4 D
As output, I need:
n A,D 2
n A,C 1
n B,C 1
n B,D 0
(Let's assume all other combinations don't exist.)
Is this possible without rearranging the data? I don't mind putting in 4 formulas (for each possible combination).
My starting point was akin to this: excel count unique combinations of columns, but I can't quite figure it out and also I know what feels like next to nothing about excel or coding...

To get the count of pairs if the data is not in order, first one needs to get the pairs by X. Create a unique list of X and use this formula:
=CHAR(AGGREGATE(15,6,CODE($B$2:$B$9)/($A$2:$A$9=D2),1))&","&CHAR(AGGREGATE(15,6,CODE($B$2:$B$9)/($A$2:$A$9=D2),2))
Note this only works if the Y is one character like your data.
Then create a unique list of that output and use a standard COUNTIF:
=COUNTIF(E:E,G2)
Or you can use a pivot table on the first helper columns to get the same without the need of getting the unique pairings.

If you use a helper column, you can combine the data into something you can count. Use the formula =IF(A2<>A1,B2,C1&","&B2) in column C. So your data then becomes:
X Y
1 A A
1 D A,D
2 A A
2 C A,C
3 B B
3 C B,C
4 A A
4 D A,D
Then it's a matter of counting up the combinations. Here's a snapshot of my example:

You can use the MMULT function for this purpose, like this (You can insert line breaks with Alt+Enter but you do not have to, it also works if you write it on the same line):
=SUMPRODUCT(
--($B$2:$B$19= LEFT($D2,FIND(",",$D2)-1) ),
MMULT(
--($A$2:$A$19=TRANSPOSE($A$2:$A$19)),
--($B$2:$B$19= MID($D2,FIND(",",$D2)+1,LEN($D2)) )))
You have to enter this as an array formula (After typing, press Ctrl+Shift+Enter, instead of just Enter; you will see the formula inside braces, like this: {=...}).
Here is a screenshot to show what the ranges in the formula mean:

Related

SUMIF but for products?

I have a table like the following:
X 1
X 3
X 2
Y 2
Y 5
Z 3
Z 4
I know I could use SUMIF to calculate the sum of the second column for each value in the first column, e.g., =SUMIF(A1:A7,"X",B1:B7) would give me the sum of values for X: 1+3+2=6. However, I want the product of values in the second column for each value in the first column. I.e., the output would be:
A 6
B 10
C 12
When I try to search for SUMIF but for product, I only see suggestions to use SUMPRODUCT, but that multiplies arrays together and then adds their values, whereas I don't want to sum anything, just multiply all the values within a column.
My only idea is to create a new column with the log of the values, then do EXP(SUMIF(...)) to get the product. However, this will not work if any values are 0 or negative. Is there any way to directly compute the product I want?
If one has the Dynamic Array formula FiLTER:
=PRODUCT(FILTER(B1:B7,A1:A7="X",0))
You can also use a Pivot Table:
There are probably cleverer* ways, but you could use this array formula:
=PRODUCT(IF($A$1:$A$7=D1,$B$1:$B$7))
*Edit - as I said, see Scott's answer. I always forget the new formulas.

Sum rows containing only the first appearance of value in separate column

I have a table that looks something like this:
Column A | Column B
A 1
B 2
C 3
A 4
What I want to do is get the sum of the values in Column B, but only first the first occurrence of each value in Column A. Thus, the result I want to get is (1 + 2 + 3) = 6 (Adding the first three rows, but omitting the fourth, because a row with 'A' in Column A has already included in the sum).
I've tried looking at Frequency, but I haven't been able to figure out how to use it properly to get the result I want.
Use SUMPRODUCT and MATCH:
=SUMPRODUCT(--(ROW(A1:A4)=MATCH(A1:A4,A:A,0)),B1:B4)

Excel PivotTable; how to show values horizontally

I'm trying to rearrange a pivot table that organizes all values (not sum or other statistic) from an original table. Seems simple but I can't find a way to make it values rather than sums.
My original data looks like:
Rank Name
1 A
1 B
2 C
2 D
3 E
3 F
and with the pivot table I get something like:
Rank Name
1 A
B
2 C
D
3 E
F
and I would like to rearrange it like so:
1 2 3
A C E
B D F
There's a way to achieve that using array functions instead of pivot tables.
Suppose your original data is located in A1:B7.
To get the headers row (1, 2, 3):
A10: =MIN(A2:A7)
B10: =SMALL($A$2:$A$7,COUNTIF($A$2:$A$7,"<="&A10)+1)
Then copy B10 as far right as you need to get all other values
To get the values for each rank, set an array formula (Ctrl+Shift+Enter) on the range A11:A16 (or lower to fit more items) with this formula:
=IFERROR(INDEX($B$2:$B$7,SMALL(IF($A$2:$A$7=A$10,ROW($A$2:$A$7)-ROW($A$2)+1),ROW()-ROW(A$10))),"")
Then copy this range (A11:A16) as far right as you need...

Excel - Formula or Macro to fill a cell based on another cell that links to yet another cell

In Excel, I am trying to make a cell based of the values contained in two other cells.
I need Cells X and Y to have data based on Cells L and #, like so....
X Y L 1 2 3 4 5 6
A 6 1 1 6;1 6;1 7;1 7;2 7;2 8;1
B 7 2 4 6;1 6;1 7;1 7;2 7;2 8;1
So row A, has columns X and Y filled based of the values in the number columns. The specific number needed is what is filled in in column L.
I am not sure the best way to phrase this question. If my example doesn't make sense, I can try to clarify or provide more examples.
I have no idea if this can be done with fancy formulas or with a VBA macro or two. I am an excel noob.
If I've understood your question correctly you can do this with a combination of Left/Right, Index and search.
In my example images, the user inputs their value in column D, and then columns B and C use the formulea
=LEFT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
=RIGHT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
respectively
Here, the Index function returns the correct column to look at (i.e. the value chosen by the user, the Search function finds the position of the semi-colon, and the left/right functions return the values either side of the semi-colon.

Index/Match multiple columns in Excel

I have 2 sheets. Sheet 1 is set up similarly to:
Keyword Domain Rank
A Z 1
B Z 2
C Z 3
D Y 10
E Y 15
B Y 20
And sheet 2 is set up like:
Keyword (Domain Z) (Domain Y)
A 1 -
B 2 20
C 3 -
D - 10
I'm trying to have a formula that will compare the keywords in Sheet 2 with those in Sheet 1 and then return the rank that corresponds to the correct domain (that's specified in Sheet 2 for that column). I can't get any formula I use to evaluate. I've used 2 formulas so far:
=INDEX(Raw!$H$11:$H$322, MATCH(A3,IF(Raw!$D$11:$D$322=All!$B$2,Raw!$B$11:$B$322),0))
The above formula works, to a point. The problem is that it simple pulls the domain for the first instance of the keyword found, which doesn't always match the domain in the column of sheet 2. The second formula I've tried:
=INDEX(Raw!$H$11:$H$322, MATCH(B3,MATCH($C$2,Raw!$D$11:$D$322,0)))
To make the values appear in the Sheet 2 table, use the following formula:
=SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$F$1),$C$2:$C$7)
This returns 0 for non-matches - you can either format the cells to display 0 how you want, or you can use the longer/uglier:
=IF(SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$G$1),$C$2:$C$7)<>0,SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$G$1),$C$2:$C$7),"-")
To calculate the rank on the first sheet based on the data from the second sheet:
=VLOOKUP(A2,$F$2:$H$5,MATCH(B2,$G$1:$H$1,0)+1,FALSE)
For sample purposes, this just put your sheet2 data in F1:H5.
This looks for the corresponding keyword and then uses match to pick the right column. I named the columns Z and Y, but if you need Domain included that can be done as well. Note that this causes an error since there is no E defined in your second table - is that the case? If so, it can be adjusted to account for no matches as follows (assuming Excel 2007):
=IFERROR(VLOOKUP(A6,$F$2:$H$5,MATCH(B6,$G$1:$H$1,0)+1,FALSE),"Rank Not Found")
You could also use PivotTable with Keywords in rows and Domain names in columns. It seems like that would do the job and be a more robust solution.

Resources