So the Fiscal Year for this company is Apr - Mar
They would like to see MTD and YTD from last fiscal year and this fiscal year for comparison reporting.
[code]
= (CASE WHEN MONTH(GETDATE()) < 3 THEN DATEFROMPARTS(YEAR(GETDATE())-1 , 3 , 31) ELSE DATEFROMPARTS(YEAR(GETDATE()) , 3 , 31) END)
select
(CASE WHEN MONTH(GETDATE()) < 3 THEN CAST(CAST(YEAR(GETDATE())-1 AS VARCHAR(4))+'0331' AS DATETIME)
ELSE CAST(CAST(YEAR(GETDATE()) AS VARCHAR(4))+'0331' AS DATETIME) END) as dt_1
,(CASE WHEN MONTH(GETDATE()) < 3 THEN CAST(CAST(YEAR(GETDATE()) AS VARCHAR(4))+'0331' AS DATETIME)
ELSE CAST(CAST(YEAR(GETDATE())+1 AS VARCHAR(4))+'0331' AS DATETIME) END) as dt_2
Perhaps the code I had is incorrect.
I tried what you've shared and I'm only getting March 31, 2018 data.
Need MTD and YTD from LAST fiscal year and THIS fiscal year, rolling forward.
So it should return
4/1/2017 - 4/16/2017 AND 4/1/2018 - 4/16/2018 today. and so on
where
next month, say on the 11th, it should return
5/1/2017-5/10/2017 AND 5/1/2018-5/10/2018 for MTD
AND 4/1/2017-5/10/2017 plus 4/1/2018-5/10/2018
and continue through the end of the fiscal year then repeat but next Apr it'll show 2018 and 2019, etc ...
How about this, what's the correct syntax for:
Last year - so something like DateAdd(yyyy, -1, Getdate())?
Last year Month to be - 4/1/previousyear using the above for the year to date so 4/18/previous year
This year - so DateAdd(yyyy, 0, Getdate())
current month to date - DATEADD(MONTH, DATEDIFF(MONTH, 0, GetDate()-1), 0) this provides current month to date
CASE WHEN { fn DAYOFWEEK(dbo.SO_SalesOrderHistoryHeader.OrderDate) } IN (1 , 7) THEN dbo.DAYSADDNOWK(OrderDate , 1) ELSE DATEADD(dd , 0 , DATEDIFF(dd , 0 , dbo.SO_SalesOrderHistoryHeader.OrderDate)) END
CASE WHEN { fn DAYOFWEEK(dbo.SO_SalesOrderHistoryHeader.OrderDate) } IN (1 , 7) THEN dbo.DAYSADDNOWK(OrderDate , 1) ELSE DATEADD(dd , 0 , DATEDIFF(dd , 0 , dbo.SO_SalesOrderHistoryHeader.OrderDate)) END
And then a way to get 4/1/lastyear to date last year and 4/1/thisyear to date in the current year as we go into other months?
Meaning next month will have 4/1/201# - May, etc ... in both years?
Set it to always start on 4/1/#### since that's the start of the fiscal year.
Thanks again!
Here's the full, in case someone else needs it like I did
Last Fiscal Year beginning April where weekends are marked as Monday = (CASE WHEN { fn DAYOFWEEK(dbo.SO_SalesOrderHistoryHeader.OrderDate) } IN (1, 7) THEN dbo.DAYSADDNOWK(OrderDate, 1) ELSE DATEADD(dd, 0, DATEDIFF(dd, 0, dbo.SO_SalesOrderHistoryHeader.OrderDate)) END BETWEEN DATEADD(year, - 1 + DATEDIFF(month, 90, GETDATE()) / 12, 90) AND DATEADD(YEAR, - 1, GETDATE() - 1))
OR
This Fiscal Year beginning April where weekends are marked as Monday = (CASE WHEN { fn DAYOFWEEK(dbo.SO_SalesOrderHistoryHeader.OrderDate) } IN (1, 7) THEN dbo.DAYSADDNOWK(OrderDate, 1) ELSE DATEADD(dd, 0, DATEDIFF(dd, 0, dbo.SO_SalesOrderHistoryHeader.OrderDate)) END BETWEEN DATEADD(year, DATEDIFF(month, 90, GETDATE()) / 12, 90) AND GETDATE() - 1)