Is there a way to count when a variable appears with (and without) another in excel? - 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))))

Related

Formula to get crossed values

I am trying to get values from other column, based on some logic, crossed logic, but I didn't get any result with the only thing I know: IF or VLOOKUP.
The table is in this way:
So basically there are groups divided by blank row, and in column D I want to add the value from column B, but according with other letter that the one from the row ( C is from Credit, D is from Debtor). So in first D5 I have C on the same row in A5, so I need to take value from D, which is 2, according to B6.
So sorry if this question is too easy or stupid, but I don't know much about excel formulas, other that the basic one.
I'd say the fastest way with formula takes two columns. The first one (let's say column C) will use this formula to define the blocks:
=IF(A5="",C4+1,IFERROR(C4+0,0))
It's meant for cell C5.
The second column will give back the actual result:
=SUMIFS(B:B,A:A,IF(A5="C","D",IF(A5="D","C","")),C:C,C5)
It's meant for cell D5.
Place them accordingly and drag them down to cover your list.
Then again: if you are looking just for the number and not for its sum (and therefore assuming all numbers are equal for each letter in each block), just use this one in cell D5 instead of the previous one:
=SUMIFS(B:B,A:A,IF(A5="C","D",IF(A5="D","C","")),C:C,C5)/COUNTIFS(A:A,IF(A5="C","D",IF(A5="D","C","")),C:C,C5)
Now if you were to use only IF and VLOOKUP functions, a possible solution could be this one:
A
B
C
D
E
F
G
Block index
0
What to search
What is
Value
Result
C
1
=IF(A5="",C4+1,C4)
=IF(A5="",C4+1,C4)&SE(A5="C","D",SE(A5="D","C",""))
=C5&A5
=B5
=IF(VLOOKUP(D5,E:F,2,FALSO)=0,"",VLOOKUP(D5,E:F,2,FALSO))
D
2
=IF(A6="",C5+1,C5)
=IF(A6="",C5+1,C5)&SE(A6="C","D",SE(A6="D","C",""))
=C6&A6
=B6
=IF(VLOOKUP(D6,E:F,2,FALSO)=0,"",VLOOKUP(D6,E:F,2,FALSO))
D
2
=IF(A7="",C6+1,C6)
=IF(A7="",C6+1,C6)&SE(A7="C","D",SE(A7="D","C",""))
=C7&A7
=B7
=IF(VLOOKUP(D7,E:F,2,FALSO)=0,"",VLOOKUP(D7,E:F,2,FALSO))
=IF(A8="",C7+1,C7)
=IF(A8="",C7+1,C7)&SE(A8="C","D",SE(A8="D","C",""))
=C8&A8
=B8
=IF(VLOOKUP(D8,E:F,2,FALSO)=0,"",VLOOKUP(D8,E:F,2,FALSO))
D
3
=IF(A9="",C8+1,C8)
=IF(A9="",C8+1,C8)&SE(A9="C","D",SE(A9="D","C",""))
=C9&A9
=B9
=IF(VLOOKUP(D9,E:F,2,FALSO)=0,"",VLOOKUP(D9,E:F,2,FALSO))
C
4
=IF(A10="",C9+1,C9)
=IF(A10="",C9+1,C9)&SE(A10="C","D",SE(A10="D","C",""))
=C10&A10
=B10
=IF(VLOOKUP(D10,E:F,2,FALSO)=0,"",VLOOKUP(D10,E:F,2,FALSO))
=IF(A11="",C10+1,C10)
=IF(A11="",C10+1,C10)&SE(A11="C","D",SE(A11="D","C",""))
=C11&A11
=B11
=IF(VLOOKUP(D11,E:F,2,FALSO)=0,"",VLOOKUP(D11,E:F,2,FALSO))
D
2
=IF(A12="",C11+1,C11)
=IF(A12="",C11+1,C11)&SE(A12="C","D",SE(A12="D","C",""))
=C12&A12
=B12
=IF(VLOOKUP(D12,E:F,2,FALSO)=0,"",VLOOKUP(D12,E:F,2,FALSO))
C
5
=IF(A13="",C12+1,C12)
=IF(A13="",C12+1,C12)&SE(A13="C","D",SE(A13="D","C",""))
=C13&A13
=B13
=IF(VLOOKUP(D13,E:F,2,FALSO)=0,"",VLOOKUP(D13,E:F,2,FALSO))
C
5
=IF(A14="",C13+1,C13)
=IF(A14="",C13+1,C13)&SE(A14="C","D",SE(A14="D","C",""))
=C14&A14
=B14
=IF(VLOOKUP(D14,E:F,2,FALSO)=0,"",VLOOKUP(D14,E:F,2,FALSO))

Return Multiple Unique Matches in Excel without Array Formula

Given an Excel table of shape
Col. A Col B Col. C Col. D Col. E
x 2 x 2 3
x 3 y 7
y 7 z -5
x 2
z -5
I want to return the first unique hit in Column B for argument "x" in Column D,
the second unique hit in Column B for argument "x" in Column E and so forth.
The formula I'm currently using in cell D1 for this is
{=IFERROR(INDEX($B$1:$B$5,MATCH(0,COUNTIF($C1:C1,$B$1:$B$5)+($A$1:$A$5<>$C1),0)),"")}
which is working.
The problem I'm having is that since this is an array formula and since I'm analyzing a decent amount of data computation time for my sheet is too high.
Is there an alternative for this functionality avoiding an array formula?
Thanks!
Haven't got time to test this properly, but if you have Excel 365 you can use a single formula per row and it may be faster:
=TRANSPOSE(UNIQUE(FILTER(B1:B10,A1:A10=C1)))
in D1.
EDIT
To pull the formula down, you need static references as OP has pointed out. Probably should check for empty cells in column C as well, so formula becomes:
=IF(C1="","",TRANSPOSE(UNIQUE(FILTER(B$1:B$10,A$1:A$10=C1))))

Calculate top-3 scores, with specific exclusions in Excel

I need to find the average of the top 3 values within a range, so have been using the following:
=IFERROR(SUM(LARGE(A2:F2,{1,2,3}))/3,Z2)
on a similar dataset to:
A B C D E F G
2 10 3 9 4 3 3
However, I now need to exclude either certain columns throughout that range (i.e. B, D, E etc), or to exclude some specific values from that range (whichever way is more efficient).
Any help would be appreciated
Here is something you may be able to work with:
In A3:
{=AVERAGE(LARGE(A1:H1,ROW(1:3)))}
In A4: (excluding B, D and E)
{=AVERAGE(LARGE((A1;C1;F1:H1),ROW(1:3)))}
Notice it's an array formula and needs to be confirmed through CtrlShiftEnter

average specific values in column

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.

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:

Resources