Excel: CountIf (cell1 > cell2 AND cell3 > cell4) - excel

I need to compare two sets of two columns and find the count of number of IDs that match a certain criteria.
A B C D E
ID: ListNum: RefNum: List2Num: Ref2Num:
1 10 5 12 6
2 3 7 10 2
3 12 8 1 5
4 2 15 13 4
5 4 11 2 8
6 6 9 1 3
Let's say that the cell containing ID = "1" is A2 and it goes down to A7
I have to count the number of IDs that have a ListNum that is higher than the RefNum AND also have a List2Num that is higher than Ref2Num. Both criteria must be satisfied in order to count the ID.
I used the following formula: =COUNT(IF(B2:B7 > C2:C7) & (D2:D7 > E2:E7))
I get a value, but it's not the right count (it's taking the total for both conditions rather than only counting it once). The final answer should be 1. Any help would be appreciated, thank you!

One reason why yours didn't work: In your formula you use & to mean AND, but & actually concatenate strings.
Option 1: Array Formula
IF and AND don't work on arrays so a normal formula containing them won't work. So use an array formula instead:
You need to enter this as an array formula (you need to press control-shift-enter instead of enter when you put the formula in):
=SUM((B2:B7 > C2:C7)*(D2:D7 > E2:E7))
When it is in the cell it will display in braces to show it is an array formula. Like so:
{=SUM((B2:B7 > C2:C7)*(D2:D7 > E2:E7))}
In this formula the X>Y will return 1 or 0 for true or false. So multiplication is the same as AND and addition is the same as OR. Then (B2:B7 > C2:C7)*(D2:D7 > E2:E7) means B2:B7 > C2:C7 AND D2:D7 > E2:E7 and it returns an array of 1 and 0 which are then summed up to get the count.
Option 2: SUMPRODUCT
There is a normal function whose sole purpose is to multiply arrays together and then add them up the same way as the array formula does: SUMPRODUCT
The problem with SUMPRODUCT is that the arrays must be numbers and not logical true and false values so any of these works:
=SUMPRODUCT(--(B2:B7 > C2:C7),--(D2:D7 > E2:E7))
=SUMPRODUCT((B2:B7 > C2:C7)*1,(D2:D7 > E2:E7)*1)
=SUMPRODUCT((B2:B7 > C2:C7)+0,(D2:D7 > E2:E7)+0)
And this does not:
=SUMPRODUCT((B2:B7 > C2:C7),(D2:D7 > E2:E7))
But SUMPRODUCT is a normal function so you don't need to enter it with control-shift-enter.

Try entering this formula into cell F1:
=IF(AND(B1 > C1, D1 > E1), 1, 0)
Then just take the sum of the F column for however many rows you really have, and you should be left with the answer (which is 1 for the sample data you gave above).

If you put a simple AND formula next to your table, you can autofill that all the way down. Next, you could count the number of True values in that column (see pic). You could combine into an IF statement as Tim suggests.
The two formulas would be
"=AND(B2>C2,D2>E2)"
"=COUNTIF(G2:G7,TRUE)"
Link to picture of possible solution

Related

Writing to a new Excel column based on other columns

I have an Excel table like this shown below. There are some empty cells under each header F1, F2, F3, F4, and F5.
Path F1 F2 F3 F4 F5 NewPath
image1.png 1 0 -1 1
image2.png 1 -1 1
image3.png 1 0 1 -1 1
image4.png 1 0 0 1
image5.png 1 1 1 -1
image6.png 1 -1
image7.png -1 1 1 0
image8.png 0 1 1
I have to write the values in column 1 (under the header "Path") to the column (under the header "NewPath") only if the following conditions are satisfied:
There should be a 1 in F3 and not any other value, i.e., 0, -1, or empty field.
There should not be a 1 or -1 in any of F1, F2, F4, and F5.
Whatever cell you need to write to, you can use an if/and statement. Assuming that the word "Path" is in cell "A1" this might look like the following:
=if(and(d2=1,b2<>1,b2<>-1,c2<>1,c2<>-1,e2<>1,e2<>-1,f2<>1,f2<>-1),a2,"Incorrect Value Found")
Then, just copy down the formulas. I put in "Incorrect Value Found" for those instances that do not satisfy your conditions, but this could be anything. If you need to cut those out after the fact, you could do so by filtering, ordering them to the bottom, or a slicer on a pivot table.
Hope this helps. Let me know if I missed the mark on my understanding of the question.
Would something like
=IF(AND($D2=1,COUNTIF($b2:$f2,"<>0")>=1),$A2,"")
Work for you?
There is no match in the input data based on your requirements. I modified (highlighted cells, see screenshot below) the input data so we can have at least one match for testing purpose.
In cell G2 put the following formula:
=LET(f3Cond, $D$2:$D$9=1, f1f2f4f5Cond,
MMULT(ABS(CHOOSECOLS(B2:F9,{1,2,4,5})),{1;1;1;1})=0,
allCond, IFERROR(XMATCH(f3Cond * f1f2f4f5Cond,1),""),
MAP(allCond, A2:A9, LAMBDA(a,b,IF(a=1, b, "")))
)
and here is the output:
Explanation
We use LET to define intermediate calculations. The variable f1f2f4f5Cond represents the condition for F1, F2,F4 and F5 columns.
MMULT(ABS(CHOOSECOLS(B2:F9,{1,2,4,5})),{1;1;1;1})=0
We use CHOOSECOLS to select the specific columns from range B2:F9 we need to evaluate the conditions. We need to exclude 1, -1, so we use ABS to ensure MMULT returns the sum of all 1's per row. We can use MMULT for this purpose because the only values we have are 0,1 and empty. All the conditions are valid if the total number of 1's is 0.
Note: As an alternative you can replace the MMULT calculation with the following:
= BYROW(ABS(CHOOSECOLS(B2:F9,{1,2,4,5})),
LAMBDA(a, SUM(a)=0))
We use XMATCH to identify the rows where all the conditions are met.
IFERROR(XMATCH(f3Cond * f1f2f4f5Cond,1),"")
#N/A values are converted to empty string. The array allCond has 1 only where all conditions are met, otherwise empty string.
Finally we use a MAP function to populate the Path column value from the rows that matches the conditions, otherwise an empty string.

Sum n values in sparse dataset

I have a sparse dataset in excel, e.g.:
1 0 2 4 5 8
2 3 0 0 0 6
Zeros represent missing values.
I want to sum the first 3 nonmissing values in each row using Excel.
Thanks
For a normally entered formula, try:
=SUMPRODUCT(N(OFFSET(A1,0,AGGREGATE(15,6,1/1/(A1:K1<>0)*COLUMN(A1:K1),{1,2,3})-1)))
For a sum against EACH row, you can do as below:
Array formula - use:
Ctrl+Shift+Enter
=SUM(INDEX($A1:$F1, 1, 1):INDEX($A1:$F1, 1, SMALL(IF($A1:$F1, COLUMN($A1:$F1) - COLUMN($A1) + 1), 3)))
Image shows second row selected, but formula I typed shows first row.
I don't have Excel at home to test it, but you can try entering this formula with Ctrl+Shift+Enter:
=SUM(OFFSET(A1:F1,0,0,1,SMALL(IF(A1:F1,COLUMN(A1:F1)),3)))
The SMALL(IF(A1:F1>"",COLUMN(A1:F1)),3) part should return the index of the third non-zero cell.
If you have the latest version of Excel 2016 and you know for sure that each cell will only contain one digit, you can leverage the CONCAT Function:
=SUM(--MID(SUBSTITUTE(CONCAT(A1:F1),0,""),{1,2,3},1))

COUNTIF with IFERROR in the range

I have the following Excel spreadsheet:
A B C
1 50 20 =IFERROR(A1/B1,"") = 2.5
2 60 0 =IFERROR(A2/B2,"") = ""
3 80 0 =IFERROR(A3/B3,"") = ""
4 10 50 =IFERROR(A4/B4,"") = 0.2
5
6 COUNT: 2
7
In Cell C6 I want to count the number of items in the list which are not " " according to Column C.
I tried to do this with the following formula:
=COUNTIF(C1:C4;"<>""")
However, this formula gives me back the value 4 instead of 2.
Therefore, I guess the issue is related to the IFERROR functions which I use in Column C.
Do you have any idea how I have to change the COUNTIF formula to only count the cells without " "?
I know - in this specific case - I could solve the issue by using the COUNTIF formula over Column B. However, I would prepfer a solution that goes over Column C.
Instead of COUNTIF use SUMPRODUCT as
=SUMPRODUCT((C1:C4<>"")*1)
This can be tricky. I usually count the blanks using COUNTBLANK and take that away from the total count
`=COUNTA(D1:D4)-COUNTBLANK(D1:D4)`
Also because in this case your output is the result of a division you could use =COUNTIF(D1:D4,">-1") to count everything with a value zero or above.
Please see https://www.ablebits.com/office-addins-blog/2014/07/02/excel-countif-examples/#countif-blank
In your example, the formula could be
=COUNTIF(C1:C4;"")

Median/average does not return the right values

Image for reference
I'm trying to achieve the following:
if(cell A1 is found in list 1), for each row in which it's found and if(C4:C10 > B4:B10), then median(the subtraction between C and B values, for every row that has text1).
I've tried two 2 different formulas:
1 - {=MEDIAN(IF(AND((C4:C10>B4:B10);(B4:B10=A1));(C4:C10-B4:B10)))}
2 - {=MEDIAN((C4:C10>B4:B10)*(B4:B10=A1)*(C4:C10-B4:B10))}
For median it always returns 0 and for the average really small values that aren't accurate. I'm sure the median and the averages aren't correct.
What would the problem be?
Also, how would I use something like:
{=MEDIAN((C4:C10>B4:B10)*(B4:B10=A1)*(C4:C10-B4:B10))}
If one the columns had text in some rows? (which isn't the case for the former problem, but it has arisen before).
text1
list 1 list 2 list 3
text2 1 5
text4 2 4
text1 4 6
text4 1 6
text1 4 5
text4 2 4
text1 3 3
You can't use AND function in these type of formulas because AND returns a single result (TRUE or FALSE) not an array as required.
Your second formula is closer but by multiplying all the conditions you will get zeroes for every row where the conditions are not met, hence skewing the results.
You can use either one of these similar versions:
=MEDIAN(IF((C4:C10>B4:B10)*(A4:A10=A1);C4:C10-B4:B10))
=MEDIAN(IF(C4:C10>B4:B10;IF(A4:A10=A1;C4:C10-B4:B10)))
both need to be confirmed with CTRL+SHIFT+ENTER
To handle text in columns B or C (and to make the formula ignore those rows but work otherwise) you can add an extra IF function like this
=MEDIAN(IF(C4:C10>B4:B10;IF(A4:A10=A1;IF(ISNUMBER(C4:C10-B4:B10);C4:C10-B4:B10))))
All formulas will work equally well with AVERAGE function in place of MEDIAN
Another way to get the MEDIAN while ignoring text is to use AGGREGATE function like this:
=AGGREGATE(17;6;C4:C10-B4:B10/(C4:C10>B4:B10)/(A4:A10=A1);2)
That doesn't need "array entry" but will only work in Excel 2010 or later versions. There's no simple equivalent for AVERAGE
17 denotes QUARTILE function - second quartile is the equivalent of median
See attached screenshot demonstrating the last two formulas with your sample data....and some added text
Supposing that the values in column C that is list 3 are bigger than those in column B that is list 2, then you can use the following formula:
=MEDIAN(IF((A4:A10=A1)*(C4:C10>B4:B10);C4:C10-B4:B10))
this is an array formula, so press ctrl+shift+enter to calculate the formula.
tell me if it doesn't work.

Excell : how many times a value OR another value appear in a column

I have a column on Excel, I found out how to count how many times a value appears in it (ex: how many 1 in the column), but I can't find how to make it count how many times one OR another value appears in this column (ex: how many cells with the value 1 OR 2).
Here is an example of my colum:
A1 1;
A2 1;2;
A3 1;3;
A4 2;
A5 1;
A6 2;3;
A7 1;2;
In this column, if I want to find how many cells with the number 1 there are, I would do :
=COUNTIF(A1:A7,"1") and then the result would be : 5
But if I want to find how many cells have the number 1 OR the number 2, I can't find how to do, but I know the result is 7 (because the all of these cells have or the number 1, or the number 2)
The only way I found is to calculate the number of cells with the number 1 wich don't have the number 2, and to calculate the number of cells with the number 2 wich don't have the number 1, and add the sum of those to the number of cells with the value "1;2", wich gives me a long formula like:
=(COUNTIF(A1:A7,"1")-COUNTIFS(A1:A7,"1",A1:A7,"2"))+(COUNTIF(A1:A7,"2")-COUNTIFS(A1:A7,"2",A1:A7,"1"))+COUNTIF(A1:A7,"1;2")
Any one has a simpler formula?
Thank you so much if someone can resolve this!!
I'm a little confused by your formula, this part, for example
=COUNTIFS(A1:A7,"1",A1:A7,"2")
...can only ever return 0 because COUNTIFS works on an "AND" basis and no cell can be both = 1 and = 2 at the same time
and if the data is exactly as shown with semi colons then surely this formula
=COUNTIF(A1:A7,"1")
will return zero too because none of your cell values are exactly 1
Are you over-simplifying your data for your question? I don't see how that formula will give you a result of 7
Try this formula to count how many cells contain either 1 or 2 (or both)
=SUMPRODUCT((ISNUMBER(FIND(1,A1:A7))+ISNUMBER(FIND(2,A1:A7))>0)+0)
...of course it will also count a cell if it contains 22 or 11, do you want it to do so in that case?
You could create a vba function and reference it.
something like
Function MyOrTest(cell)
If InStr(1, cell.Value, 1) Or InStr(1, cell.Value, 2) Then
MyOrTest = 1
Else
MyOrTest = 0
End If
End Function
You will need to add it to a new Module in VBA. Using this will allow just a normal sum function.

Resources