How to display Last week Sunday and last week Saturday in SSRS? - reporting-services

How to display Last week Sunday and last week Saturday from current date in SSRS ?
In sql server we can find the last week Sunday and last week Saturday from todays date(CurrentDate='19-02-2020') as
select DATEADD(wk, -1, DATEADD(wk, DATEDIFF(wk, 0,getdate()), -1))-- for sunday
select DATEADD(wk, DATEDIFF(wk, 0, getdate()), -2) --for saturday
So how we can write an default expression in the SSRS Parameters?

This assumes that the first day of the week is a Sunday, if you configuration is not like this then you will need to adjust the expressions to correct the offset
For Last Saturday use:
=DATEADD(DateInterval.Day, Weekday(Today()) * -1, Today())
For Last Sunday use:
=DATEADD(DateInterval.Day, (Weekday(Today()) -1) * -1, Today())

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)

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.

all records from certain day last week [duplicate]

This question already has an answer here:
MYSQL - find nearest previous day
(1 answer)
Closed 2 years ago.
Is there a way to display all records from let's say last Thursday until today in mysql?
I found all records from last week, all records in certain interval of days but nothing for a fixed day last week and all this week...
Our records always begin on Thursday that's why i'm asking
Edit:
This week (no matter if it is Monday to Friday) i want to see all records from last Thursday until NOW(). Next week the whole process repeats but must display records from this Thursday until the day (Monday to Friday) next week... and so on.
select * from table
where column = DATE_SUB(CURDATE(),INTERVAL (WEEKDAY(CURDATE()) +4) DAY);
select * from your_table where your_date_field >= DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -4)
the -4 in the DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -4) coresponds to last thursday
EDIT use this one:
select * from your_table where DATEADD(dd, DATEDIFF(dd, 0, your_date_field), 0) >= DATEADD(dd, DATEDIFF(dd, 0, '6/14/2018'), 0)
This one will work for you:
select * from your_table where DATEADD(dd, DATEDIFF(dd, 0, your_date_field), 0) >= DATEADD(dd, DATEDIFF(dd, 0, DATEADD(day,-7, DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 3))), 0)
this basically gets your_date_field >= thursday of last week. Will continue to be the last thursday as days and weeks pass.

SSRS Last Week Monday and SUnday

I need the last week Monday and Sunday , say today is March 18th hence the SSRS expression
should return
March 10th -Monday
March 16th -Sunday
I was able to get it using SQL
select dateadd(d,(2-datepart(dw, getdate())), dateadd(ww,-1,getdate()))
select dateadd(d,(1-datepart(dw, getdate())), getdate())
not sure how to get it done via SSRS expression
SSRS VBA allows very similar expressions for date manipulation to Sql, the main difference being the use of the DateInterval enum. Without checking your expressions, it will convert to SSRS VBA as follows:
Last Monday:
=DateAdd(DateInterval.Day, 2-WeekDay(Today), DateAdd(DateInterval.Day, -7, Today))
Last Sunday:
=DateAdd(DateInterval.Day, 1-WeekDay(Today), Today())
To get Sunday from Last Week :
(Note: Last Week Sunday is different from Last Sunday).
=DateAdd(DateInterval.Day, 1-WeekDay(Today), DateAdd(DateInterval.Day, -7, Today))
To get Saturday from Last Week :
=DateAdd(DateInterval.Day, 7-WeekDay(Today), DateAdd(DateInterval.Day, -7, Today))
with Weekdays values as below:
1 - Sunday
2 - Monday
3 - Tuesday
4 - Wednesday
5 - Thursday
6 - Friday
7 - Saturday

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.