How can I do a simple summation of data that meets a certain condition? Say i have the data
1 2 3 4
5 6 -7 8
for finding the average I can just type in a cell =AVERAGE(A1:B4), but how can I find the average of only the positive numbers into a block of data?
I realize the IF statement would probably be used here, but how? Thanks for any help
You can use the AVERAGEIF function of Excel. The documentation is provided here. If your data was in cells A1:D2, the following function would only average the data that was above zero:
=AVERAGEIF(A1:D2,">0")
If you want to use multiple criteria, then you can use the AVERAGEIFS function, which is similar but allows multiple conditions. The documentation is provided here. The only catch is you have to define the range to average, which in your case could just be the same range. If your data was in cells A1:D2, the following function would only average the data that was both above zero and less than or equal to 6:
=AVERAGEIFS(A1:D2,A1:D2,">0","<=6")
Try: =AVERAGEIF(A1:B4;">0";A1:B4)
Works in excel 2013.
Related
Suppose you have an ordered, indexed list of positive values. These positive values are interrupted by 0 values. I want to determine if a consecutive sub-array exists which is not interrupted by 0 values and whose sum exceeds a certain threshold.
Simple example:
Index, Value
0 0
1 0
2 3
3 4
4 2
5 6
6 0
7 0
8 0
9 2
10 3
11 0
In the above example, the largest consecutive sub-array not interrupted by 0 is from index 2 to index 5 inclusive, and the sum of this sub-array is 15.
Thus, for the following thresholds 20, 10 and 4, the results should be FALSE, TRUE and TRUE respectively.
Note I don't necessarily have to find the largest sub-array, I only have to know if any uninterrupted sub-array sum exceeds the defined threshold.
I suspect this problem is a variation of Kadane's algorithm, but I can't quite figure out how to adjust it.
The added complication is that I have to perform this analysis in Excel or Google Sheets, and I cannot use scripts to do it - only inbuilt formulas.
I'm not sure if this can even be done, but I would be grateful for any input.
Start with
=B2
in c2
then put
=IF(B3=0,0,B3+C2)
in C3 and copy down.
EDIT 1
If you were looking for a Google sheets solution, try something like this:
=ArrayFormula(max(sumif(A2:A,"<="&A2:A,B2:B)-vlookup(A2:A,{if(B2:B=0,A2:A),sumif(A2:A,"<="&A2:A,B2:B)},2)))
Assumes that numbers in column B start with zero: would need to add Iferror if not. It's basically an array formula implementation of #Gary's student's method.
EDIT 2
Here is the Google Sheets formula translated back into Excel. It gives you an alternative if you don't want to use Offset:
=MAX(SUMIF(A2:A13,"<="&A2:A13,B2:B13)-INDEX(SUMIF(A2:A13,"<="&A2:A13,B2:B13),N(IF({1},MATCH(A2:A13,IF(B2:B13=0,A2:A13))))))
(entered as an array formula).
Comment
Maybe the real challenge is to find a formula that works both in Excel and Google sheets because:
Vlookup doesn't work the same way in Excel
The offset/subtotal combination doesn't work in Google sheets
The index/match combination with n(if{1}... doesn't work in Google sheets.
With data in columns A and B, insure column B end with a 0. Then in C2 enter:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-MAX($C$1:C1),"")
and copy downwards:
Column C lists the sums of consecutive non-zeros. In another cell enter something like:
=MAX(C:C)>19
where 19 is the criteria value.
You can avoid the "helper" column by using a VBA UDF.
EDIT#1:
Use this instead:
=IF(AND(B3=0,B2<>0),SUM(B$1:$B2)-SUM($C$1:C1),"")
Thanks to #Tom Sharpe and #Gary's Student for answering the question.
While I admittedly did not specify this in the question, I would prefer to achieve the solution without a helper column because I have to do this operation on 30+ successive columns. I just didn't think it was possible in Excel.
Full credit goes to user XOR LX on the Excelforum for coming up with this solution. It has blown my mind and took me the better part of an hour to wrap my head around, but it is certainly very creative. There is no way I could have come up with it myself. Re-posting it here for the benefit of everyone who is looking into this.
Copy and paste the table from my initial question into an empty Excel sheet such that the headers appear in (A1:B1) and the values appear in (A2:B13).
Then enter this formula as an array formula (ctrl+shift+enter), which gives the max of the sums of all the uninterrupted sub-arrays:
=MAX(SUBTOTAL(9,OFFSET(B2,A2:A14,,-FREQUENCY(IF(B2:B13,A2:A13),IF(B3:B14=0,A2:A13,0))-1)))
Note the deliberate offset to include one additional row below the end of the dataset.
DESPERATE for any help :-)
I need to exclude 0 values from my =COUNT excel formula in order to get an automated frequency value feeding another formula in my model.
Actual Base Formula:
=COUNT(J10,J11,J12,N10,N11,N12,J14,J15,J16,N14,N15,N16)
I am trying to get the formula to NOT count when any of these cells have O values. For instance, if J10 is greater than 0, include it in the count.
I have tried several different "IF" statements, but am failing miserably!
I basically need to dummy proof this by automating it versus having users simply count the cells with values and enter the number into the frequency field for my score calculator. Thanks so much for any help!!
Since your range is disjointed the best method is four(4) countifs:
=COUNTIF(J10:J12,">0")+COUNTIF(N10:N12,">0")+COUNTIF(J14:J16,">0")+COUNTIF(N14:N16,">0")
Fixed based on the comments.
=COUNTIF(J10:J12,">0")+COUNTIF(N10:N12,">0")+COUNTIF(J14:J16,">0")+COUNTIF(N14:N16,">0")
I want to do some calculations, but I dont know how I can implement the idea. Given the example below:
I have two numeric values (here: 1960 and 1965).
Based on these two numeric values, in the column "H" all the years should be marked as a range (i.e. H2:H6). And based on this range, excel should take the value from the column polcon_REV and apply the average-function to it.
Does anyone have an idea how to realize that?
This solution worked for me (MITTELWERTWENNS = GERMAN FOR AVERAGEIF)
=(MITTELWERTWENNS($F:$F;$D:$D;">="&$H$3;$D:$D;"<="&$I$3;$C:$C;J3))
The last criteria checks a meanhwile introduced third criteria for the different countries for which i have to calculate the average.
Try AverageIf function
=AVERAGEIF(H1:H36000,$L$3,J1:J36000)
Let us know how you get on
I am using a VLOOKUP to find three values in a table, that might or might not be there. I want to take average of all values that are found - between 0 to 3 (inclusive) values.
This is the formula I'm using now:
=VLOOKUP(AT13,$A$18:$B$32,2,FALSE)+VLOOKUP(AT14,$A$18:$B$32,2,FALSE)+VLOOKUP(AT15,$A$18:$B$32,2,FALSE))/3
I have researched the problem and looks like AVERAGEIF looks interesting, however it takes a range of cells as an argument and I want to have three lookup results there.
As long as AT13, AT14 and AT15 are found once each at most in the lookup range you could use SUMIF function divided by COUNTIF
=SUMPRODUCT(SUMIF($A$18:$A$32,AT13:AT15,$B$18:$B$32))/MAX(1,SUMPRODUCT(COUNTIF($A$18:$A$32,AT13:AT15)))
If there are no values found then MAX function ensures the denominator is 1 to avoid #DIV/0! error and give zero as the result in that case
I'm looking for advice on how to approach a certain problem.
In short, I have a matrix with two descriptive columns to the left (Y-axis) and a range of durations/time periods on the X-axis. The first column include bond types, and the second currency. There are 6 kinds of bonds, and 4 currencies. Every combination can have one of four durations.
The problem is that I recieve the mentioned matrix weekly, and the number of rows is different each time because only non-blank rows are reported. Roughly half of them are blank (no activity since last week)
You don't need a vLookup() formula for this. A simple SumProduct() formula will help you :) See the Snapshot for an example
FORMULA
=SUMPRODUCT((A2:A15="AAA")*(B2:B15="Dollar")*(C2:F15))
SNAPSHOT
Do let me know if this is not what you wanted?
HTH
Sid
To find a value in Excel based on both a column and row value, you will need to use both a vlookup and a match function.
=VLOOKUP($A10,$A$1:$E$6,MATCH($B10,$A$1:$E$1,0),0)
Have a look at the example picture below.
![Example for Lookup & Match Formula][1]
http://pzy.be/v/2/LookupMatch.jpg
B##la