Excel SUMIF criteria within range of numbers - excel

I was wondering how to represent the criteria argument in the function =SUMIF(range, criteria) as instead of ">0" which represent greater than zero, which would add all numbers in the range that are greater than zero. I was wondering how to make it within the range of zero to 8, so "8>0", or something, but I have been googling for hours and cannot find a solution that doesn't involve doing whacky things with SUMIFS which involves other arrays which I do NOT want to get into because I feel there's a simple solution to this that I'm missing...
Theres ">=NUM" "<=NUM" ">NUM" and "<.NUM"
how do you make it require two of these?
Is there any documentation on this anywhere?

I agree with #user3240704 that SUMIFS is the way to go.
If you insist on using SUMIF only then you can use the following logic:
take the sum of the entire range
deduct the sum of values <-10
further deduct the sum of values >0
Which is the inverse of saying
only the sum the values >=-10 and <=0
The formula is:
=SUM(A1:A11)-SUMIF(A1:A11,">0",A1:A11)-SUMIF(A1:A11,"<-10",A1:A11)
E.g.

More info here
=SUMIFS(A1:A11,A1:A11,">=-10",A1:A11,"<=0")
SUMIFS has many (in this case two) conditions, broken down as follows:
=SUMIFS(A1:A11 - SUM() whatevers in A1:A11
, - That match the following conditions
A1:A11,">=-10" - ALL numbers in A1:A11, that are greater than OR equal to -10
, - AND
A1:A11,"<=0" - ALL numbers in A1:A11, that are less than OR equal to 0
)

Related

Excel, AVERAGEIFS, Date discrepancy

I would like the average of Column B based on two criteria. That it happened last year and a text criteria from another column. Average by year In the example I have a Year column for test purposes but I don't want to add it to all the data sheets.
=AVERAGEIFS(Table1[Unit], Table1[Date], "="&YEAR(TODAY())-1, Table1[Text], "Up")
throws a DIV/0 error.
I believe I need to define the Date range by year.. like (YEAR(Table1[Date]) but it doesn't work.
=AVERAGEIFS(Table1[Unit], (YEAR(Table1[Date]), "="&YEAR(TODAY())-1, Table1[Text], "Up")
I can get an IF statement to work on a single cell but is there are way to get this to work in a column?
Scott
You can't use formula when defining range so you either have to use helper column or something like this:
=AVERAGEIFS(Table1[Unit],Table1[Date],">="&(DATE(YEAR(TODAY())-1,1,1)),Table1[Date],"<="&(DATE(YEAR(TODAY())-1,12,31)),Table1[Text],"Up")
It checks if date is less than 2022/12/31 (DATE(YEAR(TODAY())-1,12,31))and more than 2022/01/01 (DATE(YEAR(TODAY())-1,1,1))
Result ((3+7)/2=5):
The SUMPRODUCT() function provides some really useful approaches to problems like this.
In your sample data, the formula =YEAR(Table1[Date])=2023 should return an array {FALSE,FALSE,TRUE,TRUE,FALSE,TRUE}.
In your sample data, the formula =Table1[Text]="Up" should return an array {TRUE,FALSE,FALSE,TRUE,FALSE,FALSE}.
SUMPRODUCT() allows us to do some interesting things with those:
If I apply a math operation to those arrays Excel automatically converts them to binary; {0,0,1,1,0,1} and {1,0,0,1,0,0} respectively. That math function can be doing something like multiplying. Or if I want to use them as-is in a function I can just use "--" to force a math operation, that makes them negative and back to positive. In our example we'll be multiplying the arrays so Excel will take care of it for us.
I can do a binary AND operation on the two arrays by using multiplication. Thus: = (YEAR(Table1[Date])=2023) * (Table1[Text]="Up") is actually {0,0,1,1,0,1} * {1,0,0,1,0,0} which in turn equals {0,0,0,1,0,0}. And the 1's in this result array represent the rows that meet both criteria.
=SUMPRODUCT((YEAR(Table1[Date])=2023) * (Table1[Text]="Up")) will equal the count of rows that met both criteria. Which is only 1 in your example.
=SUMPRODUCT((YEAR(Table1[Date])=2023) * (Table1[Text]="Up" * Table1[Unit])) is going to sum the result of array multiplication of {0,0,0,1,0,0} * {4,4,5,5,4,4}. In your sample data that results in 5.
So the formula =SUMPRODUCT((YEAR(Table1[Date])=2023) * (Table1[Text]="Up") * Table1[Unit]) / SUMPRODUCT((YEAR(Table1[Date])=2023) * (Table1[Text]="Up")) actually is "sum of rows that matched" divided by the "count of rows that matched".
Notice that a conditional array like Table1[Text]="Up" MUST be wrapped in its own parenthesis before it can be added (OR function) or multiplied (AND function) with another array.
You may want to wrap that entire formula in an IFERROR() function so you can display a friendlier message when the count is zero. For instance:
=IFERROR(SUMPRODUCT((YEAR(Table1[Date])=2023) * (Table1[Text]="Up") * Table1[Unit]) / SUMPRODUCT((YEAR(Table1[Date])=2023) * (Table1[Text]="Up")),"None")
You will want to fully debug the formula before nesting it in IFERROR() because the IFERROR() function will conceal other errors than just an occasional divide by zero.
This will all seem very cumbersome the first few times you use this approach but if you encounter these kinds of criteria problems often in excel, I promise that taking the time to understand SUMPRODUCT() on logical arrays will pay long-term dividends. Once understood it gives you a robust capability to use SUM, COUNT, and AVERAGE given multiple criteria, that can be any mix of AND and OR criteria.

Excel merge two lists

I have two Excel lists:
One extensive with 20 thousand lines. In which:
Two columns are important: First: Unique ID, Second: a value (number formatted).
It can be a value that appears several times, or only once.
I have to create the second list. In this list I have only one column of values that I would like to have.
I need a formula that will look for values from List 2 in List 1 and then match a Unique ID to each value.
It is important that, when no direct value exist. In this case it has to search for a sample which is in about 3-5% value deviation.
Example: there was no value 127, but within 3%, 125 was found.
I've tried indexing and comparison, but it does not seem to work.
VLOOKUP worked, but without 3-5% deviation
I am very grateful for the help.
Example: http://www.filedropper.com/excellist1and2
If the value exists in the list, you can use VLOOKUP or INDEX(MATCH to find it - that's the easy part. If the value is not in the list, then you need to find the nearest value.
The nearest "low" value will be the MAX value ≤ our input, and the nearest "high" value will be the MIN value ≥ our input.
If you have Office 365, you can use MINIFS($D$1:$D$6,$D$1:$D$6,">="&B1,$D$1:$D$6,"<="&(B1*1.05)) and MAXIFS($D$1:$D$6,$D$1:$D$6,"<="&B1,$D$1:$D$6,">="&(B1*0.95)) )` here. If not, you'll need an Array Formula, we can build that "±5%" in early, to simplify the formula.
Starting with the Low values, we want the MAX value ≤ our input and ≥ 95% of our input. Putting an Array Formula in a SUMPRODUCT so that we can use it in a normal formula, we get =SUMPRODUCT(MAX($D$1:$D$6*--($D$1:$D$6<=B1)*--($D$1:$D$6>=(B1*0.95))))
The High values are slightly harder, because we can't just multiply be 0 to cancel out anything too low, or over 105% of the target. We need to add a huge number like 1E+99 (a 1 with ninty-nine 0s after it) instead, so that the MIN will ignore them: SUMPRODUCT(MIN($D$1:$D$6+1E+99*(--($D$1:$D$6<B1)+--($D$1:$D$6>(B1*1.05)))))
The last steps are to decide which of these numbers is closer to the target, and then to find the Unique ID to match. The %closeness calculations are (TARGET - LOW)/TARGET and (HIGH - TARGET)/TARGET), and subtracting one from the other gives you 2-(HIGH + LOW)/TARGET - a Positive number means "High" is closer, a Negative number means that "Low" is closer, and 0 means they are both the same distance (I'll default this to the Low number). We then use SIGN to change it to ±1, add 2 to get 1,2 or 3 and finish up with CHOOSE to output our number. In pseudo-code, CHOOSE(2+SIGN(2-(HIGH+LOW)/TARGET),LOW,LOW,HIGH), and the full thing:
CHOOSE(2+SIGN(2-(SUMPRODUCT(MAX($D$1:$D$6*--($D$1:$D$6<=B1)*--($D$6>B1*0.95)))+SUMPRODUCT(MIN($D$1:$D$6+1E+99*(--($D$1:$D$6<B1)+--($D$1:$D$6>(B1*1.05))))))/B1),SUMPRODUCT(MAX($D$1:$D$6*--($D$1:$D$6<=B1)*--($D$6>B1*0.95))),SUMPRODUCT(MAX($D$1:$D$6*--($D$1:$D$6<=B1)*--($D$6>B1*0.95))),SUMPRODUCT(MIN($D$1:$D$6+1E+99*(--($D$1:$D$6<B1)+--($D$1:$D$6>(B1*1.05))))))
Now, we have a number. All we need to do is either use VLOOKUP, or use MATCH to get the row it is on, and INDEX to pull the data for that row:
Office 365:
=IFERROR(VLOOKUP(B1,$D$1:$E$6,2,FALSE),VLOOKUP(CHOOSE(2+SIGN(2-(MAXIFS($D$1:$D$6,$D$1:$D$6,"<="&B1,$D$1:$D$6,">="&(B1*0.95))+MINIFS($D$1:$D$6,$D$1:$D$6,">="&B1,$D$1:$D$6,"<="&(B1*1.05)))/B1),MAXIFS($D$1:$D$6,$D$1:$D$6,"<="&B1,$D$1:$D$6,">="&(B1*0.95)),MAXIFS($D$1:$D$6,$D$1:$D$6,"<="&B1,$D$1:$D$6,">="&(B1*0.95)),MINIFS($D$1:$D$6,$D$1:$D$6,">="&B1,$D$1:$D$6,"<="&(B1*1.05))),$D$1:E$7,2,FALSE))
Otherwise:
=IFERROR(VLOOKUP(B1,$D$1:$E$6,2,FALSE),VLOOKUP(CHOOSE(2+SIGN(2-(SUMPRODUCT(MAX($D$1:$D$6*--($D$1:$D$6<=B1)*--($D$6>B1*0.95)))+SUMPRODUCT(MIN($D$1:$D$6+1E+99*(--($D$1:$D$6<B1)+--($D$1:$D$6>(B1*1.05))))))/B1),SUMPRODUCT(MAX($D$1:$D$6*--($D$1:$D$6<=B1)*--($D$6>B1*0.95))),SUMPRODUCT(MAX($D$1:$D$6*--($D$1:$D$6<=B1)*--($D$6>B1*0.95))),SUMPRODUCT(MIN($D$1:$D$6+1E+99*(--($D$1:$D$6<B1)+--($D$1:$D$6>(B1*1.05)))))),$D$1:E$7,2,FALSE))
(Obviously, change $D$1:$D$6 and $D$1:$E$6 to your actual data table ranges, and B1 to the input-value range)

Excel: Median of even number of values with a condition

When I calculate the median of even numbers for e.g 1,2,3,4,5,6,7,8.. i want the return value to be 5 i.e. the higher value of the two middle values and not the average of 4 & 5. Please help
I don't have Excel, so I can't try it, but I think you should be able to accomplish this with a combination of the LARGE function the COUNT function, and the TRUNC function. For example, if the numbers you are working with are in cells A1 through A8, you should be able to find the answer you want, though technically it's not the median, with the formula
=LARGE(A1:A8,TRUNC(COUNT(A1:A8)/2))
Edit
=LARGE(A1:A8,TRUNC((1+COUNT(A1:A8))/2))
If you know you will always be working with an even number of entries, the call to TRUNC could be omitted.
With data in column A:
=IF(ISODD(COUNT(A:A)),MEDIAN(A:A),ROUNDUP(MEDIAN(A:A),0))
EDIT#1:
Consider the array formula:
=IF(ISODD(COUNT(A:A)),MEDIAN(A:A),MIN(IF(A:A>MEDIAN(A:A),A:A)))
If the number of values is odd, return the median. If the number of value is even, return the smallest value greater than the median.
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
This approach does not require the data to be sorted.
EDIT#2:
This array formula appears to handle Ron's case:
=IF(ISODD(COUNT(A:A)),MEDIAN(A:A),MIN(IF(A:A>=MEDIAN(A:A),A:A)))
It returns the smallest value greater than or equal to the median (for the even case)
But I don't know if this is what the Poster wants.
=LARGE(A1:A9,INT((COUNT(A1:A9)/2)+0.5))
you can use Roundafter the Median
in your case you can type: =ROUND(MEDIAN(B4:I4),0)
lets say that the given range is from B4 to I4 from 1 to 8.

Average a list of numbers if greater than 0

How do I average a list of numbers whose values are greater than 0? I know I can use AVERAGEIF function in Excel
My data is located in A2, A5, A6, A10, A17.
I only want to average it if the data is greater than 0.
Since my data is not an range, I am not able to use AVERAGEIF Function range.
Need some help on this.
EDIT
For example,
I tried with three numbers:
1) 98.068 and 98.954 and 0 so my forumla looked like this:
=AVERAGE(IF(N(OFFSET(A2,{0,5,10},))>0,N(OFFSET(A2,{0,5,10},))))
The answer came out as 99.106. Not sure why.
A few options:
1)=SUM(SUMIF(INDIRECT({"A2","A5","A6","A10","A17"}),">0"))/SUM(COUNTIF(INDIRECT({"A2","A5","A6","A10","A17"}),">0"))
2)=AVERAGE(IF(N(INDIRECT({"A2","A5","A6","A10","A17"}))>0,N(INDIRECT({"A2","A5","A6","A10","A17"}))))
3)
=AVERAGE(IF(N(OFFSET(A2,{0,3,4,8,15},))>0,N(OFFSET(A2,{0,3,4,8,15},))))
2) and 3) must be committed as array formulas**
Regards
(0) A simple method
=SUM(A2*(A2>0),A5*(A5>0),A6*(A6>0),A10*(A10>0),A17*(A17>0))/SUM(A2>0,A5>0,A6>0,A10>0,A17>0)
(4) A more general method
=SUM((A1:A20>0)*A1:A20*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))/
SUM((A1:A20>0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))
The second one is an array formula and must be entered with CtrlShiftEnter
If it's possible to have text in the cells rather than numbers, then this should replace the first formula:-
=SUM(N(A2)*(A2>0),N(A5)*(A5>0),N(A6)*(A6>0),N(A10)*(A10>0),N(A17)*(A17>0))/SUM(N(A2)>0,N(A5)>0,N(A6)>0,N(A10)>0,N(A17)>0)
(I haven't used N in the > brackets in the numerator because I reason that if A2 etc. is text, the product will always be zero)
I can't persuade N to work with arrays in the second formula, so at the moment I have the rather lengthy
=SUM((IF(ISNUMBER(A1:A20),A1:A20,0)>0)*IF(ISNUMBER(A1:A20),A1:A20,0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))/
SUM((IF(ISNUMBER(A1:A20),A1:A20,0)>0)*(ADDRESS(ROW(A1:A20),1,4)={"A2","A5","A6","A10","A17"}))
but I have tested it on text values and negative numbers and it does seem fine.
The only exception is if one of the cells contains TRUE. In this case the first formula will count it as 1, the second formula will ignore it.

Excel sumproduct with condition

I want to SUMPRODUCT to ranges but only if there is no 0.
tried =SUMPRODUCT(--(CN12:CN16="<>0");I4:I8) but the results gets 0,
i have to ranges: CN12:CN16 and I4:I8, and CN12:CN16 can sometimes contain zeros. then i do not want to take that into the calculations and multiply with the value in I4:I8.
Any suggestions
Use this formula to get the SUMPRODUCT:
=SUMPRODUCT(CN12:CN16, I4:I8)
This evaluates AS:
=CN12*I4 + CN13*I5 + CN14*I6 + CN15*I7 + CN16*I8
So if one of the values equals to 0 that specific product equals to 0, and the rest of the products add to the total.
Here you have a REFERENCE to evaluate if any of the cells in your range equals with zero.
Do you actually want to use CN12:CN16 values in the calculation or are they simply a criteria range? In SUMPRODUCT you don't need the quotes so this might be what you need -
=SUMPRODUCT(--(CN12:CN16=<>0);I4:I8)
although SUMIF will get you the same thing more easily
=SUMIF(CN12:CN16;"<>0";I4:I8)
So solved it like this: =SUMPRODUCT(CN13:CN17;$I4:$I8)/SUMIF(CN13:CN17;"<>0";$I4:$I8)
As some of you wrote SUMPRODUCT gets zero if there is a zero in a list and then i check that list again in the SUMIF when i divided with the I4:I8. Maybe I was a little unclear in my description of the problem.
Thanks for your help.

Resources