average specific values in column - excel

I have two columns in an Excel spreadsheet that look like:
Key Values
f 1
f 2
u 3
g 4
g 5
h 6
h 7
j 8
j 9
k 10
k 11
k 12
Is it possible to create formula which creates an average that uses values that are associated with a specific key? For example if I only wanted to include values for f,u,h, and k only (where the average calculated by hand is: 6.5)?
Unfortunately, I can't use VBA in this instance.

As mentioned by Brad, just use AverageIf() worksheet function:
(In my example I started the first 'f' in cell B2)
=AVERAGEIF(B2:B13;"=f";C2:C13)

Adding all the criteria the provided links show could be quite tedious, here is a way that MAY be simpler. So just as an option:
=SUM(SUMIFS($B$1:$B$12,$A$1:$A$12,{"f","u","h","k"}))/SUM(COUNTIFS($A$1:$A$12,{"f","u","h","k"}))
No need to enter as array formula, which should be an advantage.

Related

how to average first n numbers in ms excel

I have two columns in an Excel spreadsheet that look like:-
Key Values
f 1
f 2
u 3
g 4
g 5
h 6
h 7
j 8
j 9
k 10
k 11
k 12
I want to create apply formula which creates an average of first n numbers in ms excel.
I Try this:-
=AVERAGE(B:B,10)
but could not get the answer.
Please help me for give me appropriate answer.
Avoid volatile set-ups with OFFSET or INDIRECT where possible:
=AVERAGE(B2:INDEX(B:B,n+1))
Or, as pointed out by #JvdV:
=AVERAGE(TAKE(B:B,n+1))
Replace n as desired.
in cell E1: enter the number of rows to take into account for average
in cell F1: enter ="B1:B"&E1
in cell E4: enter =AVERAGE(INDIRECT(F1))
Rgds
Frederik

Is there a way to count when a variable appears with (and without) another in excel?

I am trying to count in Excel how many times "A" appears with (and then without) "C" horizontally when they are not in the same location every time. Does anyone know an Excel formula that would work? Here is an example of my data.
1
2
3
4
5
A
B
C
D
E
A
B
C
D
F
E
G
F
H
L
E
B
C
H
F
A
I
J
K
H
Thanks!
To get the number of rows that have both A and C, assuming that each can only be in the row one time each:
=SUM(--(MMULT((A1:E5="A")+(A1:E5="C"),TRANSPOSE(COLUMN(A1:E5)^0))>1))
This may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode on older versions.
To get the total without we add COUNTIF(A1:E5,"A") and sutract the formula above:
=COUNTIF(A1:E5,"A")-SUM(--(MMULT((A1:E5="A")+(A1:E5="C"),TRANSPOSE(COLUMN(A1:E5)^0))>1))
Same caveat about Ctrl-Shift-Enter.
If the values can be present multiple times per row then we need to do:
=SUM(--((MMULT(--(A1:E5="A"),TRANSPOSE(COLUMN(A1:E5)^0))>0)+(MMULT(--(A1:E5="C"),TRANSPOSE(COLUMN(A1:E5)^0))>0)>1))
Again Older versions need to use Ctrl-Shift-Enter.
Admittedly, chances are you won't have access yet to the following functions. But this could be a nice usecase for BYROW():
Formula in I1 to count rows with 'A' and 'C':
=SUM(BYROW(A1:E5,LAMBDA(x,--(COUNTIF(x,"A")*COUNTIF(x,"C")))))
Or:
=SUM(--BYROW(A1:E5,LAMBDA(x,SUM(--(UNIQUE(x)={"A","C"}))=2)))
Formula in I2 to count rows with 'A' but without 'C':
=SUM(BYROW(A1:E5,LAMBDA(x,COUNTIF(x,"A")*(COUNTIF(x,"C")=0))))

EXCEL: count combinations for unique values

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:

Count data if row is true excel

I have this Data:
var number
a 1
a 4
a 30
b 4
b 50
b 6
b 4
ab 1
I need to find the sum of each time a (var occurs X the number next column right) in excel.
In the case above the answer would be:
a = 36
b = 65
How can I write a formula for this in excel? Can I do this in excel?
Since you are counting the last one as well, I would use this formula:
=SUMIF(A:A,"*"&D2&"*",B:B)
Here, there is the value a in cell B2 and I can drag the formula down for B instead of typing a whole new formula. Handy if you have many like those to look for.
I assume that you have the table you published above in cells A1:B8. In order to do the calculating you would like to do you need to use the array-functionality in Excel. The following syntax in a cell will do:
=SUM(IF("a"=A1:A8,B1:B8))
However, instead of ENTER you need to press COMMAND+SHIFT+ENTER (I am on a Mac so it might be that Windows has a slightly different key combo. The result is that the formula now reads like:
{=SUM(IF("a"=A1:A8,B1:B8))}
This will give you 35 and not 36, but in a similar manner you could sum all the columns with 'ab' and then add that up to the sum of the 'a' columns.
Good luck!

How do transform a "matrix"-table to one line for each entry in excel

I have something like
1 2 3
a x o x
b x x o
c o o o
and want to transform it into lines like
1 a x
1 b x
1 c x
2 a o
2 b x
2 c o
3 a x
3 b o
3 c o
by using a formula in the excel document. Playing with $ for assigning values for each row and column does give me proper results. Each time I have to do some manual changes to the formula.
Any hint how to write it the right way?
Suppose that your matrix in the cells A1:D4
in A6 put:
=OFFSET($A$1;0;QUOTIENT(ROW()-ROW($A$6);3)+1)
in B6 put:
=OFFSET($A$1;MOD(ROW()-ROW($A$6);3)+1;0)
in C6 put:
=VLOOKUP($B6;$A$1:$D$4;MATCH($A6;$A$1:$D$1;0);FALSE)
Den drag down (copying formulas) A6:C6 up to A14:C14
(I translate my formulas from italian so there could be some glitch)
PS: 3 in the formulas refers to the number of rows (and column) of the example.
I know that it was answer years ago, BUT there is a much easier way to flatten the pivot table. This is also called the unpivot or reverse pivot.
see this:
https://www.youtube.com/watch?v=N3wWQjRWkJc
or this:
https://www.youtube.com/watch?v=pUXJLzqlEPk&list=LLzMcMocJLlJOCteGbfN3xvA&index=2

Resources