SSRS parameter default to most recent 1st April - sql

I have a requirement for an SSRS parameter that will be for a start date of the most 1st April, so today it will report from 1/4/2015 with 10 weeks data, but after the 1/4/16 it will default to 1/4/16.
Can anyone help with this please?
I can find most recent month, or current month etc, but this one is a little tricky. Thankyou.
I am using SQL Server 2012 and Visual Studio 2010

You could use the following expression:
=IIF(Today() >= DateSerial(Year(Today()), 4, 1), DateSerial(Year(Today()), 4, 1), DateSerial(Year(Today()) - 1, 4, 1))
The idea is to check if the 1st of April of the current year has been reached (Today() >= DateSerial(Year(Today()), 4, 1)).
If yes, take the 1st of April of the current year: DateSerial(Year(Today()), 4, 1)
If no, take the 1st of April of the previous year: DateSerial(Year(Today()) - 1, 4, 1)

Related

How can I create a new column for Fiscal Year based on custom date ranges in SQL?

I'm looking to create a new column that includes fiscal year based on custom ranges of the Date of Distribution field (See my query below) in SQL. This is probably relatively simple but I've played around a bit with no luck.
Values I wish to create and range definitions:
FY20 = July 1, 2019 to June 30, 2020
FY21 = July 1, 2019 to June 30, 2020
A very simplified version of my current query:
Select
TRANSACTION_Table.DATE_OF_DISTRIBUTION
Datename(Month, TRANSACTION_Table.DATE_OF_DISTRIBUTION) as 'Transaction Month',
TRANSACTION_Table.AMOUNT
From
Transaction_Table
Where
Transaction_Table.Date_Of_Distribution between '7/1/2019' And '6/30/2021'
Thank you in advance!
The simplest way is to add 6 months and extract the year. In Standard SQL, this looks like:
extract(year from Date_Of_Distribution + interval '6 month')
Date/time functions are notoriously database dependent, so the exact syntax depends on the database you are using.
For instance, in SQL Server, it would be:
year(dateadd(month, 6, Date_Of_Distribution))

Access Report Year should be change after April every Year

The session is starting from every year from April to March.
The report needs to publish in twice in that year first report in October and another in March.
In both conditions year will be 2019-20.
After March session will change then the year will also automatically changed 2020-21.
How to resolve it?
Below expression will not work on this condition:
Format(Date(),"yyyy-") & Right(Year(Date())+1,2)
That needs an adjustment to the fiscal year, which can be done like this:
=Format(Year(DateAdd("m",-3,Date()))*100+(Year(DateAdd("m",-3,Date()))+1) Mod 100,"0000-00")
This offsets the date of the fiscal year to match a pseudo calendar year, thus allowing you to extract year and month as for any calendar year. The first year is returned in full and multiplied with 100: 201900 and added to the decade part of the second year: 201900 + 20 = 201920. This is finally formatted to have a dash: 2019-20.
So reports between April 2019 and Mar 2020 should show 2019-20.
Use conditional IIf:
IIf(Month(Date()) < 3, Year(Date()) - 1 & "-" & Format(Date(),"YY"), Year(Date()) & "-" & Right(Year(Date()) + 1, 2))
Problem with using Date() to calculate this is if report for March is run on April 1, calculation will be wrong. Might be best to reference textbox on form for beginning year.

Why is DATEDIFF parsing these dates as dd/mm/yyyy?

I do:
SELECT DATEDIFF(month, Convert(datetime,'01/10/2018',103),
Convert(datetime,'30/04/2019',103)) AS 'Month1',
DATEDIFF(month, '10/01/2018','04/30/2019') AS 'Months2',
DATEDIFF(month, '10/02/2018','05/01/2019') AS 'Months3'
Please show me why it return 6,6,7?
here date format is day month year
DATEDIFF(month, Convert(datetime,'01/10/2018',103),
Convert(datetime,'30/04/2019',103)) AS 'Month1'
so difference of 10 month(october) and 4(april) month =6
below code date format is month day year
DATEDIFF(month, '10/01/2018','04/30/2019') AS Months2,
so again 10(october) and 4(april) month difference is 6
and the 3rd date format is like 2nd
DATEDIFF(month, '10/02/2018','05/01/2019') AS Months3
so difference between 10(october) 2018 and may(5) 2019 is 7
As a result your query showing output 6,6,7
The first 6 is the difference (in months) between 01/10/2018 (October 1, 2018) and 30/04/2019 (April 4, 2019). The CONVERT call is to force the second date to be parsed using date format 103 (i.e. British/French, i.e. dd/mm/yyyy), which makes me suspect that your SQL implementation is set up to parse them as some other format (e.g. US: mm/dd/yyyy).
The second 6 is the difference (in months) between 10/01/2018 (October 1, 2018) and 04/30/2019 (April 30, 2019).
The 7 is the difference (in months) between 10/02/2018 (October 2, 2018) and 05/01/2019 (May 1, 2019). The fact that these dates could be parsed as Feb 10 and Jan 5, but aren't, confirms that your SQL implementation is parsing dates in mm/dd/yyyy format.
If you are really passing dates as strings, I recommend using an unambiguous format (e.g. "October 1, 2018"), as it will make the code clearer, and less brittle.

SQL - Getting an integer relative to a date column

I have a date column called "Expiry_Date". What I've been asked to do is retrieve an integer based on the month relative to the current, for example:
Current Date: 7th May 2016
MyTable
----------
1) 12th May
2) 31st May
3) 2nd June
4) 11 June
5) 19 June
6) 28th July
7) 1st August
The result would be:
1) 1
2) 1
3) 2
4) 2
5) 2
6) 3
7) 4
So, it's getting the month number relative to the current for the given date (or periods). This also has to work across years so "1 January 2017" would be "9" etc.
Many thanks
Perhaps the function MONTHS_BETWEEN solves.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions089.htm
Something like:
round(months_between(expiry_date, date '2016-05-07') + 1)

Lookup a value from a range of dates in excel

Please help.
We have two tables. A list of accounts and the other is a change log. I need to add a new column in table 1 where the value is the amount in table 2 for the correct account and correct validity period.
ex.
table 1:
account# beginning period ending period
1 January 1, 2012 January 31, 2012
2 January 12, 2012 February 12, 2012
table 2:
account # amount valid period beg valid period end
1 10 january 1, 2009 december 5, 2010
1 20 december 6, 2010 june 1, 2011
1 30 june 2, 2011 december 1, 2012
2 13 january 15, 2011 december 15, 2011
2 20 december 16, 2011 february 20, 2012
Thanks.
Although it is a bit complex requirement it could be done with built-in functions (although it can look a bit obscure :-) ). Specifically I mean function SUMIFS.
It has several parameters.
The first one is an area with values to be summed. It is B8:B12 in this example.
The second is an area whith values to be checked with some condition. It is A8:A12.
The third is a criterion to be applied for area from second parameter. It is (inter alia) account #.
So the formula says: sum all values from rows in B8:B12 where account # (A8:A12) is equal to desired account # (e.g. A3).
Ok, it is not all, you need specify the time range. It would be a bit clunky because you must check if two time period are overlapping (it would be easier to check if one date is in specified period).
But it could be done because SUMIFS can take another pairs of criteria range and criterion. But it cannot be used for OR condition, so you had to combine more SUMIFS.
Nice article about overlapping ranges is e.g. http://chandoo.org/wp/2010/06/01/date-overlap-formulas/
BTW: you have to format cells in B2:C3 and C8:D12 as a date to be able to compare them.