SSRS Default Date previous Year first day and last Day - reporting-services

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)

Related

Date parameter expression(s) - SSRS

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.

MDX to get last Sunday and Saturday

I built a SSRS 2005 report on a SSAS 2005 cube. The report has start date and end date parameters from Time dimension. I need to set the default values to be last Sunday and last Saturday separately. (financial week is from Sunday to Saturday)
E.g.
Start date: [Time].[Day].&[20140309]
End date: [Time].[Day].&[20140315]
How can I do it dynamically? I mean for this week is above dates, but for next week, it should be 16 March, 22 March. I know how to do it in T-SQL, which will involve some calculation with system date, but MDX?
You can use some VBA Date functions available in MDX:
StrToMember('[Time].[Day].&['
+ Format(DateAdd('d', - DatePart('w', Now(), 1), Now()), 'yyyyMMdd')
+ ']'
)
should give you the last Saturday before today, and
StrToMember('[Time].[Day].&['
+ Format(DateAdd('d', - DatePart('w', Now(), 1) - 6, Now()), 'yyyyMMdd')
+ ']'
)
the last Sunday before that.
The second argument of DateAdd('d', ...) is the number of days to add. And as this is negative here, we go back in time that many days. DatePart('w', ...) returns the weekday number (Sunday = 1, Monday = 2, ...). Thus, if you subtract "weekday number" days from today, luckily you are already at last Saturday. And subtracting six more days, you arrive at the last Sunday before that.

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.

Is there a way to get the First day of the previous month and Last day of current month?

Here I need a function in MySQL where it returns
Dates
Starting date of previous month
Last date of current month.
starting date of previous month
select date_format(curdate() - interval 1 month,'%Y-%m-01 00:00:00')
last date of current month
select date_format(last_day(curdate()),'%Y-%m-%d 23:59:59')
You would use NOW() to get the current date and time. MONTH() to get the current month. And using that value, you can get the previous month and the next month. see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Last day of current month:
select last_day(now())
Starting date of previous month:
select adddate(subdate(last_day(now()), interval 2 month), 1)

First and Last day of current month

In SQL Server Reporting Services; How can I calculate the first and last day of the previous month?
I know I can use the expression below to get the last day of the current month, but I'm stuck when trying to find the first and last of the previous month.
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)
Just a guess based on your code working.
--previous month last
=DateSerial(Year(Now()), Month(Now()), "1").AddDays(-1)
--previous month first
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(-1)
First day of this month:
=dateadd("m",0,dateserial(year(Today),month(Today),1))
Last day of this month:
=dateadd("m",1,dateserial(year(Today),month(Today),0))
First day of last month:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
First day of next month:
=dateadd("m",1,dateserial(year(Today),month(Today),1))
Last day of last month:
=dateadd("m",0,dateserial(year(Today),month(Today),0))
Last day of next month:
=dateadd("m",2,dateserial(year(Today),month(Today),0))
For current month
=DateSerial(Year(Parameters!ParameterName.Value), Month(Parameters!ParameterName.Value), "1").AddMonths(1).AddDays(-1)
Previous month last day:
=DateSerial(Year(Now()), Month(Now()), "1").AddDays(-1)
Previous month first day:
=DateSerial(Year(Parameters!ParameterName.Value), Month(Parameters!ParameterName.Value), "1").AddMonths(-1)
Previous month last date:
=DateAdd("d",-(Day(today)), Today)
First of previous month:
=DateAdd("m",-1,DateAdd("d",1-(Day(Today)), Today))
=DateSerial(Year(Now), Month(Now), 1) for first day of the month
and
=DateSerial(Year(Now), Month(Now)+1, 0) for the last day of the month.
http://www.answermysearches.com/ssrs-how-to-set-a-default-parameter-to-the-first-day-of-the-month/2167/
Using Native VB Functions
First Day of Previous Month
=DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1))
Last Day of Previous Month
=DateAdd("d", -1, DateSerial(Year(Today()), Month(Today()), 1))
I was able to accomplish this, and show it in mm/dd format by using:
=Format(DateAdd("D", -1, (DateAdd("M", 1, yourdate.value))), "MM/dd")
=DateSerial(Year(Now), Month(Now), 1)