Date parameter expression(s) - SSRS - reporting-services

I am struggling to work out how to get an SSRS report's date parameters to default to the:
Date 1: Last day of the previous month - in the previous year.
so for example today = 22/06/2021 - I'd need 31/05/2020 to appear
Date 2: Last day of the current month - in the previous year.
So for example today = 22/06/2021 - I'd need 30/06/2020 to appear.
any assistance gratefully received. Thanks.

I always find it is easier to first change the date to the first of the current month and go from there. I used to use DATEADD but have started using the VB.NET way - it's actually easier for complicated calculations like this.
For the first of the current month:
=DATEADD("d", 1 - DAY(TODAY))
OR
=TODAY.AddDays(1 - TODAY.Day)
Then subtract a year:
=DATEADD("y", -1, DATEADD("d", 1 - DAY(TODAY)))
OR
=TODAY.AddDays(1 - TODAY.Day).AddYears(-1)
Then subtract a day from that to get the end of the previous month:
=DATEADD("d", -1, DATEADD("y", -1, DATEADD("d", 1 - DAY(TODAY))))
OR
=TODAY.AddDays(1 - TODAY.Day).AddYears(-1).AddDays(-1)
For the last day of current month in previous year, a month should be added before subtracting the day:
=DATEADD("d", -1, DATEADD("M", 1, DATEADD("y", -1, DATEADD("d", 1 - DAY(TODAY)))))
OR
=TODAY.AddDays(1 - TODAY.Day).AddYears(-1).AddMonths(1).AddDays(-1)
The VB.NET is easier to read and can be calculated as you read it while reading the nested DATEADDs is more difficult.

For the fist expression use
=
DateAdd(
DateInterval.Day,
day(today()) * -1,
DateAdd(DateInterval.Year, -1, today())
)
for the 2nd use
=
DateAdd(
DateInterval.Day,
Day(today()) * -1,
DateAdd(
DateInterval.Month,
1,
DateAdd(DateInterval.Year, -1, today())
)
)
In the first expression starting in he middle and working outwards.
Use DateAdd to subtract 1 year from today's date, then subtract the current day number (22) from this.
The 2nd is..
Subtract 1 year from today's date, then add one month to it, then finally subtract the current day number.

Related

SSRS Default Date previous Year first day and last Day

How do I get the first and last day of the previous year in SSRS for default dates
tired wth DATEADD function but getting an error
You can use DATESERIAL() to do this easily
DateSerial just take 3 integer values (year, month, day) to return a date.
First day
=DateSerial( year(today()) - 1, 1, 1)
Last Day
=DateSerial( year(today()) - 1, 12, 31)
You don't show what you tried so we can't explain what you are doing wrong.
If I needed to get the 1st day of the previous year, I would take the current year, subtract the number of days from 1 and then subtract a year.
With DATEADD, the first day of last would be:
=DATEADD("yy", 1, DATEADD("d", 1 - DATEPART("dy", TODAY), TODAY))
While the last day is actually simpler:
=DATEADD("d", 0 - DATEPART("dy", TODAY), TODAY)

How to calculate previous month from current month in SSRS with format MM-YYYY

i want to calculate previous months from current month in SSRS with the format like this: MM-YYYY, for example: 10-2016,11-2016
Can you please help me how to do it?
Thanks
You can do it like this for month previuos to current month:
=Format(DateAdd(DateInterval.Month, -1, Today), "MM-yyyy")
First you DateAdd(DateInterval.Month, -1, ...) substract one month from the date you specified (...), and then format the result date. To get required month you need to set -1, -2, ..., -n appropriately.

SSRS Month over Month Date Expression beginning with Previous business day

The business requirement is to have a report with a month over month date starting with the previous business day. How do I achieve?
For month over month - my expression is:
=dateadd("m",-3,Today)
For previous business day - my expression is:
=DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())
How can I combine these into one expression so that the month over month starts with the previous business business day and not today's date?
Thanks,
You can use your previous business day expression in the start business day expression:
=dateadd("m",-3,
DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())
)
However the outer DATEADD function could produce a date which is not business day and your query will return rows from that date, if your datasource doesn't match dates in non-business days that is not a problem, the problem comes when you have non-business dates and you don't want to report it.
For simplicity I'd create a Hidden parameter (check the Hidden property radio control in Parameter properties) called ThreeMonthsPreviousDay, set it to Date/Time data type and use the the below expression in the Default Values property.
=DateAdd(DateInterval.Month,-3,
DateAdd(DateInterval.Day
, Switch(DatePart(DateInterval.WeekDay, Today()) = 2, -3
,DatePart(DateInterval.WeekDay, Today()) = 1, -2
,True, -1)
, Today())
)
Now you can get the Business Date 3 months before the previous business day so use:
=DateAdd(DateInterval.Day,
Switch(
DatePart(DateInterval.WeekDay, Parameters!ThreeMonthsPreviousDay.Value) = 2, -3,
DatePart(DateInterval.WeekDay, Parameters!ThreeMonthsPreviousDay.Value) = 1, -2,
True, 0),
Parameters!ThreeMonthsPreviousDay.Value)
Let me know if this helps.

Report Builder 3 Creating a current, 30, 60, date range parameter to generate reports

I am new to Report Builder and I have been tasked with creating a date range search parameter. They are looking for current, 30 day, and 60 day values. The overall report is working and I have only made a few changes to it. So in parameters I made a dateRange parameter and set data type up as Date/Time and no boxes are check below. in the available values properties I have specified 3 values:
Current: =DateAdd("d", -1, Today())
30 day Range: =DateAdd("d", -30, Today())
60 day Range: =DateAdd("d", -60, Today())
Default values and advanced properties have remained untouched.
Why did I go with those expressions? I was looking at the StartDate and EndDate values and the enddate value has =DateAdd("d", -1, Today()) and so my logic said going back 30 and 60 days i would need add a neg in front. This I think is errant thinking. The customer is looking for the parameter to return the the values starting from the first day of the month so I need to make sure I use a month.minValue to have the report always start on the first day of the month. I hope all of this makes sense. Again I am very new to Report Builder so forgive me for my ignorance.
Jim
Based on your question and feedback comments, this is what I got.
Define DateRange parameter as follows:
Available values:
=DateAdd("d", -1, Today())
=DateAdd("d", -30, Today())
=DateAdd("d", -60, Today())
For use current =DateAdd("d", -1, Today()) as default you must to put it in the default values tab of DateRange parameter.
Now for handle the end date I've created other parameter named EndDate set it default value to current:
=DateAdd("d", -1, Today())
Now in the dataset query you have to use the parameter DateRange to get the first date of month. Check this example.
select *
from mytable a
where a.DocDate between DATEADD(month, DATEDIFF(month, 0, #DateRange), 0)
and #EndDate
DATEADD(month, DATEDIFF(month, 0, #DateRange), 0) will produce the first date of month based on the daterange selection example using
today date:
current: 01/10/2015 (dd/MM/yyy format)
30 days range: 01/09/2015
60 days range: 01/08/2015
EndDate parameter will produce 26/10/2015
Let me know if this was helpful

SSRS 2008 week number calculation of current month

I need to construct a data parameter in SSRS 2008 where if the current week number is 1 then I use the first day of the previous month and if not then I use the current day.
I.e. today is week 4 therefore would utilize today's date
If today was march 2, then the week would be week 1 and I would utilize the first of the previous month, feb 1
note: weeks must follow calendar weeks.
Thanks in advance for your assistance
You can set the parameter default using the following expression:
=IIf(Day(Today()) <=7
and DatePart(DateInterval.WeekDay, Today(), FirstDayOfWeek.Monday)
>= DatePart(DateInterval.WeekDay, DateSerial(Year(Today()), Month(Today()), 1), FirstDayOfWeek.Monday)
, DateAdd(DateInterval.Month, -1, DateSerial(Year(Today()), Month(Today()), 1))
, Today())
So:
If today is one of the first seven days of the month, and the day of the week number is >= the day of the week of the first of the month
=> First week, so use the first day of the previous month
=> Else use the current date.