SSRS date from previous year but with the same weekday name selected - reporting-services

I am trying to get last year's date but with the same week day as the one selected. Meaning that if the user selects Dec 5th 2013 which is a Thursday, the formula would select Dec 6th 2012 which is also a Thursday. Any help would be greatly appreciated.

Find/Replace "Today()" with the name of the field or parameter.
=Switch (
WeekDay(Today())-WeekDay(DateAdd("d",-365,Today())) = 0, DateAdd("d",-365,Today()),
WeekDay(Today()) > WeekDay(DateAdd("d",-365,Today())), DateAdd("d",(-365)+ABS(WeekDay(Today())-WeekDay(DateAdd("d",-365,Today()))),Today()),
WeekDay(Today()) < WeekDay(DateAdd("d",-365,Today())), DateAdd("d",(-365)-ABS(WeekDay(Today())-WeekDay(DateAdd("d",-365,Today()))),Today())
)

Related

Selecting records assuming future dates in mysql

I used the following select statement to select sum of amount assuming no future dates. How do I change this statement to include future dates?
SELECT SUM(amount) as allPreviousAmount
FROM `fn_table`
WHERE MONTH(transdate) < ? OR YEAR(transdate) < 2019 )
The ? is representing the month number. I have data in 2018, 2019 and 2020. Now I want to select all data before February 2019. How do I select it? The problem is that this also select data in 2020.
Regards your existing query, surely you'd need AND, not OR, to ensure no future dates? Say it's July 2020 and you have a date of January 2021 which is in the future. If you say MONTH('jan-2021') < MONTH('jul-2020') OR YEAR('jan-2021') < YEAR('jul-2020') then this will be true because jan is earlier than jul so a future date of January 2021 will be returned
Not really sure why you didn't just WHERE transdate < CURRENT_DATE() unless a any day this month is also classed as a future date... (by demanding the the month and the year have to be less than the current date, if it's 13th July 2020 you'll only get records up to end of June 2020 if you consider month and year only)
But to include all dates past and future simply remove the WHERE clause entirely
Edit in response to comment:
If you want a query that gives "all data up to but not including 01 Jul 2005" then do:
WHERE transdate < ?
And in your front end supply a date type parameter of 01-jul-2005. Any date can be supplied, but to cut off at a particular month end, supply a date that is the first day of some month
You can reuse the same query for future dates too by providing a date higher than any possible date in the table, such as 01-jan-3000

Getting the Week Number for the Month Crystal Reports Formula

I have a Crystal Report Grouped by (Day,Week,Month).
I want to be able to display the "Week Number" for the month. Ex: 1st Week Of July, 2nd week of July, 3rd Week of July, etc. on the "Week" Group Header.
I have tried using a Formula
Totext(DatePart("ww", {Command.TransactionDate}),0)
But the result is the "Week Number" for the year EX: 33,34,35. Any help would be very much appreciated
Use an expression like this:
datevar yourDate := currentdate;
Datepart("ww",yourDate)+1
- Datepart("ww",yourDate - Day(yourDate)+1)
of course, replace the variable assignment with your date.
The logic is to get the week number of the date (plus 1) and subtract the week number of the 1st of the current month.

How to set a default parameter date for the beginning of the month to yesterday date

I have build an SSRS report. I set a default date parameters as following:
startdate : take the first day of the month.
EndDate : take yesterday date.
This code for the beginning of the month
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(0)
This code for yesterdate date
=DateAdd(DateInterval.Day,-1,CDate(FormatDateTime(Now,DateFormat.ShortDate)))
The date works perfectly until, a new month comes, the result will be like this:
startdate: 01. 5 2019
end date: 30. 4 2019
where it should be :
startdate: 01. 4 2019
end date: 30. 4 2019
how can i make sure if a new date comes , it will take the first day of the previous month.
You should add an IIF statement to check if today is the first day of the month. The following expression should do what you need.
=IIF(DatePart(DateInterval.Day, Today()) = 1,
DateSerial(Year(Now()), Month(Now())-1, 1),
DateSerial(Year(Now()), Month(Now()), 1))
Just change your StartDate to reference yesterday's date:
=DateSerial(Year(Today.AddDays(-1)),Month(Today.AddDays(-1)),1)

SSRS Expression to Get last 12 months

I have created a ssrs report and this report display last 12 months data. I have 2 parameters startDate and Enddate.
The End date is always the current month and I want the start date to be the last 12 months. For example if my (End Date) current month is JAN 2018. I want my start date to be Feb 2017.
I have below expression but this give me Jan 2017 date for my start date.
=DateAdd("M", -12,Today())
Try this expression for the start date parameter default value.
=DateAdd(DateInterval.Year, -1, DateAdd(DateInterval.Month, 1, Parameters!EndDate.Value))
Here's what the parameter values would equal:

SSRS - Date parameter - Previous month specific date

I am Looking for a previous month specific date.
i.e. when the report is run on 25th of current month, the start date should always be previous month 25th.
Your expression for start date can simply be..
=DATEADD("m", -1, now())
If the run date is 25th October the start date will be 25th September.
If the run date is 31st March, the start date will be 28th of February, which is as close as you can reasonably get.
Found a solution for this.
Answer: =dateadd("m",-1,dateserial(year(Today),month(Today),25))
The above expression always looks for previous month 25th date, regardless when we run the report. for example if you always want to run on 11th of previous month then change the 25 to 11 on the above expression.
To get the same date of the previous month, just look for the previous month using DateAdd function like:
=DateAdd(DateInterval.Month,-1,Today())