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

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

Related

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 - Rather complex SUM IF criteria

I have roughly the following setup:
Values:
MONTH | DURATION | VALUE |
5 | 3 | 120 |
6 | 1 | 100 |
Expected outcome for totals:
MONTH | TOTAL
5 | 120
6 | 220
7 | 120
What I would like to do, is to be able to sum in another table the total values for each month. The logic would be to SUM every value where the total table's month is equal or higher than that of the values, but lower than the value's month + duration.
Does that make any sense? Is that possible? I'm cracking my head and I can't seem to find a way to solve it.
Thank you very much.
The easiest solution is probably to make another column with the end month. And then use SUMIFS to check if month is >= starting month and <= ending month.
=SUMIFS(<Range of Values>,<Range of starting>,"<="& "Target month",<Range of ending>,">="& <Target month>)
DSUM is the best candidate for this, I believe. See Microsft's documentation and this site for help understanding function, and what it is doing. I have made very complex calculations possible in Excel by using the "database" methods (DSUM, DCOUNT, etc).

Pivot table to return the FIRST value in a range

Glad to be joining the forum.
My question deals with attempting to return the FIRST value that occurs over several columns of data, using a pivot table that is filtered within a narrow time range. My current pivot table works by counting values in each column over the time rows. However I'm really only interested in the FIRST value that I come across for each person. So the raw looks something like this:
Person|TimeValue|Variable1|Variable2
1 | 1 | 1 | 0
1 | 2 | 1 | 0
2 | 1 | 1 | 0
2 | 2 | 0 | 1
What I currently get for a pivot using a range of time1 to time 2 is
1 | |2 | 0
2 | |1 | 1
Clearly, the time range I select includes MULTIPLE values in the same column, leading to counts of >1. What I'm thinking is that there is a way to use the same time sorting, but count only the FIRST time a value occurs in that variable, so that the pivot reports only the first time a value occurs within the range for the variables of interest.
Is there a simple way, or am I going to have to do this in VBA?
Much appreciated for any and all help. This is my first more complicated attempt with the newer pivots.
This is probably not the problem you would want to solve using a pivot table. You could just use the VLOOKUP Excel function to solve this issue in a simple way. VLOOKUP will always return the first value in the lookup range that matches the lookup value.

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.

Excel Formulas: Show total based on date entry

I've got a spreadsheet with two columns that represent the number of processed records, and the date the records were processed. In some cases, the records can be processed in multiple batches, so the document looks something like this:
33 4/1/2009
22 4/1/2009
12 4/2/2009
13 4/4/2009
36 4/4/2009
I'm trying to add a new set of columns that contain a date, and shows the total number of records for that date, automagically:
4/1/2009 55
4/2/2009 12
4/3/2009 0
4/4/2009 49
I know how to do this algorithmically, and I could probably manipulate the spreadsheet outside of Excel, but I'm trying to do this in the live spreadsheet, and am a bit bewildered as to how to pull it off.
Any ideas?
Thanks!
IVR Avenger
Will the SUMIF function work for you? SUMIF([range],[criteria],[sum_range]) I think you could set range = the set of cells containing dates in your first listing, criteria would be the cell containing the date in the second listing, and sum_range would be the counts in the first column of your first listing.
I would suggest using a Pivot Table. Put the dates into the row area and 'sum of' records in the data area. Nothing in the columns area.
A pivot table will be more dynamic than a formula solution because it will only show you dates that exist.
Assuming your dates are in column B and the numbers to be accumulated are in A, you could try something like this:
| A | B | C D
1 | 33 | 4/1/2009 | =MIN(B:B) | {=SUM(IF(B1:B5=C1,A1:A5,0))} |
2 | 22 | 4/1/2009 | =C1+1 | {=SUM(IF(B1:B5=C2,A1:A5,0))} |
3 | 12 | 4/2/2009 | =C2+1 | {=SUM(IF(B1:B5=C3,A1:A5,0))} |
4 | 13 | 4/4/2009 | =C3+1 | {=SUM(IF(B1:B5=C4,A1:A5,0))} |
5 | 36 | 4/4/2009 | =C4+1 | {=SUM(IF(B1:B5=C5,A1:A5,0))} |
Note the {} which signifies an array formula (input using Control-Shift-Enter) for any non-trivial amount of data it's heaps faster than SUMIF().
I'd be inclined to define dynamic names for the A1:A5 and B1:B5 parts, something like
=OFFSET(A1,0,0,COUNT(A:A),1)
so that I didn't have to keep fixing up my formulae.
There's still a manual element: adding new rows for extra dates, for example - that might be a good place for a little VBA. Alternatively, if you can get away with showing, for example, the last 90 days' totals, then you could fix the number of rows used.

Resources