I have a long time-series of monthly returns for the S&P500. I would like to calculate the 3-month return, for a subset of dates?
For example, I would like to calculate the 3-month return for the following
A B C
Month Year 3-Month Return
Jan 1929
Dec 1948
July 1981
I have monthly return data like:
A B C
Month Year Monthly Return
Jan 1929 0.102
Feb 1929 0.072
Mar 1929 -0.231
....
....
Dec 2019 0.157
So the first calculation would be something like (1+0.102)(1+0.072)(1-0.231)-1. I can do this manually but have many calculations, unfortunately.
to find the match, you need a unique column, so combine A & B to another column, so that the month+year combination is unique, then use that combination to find the values.
Suppose you have data in this form:
The formula for match index is =match(P3,U$3:U$13,0) and that for your 3 monthly return is =(1+index(V$3:V$13,Q3))*(1+index(V$3:V$13,Q3+1))*(1+index(V$3:V$13,Q3+2))-1
You can put the match index inside the formula in C column to avoid the column, but you'd have to put it 3 times. you can also use different combination for combined date, like the actual date format of mm/yyyy, it'll still work.
Related
I have a spreadsheet that will always return varying row contents. So I need to search each row to find the january, february, march, april, may, june, july, etc content and extract the counts from that in the cell with substring. I'm not certain how to do this since the location in the row will always vary. Any ideas? I was thinking vlookup or index or match, or mid/search, but I'm not sure.
Example spreadsheet content, notice some rows have the overallCount and some have overrideCount, and a string like "overallCount 2022/12 35" is the complete cell value, where 2022 is the year, and 12 is the month:
result1 result2 result3 result4
overallCount 2023/01 11 overallCount 2022/12 35 overallCount 2022/01 12 overrideCount 2022/08 5
overallCount 2023/02 1 overallCount 2022/11 34 overrideCount 2022/02 9 overrideCount 2022/01 5
------ (3 rows) OverrideYearOrders overrideCount 2022/10 2 overallCount 2023/01 6
And the rows can have varying column numbers as well if there are more or less orders for each device.
I want to add a formula in result8 column or so, and search to the left of each row, and pull out the counts in columns for Jan, Feb, March, April, May, etc. I was thinking of something like this:
=VLOOKUP("overrideCount 2022/12",H6:AC8517,1,FALSE)
but it's not returning the count. It's returning #N/A
example per-row output on the right:
Overall Override
Jan2022 Feb March April May June July Aug Sept Oct Nov Dec Jan2023 Jan2022 Feb Mar ...
35 11 5
As you can see, not every month has a count that would be found in the row.
How would I pull those monthly counts for overall and override out to the right in each row?
Update*
This is the generated table per comment below:
result1
result2
result3
result4
result5
Output to right here Overall Jan2022
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
Override Jan2023
Override Jan2022
Feb
overallCount 2023/01 11
overallCount 2022/12 35
overallCount 2022/01 12
overrideCount 2022/01 5
35
11
5
overallCount 2023/02 1
overallCount 2022/11 34
overrideCount 2022/02 9
overrideCount 2022/01 5
34
5
9
------
(3 rows)
OverrideYearOrders
overrideCount 2022/10 2
overallCount 2023/01 6
6
Example output is shown to right in generated table above, and also example per-row output is given in this before this added update already.
FORMULA:
This formula will get you to where you want pending a couple changes.
Rename the column headers to represent the date you want to calculate for (1/1/2022, 2/1/2022, ...).
This formula requires every cell value to "search" to be of "overallCount yyyy/mm" or "overrideCount yyyy/mm"
format
Drag and drop the formula for as far as you need it (row and columns).
First formula, totaling overallCount (F2:s2)
=IF(SUMPRODUCT(--IFERROR(SEARCH("overallCount "&TEXT(F$1,"yyyy/mm"),$A2:$E2)>0,FALSE)*SUMPRODUCT(--IFERROR(RIGHT($A2:$E2,LEN($A2:$E2)-(FIND("overallCount "&TEXT(F$1,"yyyy/mm"),$A2:$E2)+20)),0)))>0,SUMPRODUCT(--IFERROR(SEARCH("overallCount "&TEXT(F$1,"yyyy/mm"),$A2:$E2)>0,FALSE)*SUMPRODUCT(--IFERROR(RIGHT($A2:$E2,LEN($A2:$E2)-(FIND("overallCount "&TEXT(F$1,"yyyy/mm"),$A2:$E2)+20)),0))),"")
Second formula, totalling overrideCount (R2:AG2)
=IF(SUMPRODUCT(--IFERROR(SEARCH("overrideCount "&TEXT(T$1,"yyyy/mm"),$A2:$E2)>0,FALSE)*SUMPRODUCT(--IFERROR(RIGHT($A2:$E2,LEN($A2:$E2)-(FIND("overrideCount "&TEXT(T$1,"yyyy/mm"),$A2:$E2)+21)),0)))>0,SUMPRODUCT(--IFERROR(SEARCH("overrideCount "&TEXT(T$1,"yyyy/mm"),$A2:$E2)>0,FALSE)*SUMPRODUCT(--IFERROR(RIGHT($A2:$E2,LEN($A2:$E2)-(FIND("overrideCount "&TEXT(T$1,"yyyy/mm"),$A2:$E2)+21)),0))),"")
EXPLANATION:
The formula is using the SUMPRODUCT function which multiplies the corresponding elements of the arrays and returns the sum of products.
The formula uses the SEARCH function to look for the occurrence
of the string in the cells using the format
"yyyy/mm", in the cells of the range A2:E2 and returns TRUE if
found, otherwise returns FALSE.
The formula uses the IFERROR function to check for errors and returns
0 if error is found.
The formula uses the RIGHT function and is used to extract the rightmost
part of the cell, starting from the position of the found string -20/-21 (calculation that takes advantage of the date in the column header plus its position in order to autofill). In short, it totals the number after the date.
EXAMPLE:
The following formula is the root formula that is just wrapped in an IF THEN statement to show empty cells instead of 0's. This formula is written assuming Cells A1:E1 are the 'results' headers, and starting at Cell F1 is the date start of your dates. (Jan22-Feb23 of each category)
SUMPRODUCT(--IFERROR(SEARCH("overallCount "&TEXT(F$1,"yyyy/mm"),$A2:$E2)>0,FALSE)*SUMPRODUCT(--IFERROR(RIGHT($A2:$E2,LEN($A2:$E2)-(FIND("overallCount "&TEXT(F$1,"yyyy/mm"),$A2:$E2)+20)),0)))
I would like to create a Gantt chart like with excel based on task start and duration cell values. I have an excel as below:
Item
Start
Duration(months)
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
1
Q1
6
2
Q2
3
3
Q2
1
Start column indicates the quarter-calender the task starts.
Duration indicates the number of months the task can take to complete.
Based on these two value I would like to have a formula in columns Jan-Aug to fill with x appropriately
Desired output:
Item
Start
Duration(months)
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
1
Q1
6
x
x
x
x
x
x
2
Q2
3
x
x
x
3
Q2
1
x
I dabbled with XLOOKUP and OFFSET with no luck. Tried creating defined names as in Microsoft template with no luck
You can try the following formula in cell D2:
=LET(months, MONTH(D1:O1), dur, C2:C4, Qs, B2:B4,
QsUx, SORT(UNiQUE(Qs)), start, XLOOKUP(Qs, QsUx, SEQUENCE(ROWS(QsUx),,1,3)),
end, start + dur - 1,
GEN_X, LAMBDA(idx, MAP(start, end, LAMBDA(ss, ee,
LET(month, INDEX(months,,idx), IF(MEDIAN(ss, ee, month) = month, "x",""))))),
DROP(REDUCE("", months, LAMBDA(acc, m, HSTACK(acc, GEN_X(m)))),,1)
)
and here is the output:
Note: The formula generates the entire grid, there is no need to expand the formula.
Explanation
It uses the following two ideas:
To populate the grid using the MEDIAN function. Check the answer provided by #JosWoolley: Sum unique day count within a date ranges.
DROP/REDUCE/HSTACK pattern to iterate over all the month's columns to generate the x's for a given column. Check the answer provided by #DavidLeal to the question: how to transform a table in Excel from vertical to horizontal but with different lengths.
The rest is just to prepare/transform the input data in the format we want. The LET function is used for easy reading and composition.
The row with the months: D1:O1 was generated in date format as follows:
=EDATE(DATE(2022,1,1), SEQUENCE(1,12,0))
Now the months variable will have the corresponding months via the MONTH function, it returns the following sequence of values: 1,2,..,12.
QsUx has the unique names of quarters sorted. Then we can calculate the start date as follow:
XLOOKUP(Qs, QsUx, SEQUENCE(ROWS(QsUx),,1,3))
SEQUENCE generates the following sequence: 1,4,7,..etc. (as many values as rows have QsUx). Representing the starting month of each quarter.
Note: This is just one approach. The start can be obtained, by extracting the number of the Quarter and building a sequence: 1, prev. value + 3, for example. Alternatively, since we have only four quarters just a simple constant array with all the Qs and their corresponding start dates. The approach used in the main formula is more generic because the Gantt can be expanded to more than one year and it still works.
Having the start date, then we can calculate the corresponding end date as follows: start + dur - 1.
The user LAMBDA function GEN_X generates the x's values for a given column of months indicated by the idx as input argument (representing the corresponding month).
Finally, we use the DROP/REDUCE/HSTACK pattern to append each column to complete the grid.
Note: The solution assumes no excel version constraints as per the tag listed in the question, so all existing excel functions are available. If you have some limitations for example you don't have the DROP function, let me know to try to find some alternative.
Let me preface this question with: The data that I was provided is not in the most logical order and the work involved in scrubbing to a more uniform it would be time consuming.
I am compiling a sales sheet to collect an overall yearly view of each product's revenue. I am currently attempting to search for the product name within the sheets required within a main sheet (all within the same workbook).
See sample output below:
Year
2014 Jan Feb Mar ... Nov Dec
Product 1
Product 2
Product 3
2015 Jan Feb Mar ... Nov Dec
Product 1
Product 2
Product 3
Sample Input
... F ... N ... T ...
... Date ... Product ... Amount ...
------------------------------------------------------
10/03/15 ... Prod. 1 ... $1000.00 ...
04/05/15 ... Prod. 3 ... $3000.00 ...
02/09/15 ... Prod. 1 ... $2000.00 ...
I would like to break it down based on month and product which is where I am running into issues. Currently I am (attempting) using:
=SUMIFS('2015'!R:R,'2015'!F:F,"Product 1",">=1/1/2015",'2015'!F:F,"<=1/31/2015")
Any help provided would be greatly appreciated!
SUMIFS() is the best way to go. But one can make the criteria more dynamic so the formula can be copied from cell to cell instead of having the values "hard coded" in the formula.
To do such ensure that your month row is Actual dates that have a number format of mmm instead of text.
As you can see the Jan cell is actually 1/1/2015. We could have made this a little more dynamic also. Instead of the year being hard coded, since I put the year in A2, I could have used this formula:
=DATE($A$2,Column(A:A),1)
And drag/copied across. This would have put the first day of each month.
Again do a custom format of mmm
Then the formula would be:
=SUMIFS('2015'!$T:$T,'2015'!$N:$N,$A3,'2015'!$F:$F,">=" & EOMONTH(B$2,-1)+1,'2015'!$F:$F,"<=" & EOMONTH(B$2,0))
This formula then is dragged/copied across and down.
My count of business days in end date should be, for example, May 30 to May 31 will be 2 days and May 30 to May 30 is already counted as 1 day. My problem with WORKDAY is that it counts the day from May 30 to May 31 as 1 day only.
What I've think of is to subtract the result of WORKDAY with 1 business day to get my desired result. However, with my current formula, I was only able to subtract the result of WORKDAY without concerning the weekends (=WORKDAY(C2,B2-1)).
So for example, column C is June 3 (Friday) and column B is 2. The output of my formula will be June 5 (Sunday) because of the subtraction, I want it to be June 6. How will I do that?
Column B = Duration
Column C = Start Date
Column D = End Date (Formula-based)
Column D:
=WORKDAY(C2,B2-1)
Provide Input
B | C | D
2 | 2016/06/03 |
Desired outcome
B | C | D
2 | 2016/06/03 | 2016/06/06
TRY
=WORKDAY.INTL(C2,B2-1,1)
This will set sat. and sun. as offdays
It is not clear exactly what you want to do.
If you want to do an inclusive count of Workdays from 2016/06/03 to 2016/06/06 then you could just use the NETWORKDAYS function.
If you want to add two workdays to Jun 3, and have Jun 3 count as the first day, to give you the result of Jun 6, then use
=WORKDAY(C2-1,B2)
That takes care of the issue where C2 is not a workday, and you want to count 1 workday as the first workday after C2
Otherwise, your formula should work as written.
I've got a table in Excel which is structure like so:
Month Date Time ID Name Currency Value
Jan 07/01/14 5 1234567 Ted GBP 500
Jan 10/01/14 12 1234567 Ted GBP 723
Feb 23/02/14 6 9877654 John GBP 300
Feb 10/02/14 10 1234567 Ted GBP 333
What I need to do is write a formula which basically returns be the total of Value where ID and Month are equal to whatever the lookup values are. For example, using the above I would say:
Find the total of Value where Month equals Jan and ID equals 1234567.
The answer in this case would be 1223.
Ive just tried
=SUMIFS(INPUT!H:H,INPUT!D:D='TRANS BY MID'!B2,INPUT!A:A='TRANS BY MID'!C1)
INPUT!H:H is my ID column
INPUT!D:D='TRANS BY MID'!B2 is the ID I want to use
INPUT!A:A is the Month column
TRANS BY MID'!C1 is Jan
To provide a working solution I simplified your question into one sheet. The data appears like this:
You can link your other sheet to the values shown in column J.
The formula is now this:
=SUMIFS(G:G,D:D,J1,A:A,J2)
The result is shown in J7: