Dynamic way to calculate the past 6 months Average - vba

The excel user will export the data from an online website to excel (12 months data), so the date will be all the time different.
I create a pivot table, and I have the months and total Average and Frequency. However, I need the 6 months as well, and I am not sure how to get it which time the data changes.
My question: is there any VBA code that will dynamically calculate the past 6 months Average?

One can do this with Formula.
To get the average of the past 6 months:
=AVERAGEIFS('12 Months'!F:F,'12 Months'!A:A,">="&EDATE(TODAY(),-6),'12 Months'!A:A,"<" &TODAY()+1)
To get the frequency:
=COUNTIFS('12 Months'!A:A,">="&EDATE(TODAY(),-6),'12 Months'!A:A,"<"&TODAY()+1)
If one wants the last 12 months, change the -6 to -12 in both formula.

Related

How to calculate standard deviation for 7 day intervals

This is my dataframe, how could I calculate the (standard deviation) * 7for the first seven days, then the next 7 days then the 7 days after that, to get the weekly volatility from the column log returns. And put it into a dataframe, I tried a few functions that I wrote but they dont work.
You could use a rolling window with a time-aware offset (7 days is represented by "7D") using pandas.DataFrame.rolling:
>>> df["log"].rolling("7D").std()
This will look at your datetime index, take current take the last 7 days (including current date/index) and calculate the standard variation of the "log" column by using pandas.DataFrame.std.

Given starting Year and Month as separate columns, how to tell if a specific year and month are within next 6 months?

I have a table A with two columns named Year and Month. I need to join it with another table B also with Year and Month columns. The condition I need to impose is that the month in B is within next 6 months of the month in A. For example, if A.Year=2014 and A.Month=09, then B.Year=2015 and B.Month=01 would be selected because it is within the next 6 months.
I've searched on SO but have not been able to find a solution. This thread
gave me a hint of using Year*100+Month calculations. But I am not sure how to add 6 months to such a calculation easily (guess I could use modulo). Does anyone have a good clean solution to this?
Instead of Year*100+Month simply use Year*12+Month.
WHERE B.Year*12+B.Month BETWEEN A.Year*12+A.Month AND A.Year*12+A.Month +6

extracting common data of current months and last two monnth sql

I have a table with more than 20000 rows, In one of column i have month from jan 2014 to Dec 2014, and in another column i have a loan number. Most of the loan Numbers are reapeting every months,now i need to get only the loan Number which are apperead in all three monthy consecutively. For eg if i am getting data for current months i also wanted get data which are common in two months before the current months. The database that i m using is Access DB. Any adivice will be more than a help, Thanks in Advance.
SELECT Loans.LoanID, Sum(IIf([period]=[month],1,0)) AS CM, Sum(IIf([period]=[month]-1,1,0)) AS [m-1], Sum(IIf([period]=[month]-2,1,0)) AS [m-2]
FROM Loans
GROUP BY Loans.LoanID
HAVING (((Sum(IIf([period]=[month],1,0)))>1) AND ((Sum(IIf([period]=[month]-1,1,0)))>1) AND ((Sum(IIf([period]=[month]-2,1,0)))>1));
I used month as an integer, and didn't make any adjustment for months 1 and 2 to loop back and look at prior year - you should be able to modify this based on the actual format you are using for the month.

Need Excel Sheet to Calculate Complex Vacation Accrual and Use of Vacation Time

I've been working on this for a while and trying different styles... I need to develop a tracking spreadsheet (for 50 employees) that will calculate accruing vacation time and vacation time used based on the following parameters:
*Employees who have worked less than 3 years but past the 90 day anniversary with the company (at which they start accruing) (there is one more caveat on this... the accrual day is the 1st of the month of the month past the 90 days)[I figured this 90 day item out on my excel sheet] it will accrue at:
- 4 hours a month, at the end of the month, with a MAX cap of 72 hours that they can have stored (but if they use vacation time and fall below the 72 hours they can continue to accrue again up to 72 hours...)
*Employees who have worked more than 3 years but less than 6 years with the company carry over their prior vacation and begin to accrue from here onward at:
- 6.8 hours a month, at the end of the month, with a MAX cap of 122.4 hours that they can have stored (but if they use vacation time and fall below the 122.4 hours they can continue to accrue again up to 122.4 hours...)
*Employees 6 years and over with the company carry over their prior vacation and begin to accrue from here onward at:
- 10 hours a month, at the end of the month, with a MAX cap of 180 hours that they can have stored (but if they use vacation time and fall below the 180 hours they can continue to accrue again up to 180 hours....)
& yes, I need to be able to deduct vacation time used.
Does anyone have any suggestions for layout or for a formula that can do part of these functions? I appreciate any advice or suggestion on what else I can use!
I have created a test sheet, and was accruing based on these conditions for the first and started on the second set of rules (for years 3+).
However when I accrue to the max of 72 hours on the first policy, it no longer accrues correctly if they used vacation and fall under the 72 hours cap again.
I know this is a overcomplicated policy, but that is what the company wants and they will not budge.... Any help or advice is appreciated. I know my sheet isn't great.. but I'm trying options.
Below is the equation I used for the 90 day:
=IF(F2<TODAY()-90, F2+90, "90 Day Period")
Then to get the first day of the month after I used:
=IF(G2="90 Day Period","N/A",DATE(YEAR(G2),MONTH(G2)+1,1))
I tried using for the accrual of the first rule (but it has issues...):
=IF(N2="N/A","N/A",IF(N2<=36,MIN(72,((N2*4)-P2),72),(72-P2)))
For second rule:
=IF(AND(N2>36,N2<=72),MIN(122.4,((N2-36)*6.8)+Q2-S2),0)
Let
column A EmpName
column B EntryDate
Column C DaysSpent for the current month
Column D capped Accruel buffer at end of month
repeat Columns C and D month after month
example
01-Sep-2013 01-Oct-2013
EmpName EntryDate spent buffer spent buffer ... etc ...
.-------.-----------.-----------.------.-----------.------.
me 01-Jan-2010 0 12 0 18.8
you 01-Jun-2013 0 4 0 4
In order not to get spaghetti formulas I recommend to create some user defined functions in VBA, like
Function GetCap(EntryDate, ThisDate) As Single
Function GetMonthly(EntryDate, ThisDate) As Single
By experience it's easier to debug/maintain 2-3 nested If's or Select Case's in VBA than 92 character long formulas with no blanks, no comments etc. in the sheet. Should the business logic change, there's one code block to review - instead of dozens/hundreds of formula in a sheet that has grown for 3x12 months x 50 users.
The above functions may want the help of e.g.
Function EndOfMonth(MyDate) as Date
Function BeginOfNextMonth(MyDate) as Date
so that in the sheet you just
manually enter hours spent month after month
calculate new buffer as = MIN([oldbuffer] - [Spent] + GetMonthly(...), GetCap(...))
carefully use relative/absolute addressing to make the formula "copyable" across columns/rows, e.g.
row-absolute on ThisDate got from the header when copying downwards
column-absolute on EntryDate for each Emp when copying rightwards
You can of course use =GetCap(...) and =GetMonthly(...) directly in cells of your sheet to display intermediate results and for debugging purposes.
Be carefull when you compare dates
Tips:
3 years later is not always 365x3 days later
check what the VBA DateSerial() functions does for months > 12 and months < 0
the end of next month always is the first days of 2 months ahead minus 1 ... even in February of a leap year ggg
and post more questions if you get stuck on these functions.

SSRS - How to get subtotals for a set of 12 records in a column

I am novice with SSRS.
I have a report which should display 60 months (5 years displayed like Jan 1999, feb 1999 & so on till the end of 60 months) and its corresponding sales amount. I want to get averages for the first 12 months (i.e for the 1st year) and so on. Is it possible? My dataset just gives me all the 60 months row-by-row.I am using matrix for my report.
Thanks,
User007.
I would add some nested row grouping to the matrix. The higher-level group would be by year, which would allow you to have a row in the matrix that totals/averages all of the data for the year. The inner group would be by month, giving you the individual month rows as your dataset returns them.
Here is some information about defining and using groups