Excel count rows where column B equals value and value in column A is not a duplicate - excel

I have a table as follows:
A
B
1
happy
1
sad
2
angry
2
sad
3
sad
4
moody
4
thoughtful
5
excited
I need to count the number of times "sad" appears in column B whose value in column A is NOT a duplicate.
I am aware of =COUNTIF(B2:B8, "sad") but I'm stuck there.
Our answer should be 1 since sad only occurs once without a duplicate in A.
Thank you.

Use SuMPRODUCT with a COUNTIFS():
=SUMPRODUCT((B1:B8="Sad")*(COUNTIF(A1:A8,A1:A8)=1))

Related

Excel Count rows where value in Column A has a duplicate in column B and that duplicate's other value in Column A equals something

Given the table below:
A
B
C
D
1
happy
1-veggies
GHF
1
sad
1-veggies
HGF
2
angry
1-veggies
GHG
2
sad
1-veggies
FGH
3
sad
1-veggies
HGF
4
moody
2-meat
FFF
4
sad
2-meat
HGF
5
excited
2-meat
HGF
I need to count the number of rows where C="1-veggies", B="sad", A has a duplicate within A and whose alternative rows in A have a value in D = "GHF" or "FGH".
The result should be 1 based on the second row.
The second row has B= "sad", C = "1-veggies", A has a duplicate within A, whose value is "1" and the 1st row, also with A = "1" has a value in D = "GHF".
I already have a formula as follows:
=SUMPRODUCT((B1:B8="sad")*(COUNTIF(A1:A8,A1:A8)>1)*(C1:C8="1-veggies")*(D1:D8="GHF"))
However, it isn't able to check the 1st row nor does it check the necessary OR statement on column D.
I don't know how to accomplish either task. Thank you.
Add some more COUNTIFS():
=SUMPRODUCT((B1:B8="sad")*(COUNTIF(A1:A8,A1:A8)>1)*(C1:C8="1-veggies")*(COUNTIFS(A1:A8,A1:A8,B1:B8,"<>sad",D1:D8,"GHF")+COUNTIFS(A1:A8,A1:A8,B1:B8,"<>sad",D1:D8,"FGH")>0))
Basically you want to select rows where C="1-veggies", B="sad" and join them to rows where D = "GHF" or "FGH", but with a condition that the value in column A is equal in both but the row number is unequal. This might be a good use of Power Query for users who are expert in this, but using formulas is problematic because of the limitations of countifs (has to be a range). A workaround if you have Excel 365 would be to use some filters followed by countifs:
Column E
=FILTER(A1:A8,(B1:B8="sad")*(C1:C8="1-veggies"))
Coumn F
=FILTER(ROW(A1:A8),(B1:B8="sad")*(C1:C8="1-veggies"))
Column G
=FILTER(A1:A8,(D1:D8="GHF")+(D1:D8="FGH"))
Column H
=FILTER(ROW(A1:A8),(D1:D8="GHF")+(D1:D8="FGH"))
Then
=SUM(COUNTIFS(E1#,G1#,F1#,"<>"&H1#))

How to SELECT N values ABOVE and BELOW from specific value

If I have a table:
Column A
Column B
Column C
1
Jane
10
2
Stewe
9
3
John
8
4
Mike
7
5
Luke
6
6
Andrew
5
7
Carl
4
8
Sasha
3
9
Ariel
2
10
Carol
1
I would like to SELECT 3 rows above and below WHERE Column B = someValue .
IF query SELECT * WHERE Column B = "Andrew" result should look like:
Column A
Column B
Column C
3
John
8
4
Mike
7
5
Luke
6
6
Andrew
5
7
Carl
4
8
Sasha
3
9
Ariel
2
I know how to select one row, but cant understand how to select such range.
Thanks for ideas!
You can limit and offset inside your QUERY():
=QUERY(A1:C,"limit "&2+MIN(5,MATCH(D1,B:B,0))&" offset "&MAX(0,MATCH(D1,B:B,0)-5))
Well, this was fun...
If 3 above or below are not available then blank... rolling data around is a different proposition.
Below the image is the list of formulae used.
So, per cell not including the data validation that is based on cells B2:B11
A14 and dragged down:
=IFERROR(INDEX($A$2:$A$11,MATCH(B14,$B$2:$B$11,0)),"")
C14 and dragged down:
=IFERROR(INDEX($C$2:$C$11,MATCH(B14,$B$2:$B$11,0)),"")
Cells B14 through B20:
=IFERROR(IF(MATCH(B$17,$B$2:$B$11,0)=3,NA(),INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)-3)),"")
=IFERROR(IF(MATCH(B$17,$B$2:$B$11,0)=2,NA(),INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)-2)),"")
=IFERROR(IF(MATCH(B$17,$B$2:$B$11,0)=1,NA(),INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)-1)),"")
=E2
=IFERROR(INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)+1),"")
=IFERROR(INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)+2),"")
=IFERROR(INDEX($B$2:$B$11,MATCH(B$17,$B$2:$B$11,0)+3),"")
In Excel 365, you could try:
=INDEX(A:C,MAX(2,MATCH(D2,B:B,0)-3),0):INDEX(A:C,MIN(COUNTA(B:B),MATCH(D2,B:B,0)+3),0)
In Google sheets, on the other hand, the formula would be:
=INDEX(A:C,MAX(2,MATCH(D2,B:B,0)-3),0):INDEX(A:C,MIN(COUNTA(B:B),MATCH(D2,B:B,0)+3),0)
(spot the difference).
Excel
Google Sheets
This should produce what you want in all cases:
=IFERROR(FILTER(A2:C,B2:B<>"",ROW(A2:A)>=VLOOKUP("Andrew",{B2:B,ROW(B2:B)},2,FALSE)-3,ROW(A2:A)<=VLOOKUP("Andrew",{B2:B,ROW(B2:B)},2,FALSE)+3))
Of course, you can replace the two instances of "Andrew" with a cell reference (one where you type a changeable name).
This just looks up the row in a curly-bracket array formed from the names and row numbers and uses FILTER to keep results to rows between +/-3 rows of where the target name is found. If you choose the first name (or any other name), you won't get an error; because even if the target name were on Row 1 and the formula goes looking for anything "greater than or equal to 1 minus 3, all rows will be greater than a negative number. Same on the high end. You just won't get a full seven names if there aren't at least three other rows prior to or after the target row.
this not the best solution but it will work , you can use a helper column 'D' that contains the following formula =if(countif(INDIRECT("B"&ROW()+3&":"&"B"&ROW()-3),"Andrew")>0,TRUE,FASLE)
and u can query from here like this SELECT * WHERE Column D = TRUE

EXCEL: Color cell if column is "nth" in the row

Here is an example excel table
Row# A B C D E F G
1 Q# Ans Student Answers
2 1 4 0 3 1 4 2
3 2 2 1 7 9 2 0
4 3 3 5 1 1 3 8
Column A stores the question number, Column B is the index of the correct scantron answer (1-5 for A-E).
Columns C-G are sums of the student's answers and how many of them answered each option, so for example:
Question 2, the correct answer was B (because column B is stored as
2). C-G say that 1 student answered A, 7 answered B, 9 answered C, 2
answered D and none selected E on the scantron.
With each exam, the values in column B change around. I would like a way to conditionally format columns C-G so that only the nth column is colored (as dictated by whatever the number of A is). So for question 2, it would color the cell at D3 green or something.
I tried to figure this out with conditional formatting, but I can't say if the values match column B, because B is a indicator for which column is correct, not a value to match in each column.
I'm comfortable implementing VBA code, so that works as well, I'm just not familiar enough with it to know how to construct the proper code to insert into my spreadsheet.
Put the answer number accross the top of the data in row 1:
Then you can use the following formula:
=MATCH($B2,$1:$1,0)=COLUMN(C2)
And apply it to the answer statics area, in this case $C$2:$G$4
Or if your answer statistics always follow the Ans column you could use this formula for the same result:
=COLUMN($B2)+$B2=COLUMN(C2)
Without the need of the title row.

Rank only values in a column that are numbers

I have several columns that I would like to derive a ranking for. The original, unranked values are, for instance:
A B
1 2
3 5
6 4
5 #N/A
4 0.1
The ranking would be, through the function =RANK(A*,A:A,0):
A B
5 3
4 1
1 2
2 #N/A
3 4
If the value in the same row in column B is #N/A, then I would like to exclude the value in the same row from column A, i.e., it should omit the value "5" from the original column and then just rank the remaining 4 values. The result should be:
A B
4 3
3 1
1 2
#N/A #N/A
2 4
I tried several if statements, but they always fail to remove the values from the array against which they are ranked.
How can I calculate the rank in column a with the same amount of values as column B?
If you add one column where you can remove that number with IF="#N/A"first then you can sort the new list with =RANG.EQ(C1;C:C), but I am assuming you have that error in the column.
If you import any number you could add the IF when u import the numbers.
Hope that helps
Thanks, Krib. I ended up inserting some helper rows with the intermediate step, per your suggestion. I was hoping to do it in one go. Anyway, task accomplished, albeit less elegantly than I wished.

Numbering/sequencing sets of same column values

How to do numbering/sequencing for sets of same column values? For example:
Col1 Col2
Andy 1
Chad 1
Bill 1
Andy 2
Bill 2
Bill 3
Chad 2
Bill 4
Since Andy got 2 values, I want to number it 1 and 2 in Column 2. For Bill, I want to number it 1, 2, 3 and 4 and so on.
You can accomplish this with countif and a sliding range :
A B
1 val1 =COUNTIF($A$1:A1, A1)
2 valx =COUNTIF($A$1:A2, A2)
and so on.
The formula in column B can be dragged down / autofilled in the column. It anchors to the start of the range and only looks as far down as the value we are numbering; COUNTIF is tallying up the matching values in the preceding set this way.
That is kind of slow when your list is really long. I've found sorting the column A to Z or small to larger and then using this formula is much faster:
=IF(A2=A1, A1+1,1)
Basically
if the value above is the same then add one to the count else start over at 1

Resources