Execute range function (e.g. sum, average, median) based on a condition - excel

I am trying to do something in Excel that seems easy but I cannot figure out which functions to use. I want to calculate the median value of a range of cells, for which it is true that an adjacent cell contains a specific word.
For instance, take these two columns
yes | 1
no | 4
no | 5
yes | 3
yes | 2
no | 9
no | 6
no | 8
yes | 1
no | 5
yes | 3
no | 5
Let's say I want to calculate the median for all no-cases, and the median for all yes cases. It would look like this (if I did it correctly):
yes | 2
no | 5
I know that for taking the sum, you can use COUNTIF, and I also know that there is an IF function, but I do not know how to use it to target adjacent cells to the one you want data from.

Use the array formula:
=MEDIAN(IF(A1:A12="yes",B1:B12))
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
(same for the "no" case)

Related

Calculate sum of differences between rows but only if the previous cell is bigger than the current one

I started to get a headache around my problem that I cannot figure out for the love of me.
There are unknown amounts of column if that makes any difference, but basically each row needs to be compared to the previous one and ONLY when the previous value is greater, the difference between them gets added to the sum.
So for example I have this table
| A |
--|-----|
1 | 100 |
2 | 90 |
3 | 80 |
4 | 100 |
5 | 70 |
6 | 20 |
7 | 100 |
...
Expected result: 100, derived from ((100-90) + (90-80) + (100-70) + (70-20))
I have spent a whole day browsing every single excel tutorial page and cannot find a single helpful answer. Please help :(
Formula for Cell B2: (pull down through the rows).
=IF(A1>B1;A1-B1;0)+B1
Logic: If previous value is larger than current value, add the difference to the total.
If you want to do it in one formula, a basic way would be two use two ranges offset by one cell:
=SUMPRODUCT((A1:A6-A2:A7)*(A1:A6>A2:A7))
If you wanted to make a bit more dynamic (assuming there are no gaps in the data) you could try
=SUMPRODUCT((A1:INDEX(A:A,COUNT(A:A)-1)-A2:INDEX(A:A,COUNT(A:A)))*(A1:INDEX(A:A,COUNT(A:A)-1)>A2:INDEX(A:A,COUNT(A:A))))
If there are blanks between numbers, this won't work and you would probably need to go back to a simpler pull-down formula

Excel countif and sumif together

I am trying to write a formula in Excel which will count how many times we have sold less than 50 of a particular product. For example, here is a day's sales:
Order | Product | Qty
1 | A | 5
2 | A | 5
3 | A | 5
4 | B | 30
5 | C | 75
I want a formula in a cell which says how many times we have a requirement for less than 50 of a certain product. So in the example above, there is a total of 15 As, 30 Bs and 75 Cs, so 2 of those are less than 50.
I think it will need to be an array function of COUNTIF and SUM, but can't figure it out.
You could use this formula:
=SUMPRODUCT(--(IF(ROW($B$2:$B$10)=MATCH($B$2:$B$10,$B$1:$B$10,0),SUMIF($B$2:$B$10,$B$2:$B$10,$C$2:$C$10),"")<50))
Note: It's an array formula and must be entered through Ctrl+Shift+Enter
Product order placement can be randomized and does not have to be in order.
Another way
=SUMPRODUCT((SUMIF(B2:B10,B2:B10,C2:C10)<50)/COUNTIF(B2:B10,B2:B10))
Maybe something like that will help:
=SUMPRODUCT(--IF($B$2:$B$11<>$B$1:$B$10,SUMIF($B$2:$B$11,$B$2:$B$11,$C$2:$C$11)<50,0))
Note that this is an array formula so needs to be entered with Ctrl+Shift+Enter. Data needs to be sorted by Product (i.e. product A cannot appear in random rows, like row 2, 20 and 100; it needs to be grouped together).
Result:

Excel: Average of parts of a column based on another column

Using Excel 2011 for mac, I have three columns: ID, Start date, end date and time to completion. (Date format: dd/mm/yyyy)
ID | Start | End | Time
1 | 01/01/2016 | 05/01/2016 | 4
2 | 04/01/2016 | 08/01/2016 | 4
3 | 01/02/2016 | 14/02/2016 | 13
4 | 02/02/2016 | 20/02/2016 | 18
5 | 01/03/2016 | 05/03/2016 | 4
6 | 06/03/2016 | 08/03/2016 | 2
7 | 12/03/2016 | 15/03/2016 | 3
Column D is basically the difference between column C and column B.
Now I have the total average, which is easy to calculate but I'd also like to have the averages for the different months.
And this is where my knowledge falls apart. I've tried several things but I can't seem to figure out how to calculate an average using only the cells in a column that match a certain value in another column. I could sort the tickets by date and do it manually by doing the average for only a certain range but as this list constantly changes this is definitely not a nice option.
Check out the AVERAGEIF function
AVERAGEIF(selection_range, criteria, averaging_range)
It uses the values in the selection_range to filter which values in the averaging_range will be averaged.
In your case you could say AVERAGEIF(B1:B8, "01/01/2016", D1:D8)
There are multiple ways. I would personally use an array formula, but that may be a bit advanced and overly complicated.
I suggest adding a column E "Month" - into E2 add:
=MONTH(B2)
Then copy cell E2 to E3:E8.
Now you can easily get a monthly average by applying the AVERAGEIF command:
=AVERAGEIF(E2:E8,2,D2:D8)
The second argument, 2, indicates February, but may be exchanged with any number from 1-12.

Excel - Replace blank if only 1 contiguous value

Okay this one is a bit strange to explain, but I have a file like this:
A B
1 | Person
2 | Person test
3 | Record
4 | Record test
5 | Tiger
6 | Scott
7 | Scott test
8 | Scott test
9 | Scott test
As you can see, in column A the first row where a new value starts, column B is blank. What I need is to fill in a placeholder (NULL) value into column B if there is only one contiguous value in column A. So for the above example row 5 only has 1 contiguous value so the result should look like this:
A B
1 | Person
2 | Person test
3 | Record
4 | Record test
5 | Tiger (NULL)
6 | Scott
7 | Scott test
8 | Scott test
9 | Scott test
I tried something like this but doesn't work:
IF(EXACT($A5,$A4)=FALSE AND EXACT($A5,$A6)=FALSE, "(NULL)", $B5)
I just want a formula I can paste all the way down column B. Any suggestions?
=IF(OR(A2=A1,A2=A3),"Test","(NULL)")
Replace Test and (NULL) with the values you want to display.
This checks to see if the AX = A(X-1) or A(X+1). If either of those is true, then it means that AX is contiguous with another row.
I should note that I tested this with Excel 2013.
Also, if you want the formula to use the value already in BX when the rows are contiguous, then you can't do that in the same column.
I'm not sure if that's what you're asking to do, but the formula you tried would have been attempting to do that, creating a circular reference.
You can easily do this in column C. Replace "Test" with BX. Then, if you only want 2 rows in the end, you can copy and paste the values of column C into column B, then delete column C. But, this way you'll lose your formula.
Suppose you have your data like this:
This formula seem to work:
=IF(OR(A2=A1,A2=A3),IF(B2="","",B2),"(NULL)")

Conditional sum

I need a quick macro/formula. I have two columns:
A | B
|
| 10 | 9 |
| 11 | |
| 8 | 10 |
| N/A |
| 4 | |
| 7 | 8 |
total | 40 | 27 |
Now I want to add a sum to a column that adds up all the values in column A BUT replaces that value with the value in column B if it exists.
So I would expect the sum to end up being 9 + 11 + 10 + 4 + 8 = 42
I'm not entirely sure about the power of Excel's calculations or how to go about performing a for loop within a formula.
There are MANY ways to do this, but I would do it as two sums added together....
Assuming you had the data you supplied in your question in cells A1 to B6, your formula could look as follows:
=SUMIF(B1:B6,"",A1:A6)+SUM(B1:B6)
In essence, you're saying:
Sum A1:A6 anywhere B1:B6 is blank
PLUS
Sum B1:B6
Hope this makes sense and helps!
=SUM(IF(ISNUMBER(B1:B5),B1:B5,A1:A5))
will do this. Note that the formula is an array formula though; once you've finished entering it in a cell, press Ctrl + Shift + Return, rather than Return. That tells Excel that the formula is an array type.
I've set this up for 5 rows; just extend as you need.
One of many other possibilities could be a helper column containing:
=IF(ISBLANK(B3),A3,B3)
copied down (and up, if necessary!)
Not necessarily better than many alternatives but perhaps easier to see 'what is going on' and =IF may already be familiar.

Resources