Excel 2016 calculate fiscal year - excel

Fiscal Year starts July 1 - June 30, I need to calculate and display the number of Fiscal Years between 2 dates.
For example: 01/01/2018-12/31/2018. Result (YYYY-YYYY) should give 2 fiscal years:
2017-2018 (July 1, 2017 - June 30, 2018)
2018-2019 (July 1, 2018 - June 30, 2019)
Its easy to calculate if there are only 2 fiscal years between 2 dates but using =IF(StartDate<DATE(YEAR(StartDate), 7,1),CONCATENATE(YEAR(StartDate)-1, "-", YEAR(StartDate)), CONCATENATE(YEAR(StartDate), "-", YEAR(StartDate)+1)) or
=IF(EndDate<DATE(YEAR(EndDate), 7,1),CONCATENATE(YEAR(EndDate)-1, "-", YEAR(EndDate)), CONCATENATE(YEAR(EndDate), "-", YEAR(EndDate)+1))
But how do I capture if there are more than 2 fiscal years? For example: 01/01/2018 - 09/20/2019
I would prefer the fiscal year be entered in a table range and automatically insert a new row if there are more than 2 fiscal years. I'm looking for either formula or vba solution.

last time i had to do this was for a specific report where the dates are written in cells:
dim s as long, e as long
if month(cells(1,1).value) >= 7 AND day(cells(1,1).value) >= 1 Then
s = year(cells(1,1).value)+1
else
s = year(cells(1,1).value)
end if
if month(cells(2,1).value) >= 7 AND day(cells(1,1).value) >= 1 Then
e = year(cells(1,1).value)+1
else
e = year(cells(1,1).value)
end if
'FY range:
cells(3,1).value = "FY" & s & "-FY" & e
rehashed the code for you, untested

Related

Identify Gap Years Between Multiple Dates in Excel

For example, I have these list of dates (These are of type 'date')
Sept 1, 2010
July 1, 2016
July 1, 2022
The gap years between these dates are 6 years. Another Example:
Sept 1, 2010
July 1, 2012
Sept 1, 2014
July 1, 2016
The gap years between these dates is 2. How will I make a formula to identify the gap years?
You should check out DATEDIF function:
https://support.microsoft.com/en-us/office/datedif-function-25dba1a4-2812-480b-84dd-8b32a451b35c
You can create a new column:
Gap_row = Previous Row - Current Row
D2 = IFERROR(IF(D1<0;0;C2-C1);0)
Then create a pivot table for year and show Max(Gap_Row).
The Grand total for Max of Year Gap is the Year Gap for those rows:
Starting at your second date, just do =YEAR(A2)-YEAR(A1)

How to get the Week of the month (1:6)

We can find different approaches to determining the week of the month, and even though there are many pages on 1:4 and/or 1:5, there is very few around 1:6 approach.
So to give you a bit of the context, I am working with a pivot table in Excel which gets its values from a Power Query source.
In Power Query, there is a function Date.WeekOfMonth which takes in the date and returns a number between 1 and 6.
In this definition, weeks start from Sunday and ends on Saturday.
So, for example, the first two days of October 2021 -i.e. Fri & Sat- fall in the 1st week of Oct, while the 3rd day of Oct 2021 starts the second week, and then the last day of October 2021,i.e. Oct 31, is the only day in the 6th week.
I had an automation task on hand in which I needed to pull data from the Power Query-generated pivot, so I had to implement a piece of code in VBA which calcs weeks the same.
Unfortunately, I couldn't find any prepared snippet, so after implementing I though it might worth sharing.
(Any comments and suggestions appreciated)
The DatePart function is perfect for this. Using DatePart with the interval set to "weeks" you can find the week of a given date. Then you subtract the number of weeks before the first day of that month (which sets the first day to week = 1).
Function WeekNumOfDate(D As Date) As Integer
WeekNumOfDate = DatePart("ww", D) - DatePart("ww", DateSerial(Year(D), Month(D), 1)) + 1
End Function
Here is a second version of the function that has the ability to set the FirstDayOfWeek argument:
Function WeekNumOfDate(D As Date, Optional FirstDayOfWeek As VbDayOfWeek = vbSunday) As Integer
WeekNumOfDate = DatePart("ww", D, FirstDayOfWeek) - DatePart("ww", DateSerial(Year(D), Month(D), 1), FirstDayOfWeek) + 1
End Function
As an example for using FirstDayOfWeek: With FirstDayOfWeek set to vbThursday, the date "Nov 5th, 2021" will return as Week 2, whereas it would by default be counted as Week 1. November 1st to 3rd of 2021 will be week 1, and then 4th to 10th will be week 2.
Its implementation in VBA is:
Function WeekOfMonth(My_Date As Date)
If Day(My_Date) > Day(My_Date - 1) And Weekday(My_Date) > Weekday(My_Date - 1) Then
WeekOfMonth = WeekOfMonth(My_Date - 1)
ElseIf Day(My_Date) > Day(My_Date - 1) And Weekday(My_Date) < Weekday(My_Date - 1) Then
WeekOfMonth = WeekOfMonth(My_Date - 1) + 1
Else
WeekOfMonth = 1
End If
End Function
Note that even though the above function is recursive, its time and space complexity is an expression of order N, which here cannot exceed 31.

Number of days in between given two dates treating number of days in month as fixed 30 days

I need to calculate difference between two dates with a fixed 30 days a month logic. The example is shown below
Start date = 10/4/2018
End date = 28/10/2018
Expected number of days between = 199
(Excel calculation of difference of days => 28/10/2018 - 10/4/2018 = 201 which is not what I need)
The basis for calculation of difference between two dates is, it should consider number of days in a month as 30 days irrespective of the month. So all months in between the start & end dates with 31 days should be treated as 30 days. If there is Feb in between, it should also be taken as 30 days month.
Procedure to calculate number of days in between two given dates:
Fraction of days in the starting month = 30/4/2018 - 10/4/2018 = 21 days
Months in between 1/5/2018 to 30/9/2018 = 5 months = 5 x 30 = 150 days
Fraction of days in the last month = 28/10/2018 - 1/10/2018 = 28 days
Total days = 21 + 150 + 28 = 199 days.
If A1 is start date cell, B1 = End date cell, please suggest how to do it in excel.
I've broken the formula into the three components as in your example:
The formulas in D2:G2 are:
D2: =IF(AND(YEAR(A2)=YEAR(B2),MONTH(A2)=MONTH(B2)),MIN(DATE(YEAR(A2),MONTH(A2),30),B2),DATE(YEAR(A2),MONTH(A2),30))-MIN(DATE(YEAR(A2),MONTH(A2),30),A2)+1
E2: =MAX(((YEAR(B2)-YEAR(A2))*12+MONTH(B2)-MONTH(A2)-1)*30,0)
F2: =MIN(IF(AND(YEAR(B2)=YEAR(A2),MONTH(B2)=MONTH(A2)),0,B2-DATE(YEAR(B2),MONTH(B2),1)+1),30)
G2: =SUM(D2:F2)
or, all in one:
=IF(AND(YEAR(A2)=YEAR(B2),MONTH(A2)=MONTH(B2)),MIN(DATE(YEAR(A2),MONTH(A2),30),B2),DATE(YEAR(A2),MONTH(A2),30))-MIN(DATE(YEAR(A2),MONTH(A2),30),A2)+1+MAX(((YEAR(B2)-YEAR(A2))*12+MONTH(B2)-MONTH(A2)-1)*30,0)+MIN(IF(AND(YEAR(B2)=YEAR(A2),MONTH(B2)=MONTH(A2)),0,B2-DATE(YEAR(B2),MONTH(B2),1)+1),30)
I like the elegance of #Jeeped 's code, but this might be easier to follow.
Try this,
=SUMPRODUCT(--(DAY(ROW(INDIRECT(A2&":"&B2)))<>31))+
(DAY(A2)=31)+
SIGN(SUMPRODUCT((MONTH(ROW(INDIRECT(A2&":"&B2)))=2)*(DAY(ROW(INDIRECT(A2&":"&B2)))=28)))*2
It seems to me that your examples are a little "wonky", like how the 31st counts sometimes (like if it's the start/end date) but not others.
Anyway, this function ain't pretty but it does the trick:
Function No31st(startDate As Date, endDate As Date) As Long
Dim d As Date
For d = startDate To endDate 'iterate each day in range
If Day(d) <= 30 Then 'count days up to the 30th
No31st = No31st + 1
'if it's Feb 28, add 2 more days
If (Month(d) = 2 And Day(d) = 28) Then
No31st = No31st + 2 ' + Feb 29 + Feb 30
If Day(d + 1) <> 1 Then d = d + 1 'leap year
End If
End If
Next d
'since the 31st counts if it's the last day:
If Day(startDate) = 31 Or Day(endDate) = 31 Then No31st = No31st + 1
End Function
Sample Output:
StartDate endDate result
10/4/2018 28/10/2018 199
31/1/2018 31/3/2018 61
28/2/2018 1/3/2018 4
28/2/2000 1/3/2000 4
31/1/2018 1/2/2018 2
The DAYS360 formula is based on the 12 30-day month logic.
Simple & Easy Ways:
(1)
=YEARFRAC(start_date,end_date,4)*360
(2)
=DAYS360(start_date,end_date)

Creating flags for calculating rolling averages in excel

I have a data sheet which looks like:
year month
2017 2017-01
2017 2017-02
2017 2017-03
......
2018 2018-04
2018 2018-05
Note the column month contains text string. I need to create a new column flag in excel which filters which months are to be used in calculating rolling 12M averages for current month and which months are to be used for calculating rolling 12M averages for previous year.
For example today's date is 5/24/2018, so month 2018-05 will be marked as "current". months between 2018-04 to 2017-05 will be marked as "both". month 2017-04 will be marked as "previous". Rest all months will be marked as NA. My final data should look like this:
year month flag
2017 2017-01 NA
2017 2017-02 NA
2017 2017-03 NA
2017 2017-04 Previous
......
2018 2018-04 Both
2018 2018-05 Current
I am having trouble implementing this logic in excel as the column month is a text string and simply using IF condition doesn't works for me. Any leads on this is appreciated.
Edit:
I tried converting the months to a number by using =DATEVALUE(B2 & "-01").
I stored the number for current month in a variable curr.
Now I am using the following formula =IF(B22=curr,"Current", IF(B2=curr-395,"Previous","Both")).
This creates the flag column which I require although there's still an issue based on whether the month has 30 or 31 days. Any solutions for this please?
One way to do it is to use DATDIF.
=DATEVALUE(B2 & "-01") will give you the first of each month in column B.
To return the month number use =DATEDIF(C2,TODAY(),"m") where column C holds the DATEVALUE formula. This will return 0 for this month, 1 for last month... 13 for April 2017.
Now check to see if the count of months = 0 then it's current, if it's greater than 11 it's NA, otherwise it's both. =IF(D2=0,"Current",IF(D2>11,"NA","Both"))
... Edit ....
That's wrong - forgot the "Previous" and "Both" isn't 12 months ....
=IF(D2=0,"Current",IF(D2>13,"NA",IF(D2=13,"Previous","Both")))
As considering your column month format = "YYYY-MM" this code may be helpful
this is Excel function, to use it you need to save this function in the module and use it. `
Function MyDateCal(cell As Range) As String
Dim YearCurrent, YearCell, monthCurrent, MonthCell As Integer
Dim cellVal As String, DiffMonth As Integer[![enter image description here][1]][1]
cellVal = cell.Value
MyDateCal = "NA"
'cellVal = Cells(1, 1).Value
YearCurrent = Year(Now())
monthCurrent = Month(Now())
YearCell = CInt(Left(cellVal, 4))
MonthCell = CInt(Right(cellVal, 2))
DiffMonth = 12 * (YearCurrent - YearCell) + (monthCurrent - MonthCell)
If DiffMonth = 0 Then
MyDateCal = "Current"
ElseIf DiffMonth > 0 And DiffMonth <= 12 Then
MyDateCal = "Both"
ElseIf DiffMonth = 13 Then
MyDateCal = "Previous"
End If
End Function
`

How to get Quarters for financial year based on date

I'm trying to display quarters based on the financial year: Q1 starting in April, Q2 from July, etc.
In column B there is a list of dates, and in column A I want the corresponding quarter & year. There are 2 parts I can't figure out:
currently for 01 JAN 2018 it shows Q4 2018. Although the date is 2018, it's the final quarter of 2017 and should show Q4 2017
how to get the display format like Q1 17/18 (eg)
At the moment I have:
=CHOOSE(INT((MONTH(B1)-1)/3)+1,"Q4","Q1","Q2","Q3") & " " & YEAR(B1)
Is it possible without any helper columns?
Subtract a Boolean expression from the Year part:
YEAR(B1) - (MONTH(B1)<4)
If the MONTH is Less than 4 it will subtract 1 from the year, otherwise it will subtract 0:
=CHOOSE(INT((MONTH(B1)-1)/3)+1,"Q4","Q1","Q2","Q3") & " " & YEAR(B1) - (MONTH(B1)<4)

Resources