T-SQL: Get Current Fiscal Year Dates

Quite often in financial data analysis we have the scenario where we need to do the reporting based on fiscal year.
Fiscal year may vary from one organization to another. The reporting period may be year on year, month on month etc. I have written a generic function which returns the fiscal dates for the current fiscal year. This can be utilized in requirements like above to generate the fiscal date values and utilize them for filtering the data in the reports.
The function looks like below

CREATE FUNCTION  [dbo].[GetFiscalDates]
(
@FiscalStartMonthNo int,
@Period varchar(2) = 'FY'--values FY,Q,M
)
RETURNS TABLE
AS
RETURN
With CTE
AS
(
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()) + @FiscalStartMonthNo - MONTH(GETDATE())-CASE WHEN  @FiscalStartMonthNo > MONTH(GETDATE()) THEN  12 ELSE  0 END,0) AS  StartDate
)
SELECT StartDate,DATEADD(yy,1,StartDate)-1 AS EndDate,'FY'  + DATENAME(yyyy,DATEADD(yy,1,StartDate)-1) AS Period
FROM CTE t
WHERE @Period = 'FY'
UNION ALL
SELECT DATEADD(mm,3*(N-1),StartDate),DATEADD(mm,3*N,StartDate)-1 AS  EndDate,'FY' + DATENAME(yyyy,DATEADD(yy,1,StartDate)-1) + ' Q' +  CAST(N AS varchar(2)) AS  Period
FROM CTE t
CROSS JOIN (VALUES (1),(2),(3),(4))M(N)
WHERE @Period = 'Q'
UNION ALL
SELECT DATEADD(mm,N-1,StartDate),DATEADD(mm,N,StartDate)-1  AS  EndDate,'FY' + DATENAME(yyyy,DATEADD(yy,1,StartDate)-1) + ' M' +  CAST(N AS varchar(2)) AS  Period
FROM CTE t
CROSS JOIN (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12))M(N)
WHERE @Period = 'M'
 
GO

The function accepts two parameters
FiscalStartMonthNo which designates the start month of the fiscal year for the organization  and
Period which indicates period for which date value are to be returned like fiscal year dates, quarter dates etc
The function may be invoked as below:

SELECT * FROM dbo.GetFiscalDates(5,'FY')

To get fiscal year dates for organization starting from 5th month and the resultset will look like below:

StartDate                          EndDate                         Period

2014-05-01 00:00:00.000 2015-04-30 00:00:00.000 FY2015

Similarly to get quarter and month dates invoke UDF as below:

SELECT * FROM dbo.GetFiscalDates(5,'Q')
 
SELECT * FROM dbo.GetFiscalDates(5,'M)

This UDF can be utilized whenever you want to generate fiscal period dates on the fly for filtering the data for interested fiscal year period.