Calculate sum for a date range - excel

I'd like to calculate the sum of multiple values for a range of overlapping dates. It should return the sum for a specific year and split the values that fall into more than one year proportionally.
Example:
╔══════════╦════════════╦════════════╗
║ Value ║ From ║ To ║
╠══════════╬════════════╬════════════╣
║ 100,00 € ║ 01.01.2015 ║ 31.12.2015 ║
║ 150,00 € ║ 01.07.2015 ║ 31.06.2016 ║
║ 300,00 € ║ 01.01.2016 ║ 31.12.2016 ║
╚══════════╩════════════╩════════════╝
Expected result for year 2015: 175,00 €
Expected result for year 2016: 375,00 €
I did't find an efficient way using SUMIF() or SUMIFS(). Especially for splitting the result proportionally for overlapping dates.

I am suggesting the use of helper columns, with the column headers being the years for which the totals will be calculated i.e. 2015 and 2016.
I have added an extra row to the data to make sure that it works when the time period is not a whole year.
The area of overlap is the smaller of the higher ends of the two date ranges minus the larger of the lower ends of the two date ranges. If this turns out to be negative then there is no overlap and the result should be set to zero. This gives rise to the following formula for each row starting in E2:-
=MAX((MIN(DATE(E$2,12,31),$C3)-MAX(DATE(E$2,1,1),$B3))+1,0)
where the first column header is in E2.
However with the data given in the question, this produces an inexact answer because there are more days in the second 6 months of the year than in the first
6 months.
If you work in whole months using the following formula you can get an exact answer:-
=MAX((MIN(E$2*12+12,YEAR($C3)*12+MONTH($C3))-MAX(E$2*12+1,YEAR($B3)*12+MONTH($B3)))+1,0)
Then work out the total overlap for each time period in G3:-
=E3+F3
And calculate the proportion of spending as Amount X Overlap for year / Total overlap in H3:-
=$A3*E3/$G3
The method can readily be extended to arbitrary date ranges, not just whole years.
Working in days:-
Working in months:-
Here is an array formula (must be entered with Ctrl-Shift-Enter) which will do the calculation in one step, but it seems rather long. In a production version, $C3:$C6 would be replaced by $C:$C etc. :-
=SUM(IF(IF(E$2*12+12<YEAR($C3:$C6)*12+MONTH($C3:$C6),E$2*12+12,YEAR($C3:$C6)*12+MONTH($C3:$C6))-IF(E$2*12+1>YEAR($B3:$B6)*12+MONTH($B3:$B6),E$2*12+1,YEAR($B3:$B6)*12+MONTH($B3:$B6))+1>0,IF(E$2*12+12<YEAR($C3:$C6)*12+MONTH($C3:$C6),E$2*12+12,YEAR($C3:$C6)*12+MONTH($C3:$C6))-IF(E$2*12+1>YEAR($B3:$B6)*12+MONTH($B3:$B6),E$2*12+1,YEAR($B3:$B6)*12+MONTH($B3:$B6))+1,0)*$A3:$A6/(YEAR($C3:$C6)*12+MONTH($C3:$C6)-YEAR($B3:$B6)*12-MONTH($B3:$B6)+1))
The two methods (helper columns and array formula) would give different results for date ranges that fell partly outside the years 2015 and 2016 e.g. 1/7/14-30/6/15 would put the full amount into 2015 the first way but only half of it the second way.

Thank you very much for your answer, Tom.
This is what I ended up with.
╔═══════╦════════════╦════════════╦══════╦══════╗
║ Value ║ From ║ To ║ 2015 ║ 2016 ║
╠═══════╬════════════╬════════════╬══════╬══════╣
║ 100 ║ 01.01.2015 ║ 31.12.2015 ║ 175 ║ 375 ║
║ 150 ║ 01.07.2015 ║ 30.06.2016 ║ ║ ║
║ 300 ║ 01.01.2016 ║ 31.12.2016 ║ ║ ║
╚═══════╩════════════╩════════════╩══════╩══════╝
Using the following formular were Value is A, From is B, To is C
=ArrayFormula(
SUM(
IFERROR(
IF(
IF(
D$1 * 12 + 12 < YEAR($C:$C) * 12 + MONTH($C:$C);
D$1 * 12 + 12;
YEAR($C:$C) * 12 + MONTH($C:$C)
)
- IF(
D$1 * 12 + 1 > YEAR($B:$B) * 12 + MONTH($B:$B);
D$1 * 12 + 1;
YEAR($B:$B) * 12 + MONTH($B:$B)
) + 1 > 0;
IF(
D$1 * 12 + 12 < YEAR($C:$C) * 12 + MONTH($C:$C);
D$1 * 12 + 12;
YEAR($C:$C) * 12 + MONTH($C:$C)
)
- IF(
D$1 * 12 + 1 > YEAR($B:$B) * 12 + MONTH($B:$B);
D$1 * 12 + 1;
YEAR($B:$B) * 12 + MONTH($B:$B)
) + 1;
0
)
* $A:$A / (
YEAR($C:$C)
* 12 + MONTH($C:$C)
- YEAR($B:$B)
* 12 - MONTH($B:$B)
+ 1
)
; 0
)
)
)
Hint: You can post the formular indented to Google Spreadsheets to keep it readable.

Related

Turn nested if formula into VBA

I would like to turn my formula into a macro. I want to look up values on column A and return a different value to column D depending on conditions.
My formula is :
=IF(ISNUMBER(SEARCH("*10*",D2,6)),"word1",IF(ISNUMBER(SEARCH("*15*",D2,1)),"word2",IF(ISNUMBER(SEARCH("*1*",D2,1)),"word3",IF(ISNUMBER(SEARCH("*20*",D2,1)),"word4",IF(ISNUMBER(SEARCH("*30*",D2,1)),"word5")))))
I need to make sure that search for 1 comes after 10 or 15 so that formula doesn't return word3 for all cells containing "1".
Column A contains a text that is at times is misspelled so the only common value is the numbers. That is why I want to search the numbers within text to return word12345.
Below is an example- I get a different excel file with age name and last name every week and need to fill out the program classification manually.
╔═══════════╦═════════╦═══════════╦═════════╗
║ AGE ║ NAME ║ LAST NAME ║ PROGRAM ║
╠═══════════╬═════════╬═══════════╬═════════╣
║ 10 YE OLD ║ ANNE ║ BROWN ║ word1 ║
║ AGE 10 ║ ALLY ║ SMITH ║ word1 ║
║ 15 YO ║ MATT ║ JANES ║ word2 ║
║ 15 ║ DENNIS ║ JOHNSON ║ word2 ║
║ 10Y OLD ║ DIANA ║ WILLIAMS ║ word1 ║
║ 20yr ║ JORDAN ║ BROWN ║ word4 ║
║ 30 YR OLD ║ MELISSA ║ RODRIGUEZ ║ word5 ║
╚═══════════╩═════════╩═══════════╩═════════╝
The data that I need to get the result from is exampled below:
10 YE OLD
AGE 10
15 YO
15
10Y OLD
20yr
30 YR OLD
You could try this, I tested it and it worked for me utilizing whole numbers as you stated (10,15,5,1,30,20) and changed it to the "word1" etc. If you were looking for a range, i.e. 1 but less than 10, the select case can be updated. You have to insert your workbook name and extension as well as worksheet name where indicated in the code.
Sub Select_Case()
Dim RNG As Range
Dim ws As Worksheet
Dim TextTest As String
Dim AllColumn As Variant
Set ws = Workbooks("<EnterYourWorkbookName.Extension_Here>").Worksheets("<EnterYourSheetNameHere>")
AllColumn = ws.Range(ws.Cells(2, 4), ws.Cells(1, 4).End(xlDown).Offset(1, 0)) 'assumes heading in row 1 and no breaks in data
For i = 2 To UBound(AllColumn) 'assumes heading, no headin i = 1
TextTest = ws.Cells(i, 4).Value
Set RNG = ws.Cells(i, 4) 'rows then columns if you need to change it later, this should be where you want to output your data
Select Case TextTest
Case Is = 10
RNG = "word1"
Case Is = 15
RNG = "word2"
Case Is = 1
RNG = "word3"
Case Is = 20
RNG = "word4"
Case Is = 30
RNG = "word5"
'Case else (utilize if you have a catch all just in case it is something not predicted)
'what to do
End Select
Next i
End Sub
Let me know if you need some small tweaks.

Excel Formula if | and

I need some help with an Excel formula
╔═══════════════╦═════════╗
║ SALE PRICE ║ Bonus ║
╠═══════╦═══════╬═════════╣
║ 0 ║ 14999 ║ $25.00 ║
╠═══════╬═══════╬═════════╣
║ 15000 ║ 20000 ║ $50.00 ║
╠═══════╬═══════╬═════════╣
║ 20001 ║ 25000 ║ $125.00 ║
╠═══════╩═══════╬═════════╣
║ 25001+ ║ $250.00 ║
╚═══════════════╩═════════╝
The above table is the data I need to extract when the sale price is entered into a field.
For example.....
Cell D24 is the BONUS FIELD
Cell E29 is the SALE PRICE FIELD
If the sale price is between $0-$14,999, then I want cell D24 to auto-populate $25.00. Then, if the sale price is between $15,000-$20,000 I want Cell D24 to auto-populate $50. Then, if the sale price is between $20,001-$25,000 I want Cell D24 to auto-populate $125. Then finally, if the sale price is $25,001+ I want Cell D24 to auto-populate $250.
try,
=25+(A2>15000)*25+(A2>20000)*75+(A2>25000)*125
this formula seems to work but only the first rule being 0-14999.....how do I add another rule to the existing formula.
=IF(AND(E29>=0,E29<=14999),25,0)

How to filter and list data from another sheet?

I need some help for my Excel formula. I am trying to select specific rows based on the values in those rows.
Let's say these are my values:
╔══════════╦═════════╦══════╗
║ Data ║ Month ║ Name ║
╠══════════╬═════════╬══════╣
║ Value 1 ║ january ║ mark ║
║ Value 2 ║ january ║ mark ║
║ Value 3 ║ january ║ rick ║
║ Value 4 ║ january ║ rick ║
║ Value 5 ║ march ║ mark ║
║ Value 6 ║ march ║ mark ║
║ Value 7 ║ march ║ rick ║
║ Value 8 ║ march ║ rick ║
║ Value 9 ║ august ║ mark ║
║ Value 10 ║ august ║ rick ║
╚══════════╩═════════╩══════╝
The value in A1 = january
The value in A2 = mark
I want to list all the rows where the month is A1 and the name is A2.
This should be my result:
║ Value 1 ║ january ║ mark ║
║ Value 2 ║ january ║ mark ║
I have tried using INDEX combined with IF, but with no succes. I am looking for either a VBA solution or a formula, whatever is best.
I am assuming that:
'Data' is in Sheet1!A1, 'Month' is in Sheet1!B1 and 'Name' is in Sheet1!C1.
On Sheet2!A1 is the value of 'Month' which you want to filter out and on Sheet2!A1 is the value of 'Name' you want to filter out.
Make a new column in Sheet1!D1 (beside 'Name') and give it a header, eg. 'Tag'. In the first row, D1 (under 'Tag') write the formula
=IF(AND(B2=Sheet2!$A$1,C2=Sheet2!$A$2),MAX($D$1:D1)+1,"")
Pull (copy) the above formula down till the last row of your table.
On Sheet2, replicate your table headers, this time in B1, C1 and D1 (as A1 & A2 already have your filter values).
// now everything is for Sheet2
In B2 write the following formula:
=IFERROR(INDEX(Sheet1!A:A,MATCH(ROWS($B$2:B2),Sheet1!$D:$D,0)),"")
Paste this formula in the new filter table rows.
Change values in Sheet2!A1 (Month) and Sheet2!A2 (Name) to test if the correct rows are being filtered.

Wildcard condition with * for SUMIFS formula on a column with number values

I have a huge excel sheet that looks like this:
╔══════╦══════╦═════╗
║ A ║ B ║ C ║
╠══════╬══════╬═════╣
║ Jack ║ 2001 ║ 1,5 ║
║ Jack ║ 2002 ║ 2,0 ║
║ Jack ║ 2003 ║ 1,0 ║
║ Jack ║ 3001 ║ 3,5 ║
║ Jack ║ 3002 ║ 4,0 ║
║ Jack ║ 3003 ║ 1,0 ║
║ Jill ║ 2001 ║ 3,0 ║
║ Jill ║ 2002 ║ 5,0 ║
║ Jill ║ 2003 ║ 2,0 ║
║ Jill ║ 3001 ║ 0,5 ║
║ Jill ║ 3002 ║ 6,0 ║
║ Jill ║ 3003 ║ 2,5 ║
╚══════╩══════╩═════╝
Column B contains many different numbers, but they all begin with digits 2, 3 or 8. The numbers in column B are always be 4 digits long; I'm only interested in checking the first digit.
I need to add together the values of column C, where the first digit of the corresponding cell in column B is either 2*, 3* or 8*. What I need is to create a formula that does this (Ruby-esque pseudocode):
sum = 0
spreadsheet_rows.each do |row|
if row.a == "Jack" and row.b == "2*" # Note the second wildcard condition.
sum += row.c
end
end
puts sum # Should print 4,5 in this example.
I'm trying to use the following formula in Excel to accomplish this:
=SUMIFS($C:$C; $A:$A; "Jack"; $B:$B; "=2*")
I know that Excel does not support wildcard conditions for numbers, however, I have formatted column B as type "Text" in Excel, so I thought it would be treated as such, but it appears that it is still treated as an int.
Is there a different way of applying a wildcard condition in =SUMIFS for number values in Excel? Perhaps there's a way to somehow "cast" the integers to strings in the formula? I haven't found a way to do it (yet).
I'm using Excel for Mac 2011.
I'd go for the less readable, but more powerful SUMPRODUCT:
=SUMPRODUCT(($A:$A="Jack") * (LEFT($B:$B;1)="2") * ($C:$C))
which will generate boolean arrays for each of the conditions (first and second brace part) which it will multiply with the third one (your numbers).
EDIT:
As noted in comments, #VALUE errors can appear if any value in column C cannot be converted to a number. To avoid that, you could use the syntax suggested by barry houdini
=SUMPRODUCT(($A:$A="Jack") * (LEFT($B:$B;1)="2"); $C:$C)
and let SUMPRODUCT skip over non-numbers.
This works for me:
=SUM((A1:A12=F2)*(LEFT(B1:B12)=""&F3)*C1:C12)
entered as an array formula with CtrlShiftEnter
You ask how to cast numbers to strings; concatenating an empty string and a number ""&F3 is one way to do that.
{=SUM((A1:A12=F2)*(LEFT(B1:B12)=""&F3)*C1:C12)}
No need for an arrays as shown above just type following formula which gives equal results to above Arrays formula
=SUMPRODUCT((A1:A12=F2)*(LEFT(B1:B12)=F3&"")*C1:C12)
Remember difference [Arrays=""&F3] vs [SUMPRODUCT= F3&""]
I shall be bring to your kind I am just very very very happy to see your work and the way you authoritatively asked question as as
"I know that Excel does not support wildcard conditions for numbers, however, I have
formatted column B as type "Text" in Excel, so I thought it would be treated as
such, but it appears that it is still treated as an int."
SUMPRODUCT only not work when we have to get output in TEXT
Hoping your verification about all above and kindly highlight at prominent place

How to sum up values between 2 dates in Excel 2007

Lets say I would like to sum up values from January to March. Below is an example
╔═══════════╦════════════╗
║ Column A ║ Column B ║
╠═══════════╬════════════╣
║ 1/30/2011 ║ 1 ║
║ 1/25/2011 ║ 1 ║
║ 3/30/2011 ║ 1 ║
║ 3/25/2011 ║ 1 ║
║ 5/13/2011 ║ 1 ║
╚═══════════╩════════════╝
I did some research and found I can use the SUMIFS function
=SUMIFS(B1:B5,A1:A5,">="&DATE(YEAR(2011),MONTH(1),DAY(1)),A1:A5,"<="&DATE(YEAR(2011),MONTH(4),DAY(1)))
But for some reason instead of returning 4 it returns 0. I would really appreciate if someone could figure out why.
Thank you
I don't think Year/Month/Day do what you're expecting, see:
http://www.techonthenet.com/excel/formulas/year.php
They return the year value, month value, and day value of their argument. Try entering
=YEAR(2011)
and compare it to
=YEAR("1/30/2011")
That said, you can get what you want by just putting the dates in the quotes
=SUMIFS(B1:B5,A1:A5,">=2011-01-01",A1:A5,"<=2011-04-01")
produces 4 in my Excel.

Resources