Count rows based on data in any of 3 columns, then average numbers

toshimarise

New Member
Joined
Feb 1, 2013
Messages
21
Thank you in advance for your assistance.

I need to perform two related functions on a set of data. Here is a sample of the spreadsheet, which is actually around 500 lines long and will be growing in the future:

[TABLE="width: 500"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Case 1[/TD]
[TD]Case 2[/TD]
[TD]Case 3[/TD]
[/TR]
[TR]
[TD]Aa23[/TD]
[TD]none (24)[/TD]
[TD]none (31)[/TD]
[TD]none (14)[/TD]
[/TR]
[TR]
[TD]Bb34[/TD]
[TD]7[/TD]
[TD]none (38)[/TD]
[TD]none (45)[/TD]
[/TR]
[TR]
[TD]Jj45[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]Cc56[/TD]
[TD]4[/TD]
[TD]none (14)[/TD]
[TD]none (18)[/TD]
[/TR]
[TR]
[TD]GG67[/TD]
[TD]3[/TD]
[TD]3[/TD]
[TD]none (15)[/TD]
[/TR]
[TR]
[TD]Zz78[/TD]
[TD]none (14)[/TD]
[TD]4[/TD]
[TD]0[/TD]
[/TR]
</tbody>[/TABLE]

First, I need to count how many people (unique IDs) have a number greater than 0 in at least one of the columns, ignoring anything marked "none". In this case it should return "4".

Second, I need an average of the non-0 numbers, again ignoring anything marked "none". In this case it should return "4.2".

I am having a ridiculously hard time with this. I have tried a variety of sumproduct and countif solutions, and even one promising sumproduct(sign((etc)). But I just can't get my head around excluding text, excluding 0s, and only counting each row once, all at the same time.

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Heres one way, but shouldn't the average be 5.25 ???........7,4,3,3,4 divided by 5



Excel 2003
ABCDEF
1IDCase 1Case 2Case 3
2Aa23none (24)none (31)none (14)00
3Bb347none (38)none (45)17
4Jj4500000
5Cc564none (14)none (18)14
6GG6733none (15)26
7Zz78none (14)4014
845.25
Sheet1
Cell Formulas
RangeFormula
E2=COUNTIF(B2:D2,">0")
E3=COUNTIF(B3:D3,">0")
E4=COUNTIF(B4:D4,">0")
E5=COUNTIF(B5:D5,">0")
E6=COUNTIF(B6:D6,">0")
E7=COUNTIF(B7:D7,">0")
E8=COUNTIF(E2:E7,">0")
F2=SUMIF(B2:D2,">0",B2:D2)
F3=SUMIF(B3:D3,">0",B3:D3)
F4=SUMIF(B4:D4,">0",B4:D4)
F5=SUMIF(B5:D5,">0",B5:D5)
F6=SUMIF(B6:D6,">0",B6:D6)
F7=SUMIF(B7:D7,">0",B7:D7)
F8=SUM(F2:F7)/COUNTIF(F2:F7,">0")
 
Upvote 0
Maybe this:

Code:
First-> =SUMPRODUCT(--(MMULT(--ISNUMBER(B2:D7),ROW($1:$3))*((B2:B7>0)+(C2:C7>0)+(D2:D7>0))>0))

Second-> =AVERAGEIF(B2:D7,">0",B2:D7)

Markmzz
 
Upvote 0
A small modification:

Code:
First-> =SUMPRODUCT(--(MMULT(ISNUMBER(B2:D7)*(B2:D7>0),ROW(1:3))>0))

Markmzz
 
Upvote 0
MichaelM, thanks for your answer, but I can't add helper columns to the data. (Also your average is different because you're averaging the lines, not the individual cases.) :)

Code:
First-> =SUMPRODUCT(--(MMULT(ISNUMBER(B2:D7)*(B2:D7>0),ROW(1:3))>0))

Second-> =AVERAGEIF(B2:D7,">0",B2:D7)

Markmzz

Thanks! These work great when placed on the same sheet as the data. But the statistics actually go on a separate sheet in the workbook, and when I put the second formula (the sumproduct(etc)) in another sheet I get a #VALUE error citing a circular reference. (I cut & pasted, so it added the sheet name to the ranges automatically.)

This is the code that I put on Sheet2, which returns a #VALUE:

Code:
=SUMPRODUCT(--(MMULT(ISNUMBER(Sheet1!B2:D7)*(Sheet1!B2:D7>0),ROW($B2:$B2))>0))

You guys are great! :)
 
Upvote 0
I figured out the problem but I can't figure out how to edit my previous post. The row #s in the sumproduct(etc) change to those bizarre $-marked cells instead of 1:3. Not sure why that happens, but luckily it's an easy fix.

You would not believe how much time I spent poking at this before giving up and asking for help. I say again: you guys are great!
 
Last edited:
Upvote 0
I figured out the problem but I can't figure out how to edit my previous post. The row #s in the sumproduct(etc) change to those bizarre $-marked cells instead of 1:3. Not sure why that happens, but luckily it's an easy fix.

You would not believe how much time I spent poking at this before giving up and asking for help. I say again: you guys are great!
Hi Toshimarise.

The part of the formula ROW(1:3) is necessary in the example of the post #1 because I need three numbers (three columns of cases - B, C and D) to multiply the array ISNUMBER(Sheet1!B2:D7)*(Sheet1!B2:D7>0) and turn it into an numeric array with six rows and one column. I didn't use $ (for absolute reference - fix position in the sheet) because there was no need to copy the formula in that case (and anyway I need three numbers >0).

Markmzz
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top