Comparing todays date against first day of the week - reporting-services

I am trying to set a default parameter value in SSRS report. I want to test the current date to see if it equals the first day of the week (in my case Monday). If it is the first day of week, then I want the default value to be current date minus 2 days, if it is not the first day of the week then I want the default value to be current date minus 1 day.
I seem to have a syntax problem but it doesn't tell me where. My parameters are StartDate and EndDate.
this is what I've tried:
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today(),DateAdd("d",-1,today())
this is the generic error I get:
The value expression for the report parameter 'StartDate' contains eror:[BC30201] Expression expected.
Where am I going wrong?

You are trying to use Sql syntax in a SSRS VBA expression. SSRS VBA allows very similar expressions for date manipulation to Sql, the main difference being the use of the DateInterval enum.
So your expression needs to use VBA syntax:
=IIF(Weekday(Today, FirstDayOfWeek.Monday) = 1, DateAdd(DateInterval.Day, -2, Today), DateAdd(DateInterval.Day, , -1, Today))

It appears that you are missing a closing parentheses after the first logical part of the if statement and another to close the statement.
=iif(weekday(Today(),FirstDayOfWeek.Monday)==1,DateAdd("d",-2,today()),DateAdd("d",-1,today()))

Related

Using IIF and DATEPART in SSRS to highlight the first of the month in date column

I have a date column that shows the days on each month. I need to highlight the first date of the month for each month. Example 07/01/2021 would be highlighted, 08/01/2021 would be highlighted; all other dates in the month would not. This is the code I am trying:
=iif(DatePart(DateInterval.Day, Fields!Date.Value, 1, "Yellow","Transparent"))
but it is not working. Any help would be appreciated.
Your expression is incorrect. The Datepart should be compared to 1 and then IIF will use either the 2nd (true) and 3rd (false) argument.
=IIF(DatePart(DateInterval.Day, Fields!Date.Value) = 1, "Yellow", NOTHING)
Also, I think it's a old bug where "Transparent" is not a valid color even though that's what the GUI gives the user when clicking on NO COLOR. Either use "White" or NOTHING (the SSRS null value)

Twelve month ago as parameter in SSRS

I have two main parameters in a Report:
First is to be used to set the month (e.g. Dez 2017), from this Parameter the Report shall set its fromParameter itself (e.g. Dez 2016 in this case).
What is the easiest way to solve this issue? Is there a dateadd formula, calculating backwards from a Parameter?
My first Parameter is set by a formula like this(creating the last month):
('[Date].[Fiscal].[Month].&['+Format(DateAdd("d",-39,now()),'MMM yyyy')+ '] ')
Or do I have to calculate the "fromperiod" first in a query? How could this look like?
In the end the reporst should still be flexible, so changing its 12 month period based on would the user is selecting in the end.
Try this expression for the start date parameter default value to set it 1 year before the the parameter end date.
=DateAdd(DateInterval.Year, -1, Parameters!EndDate.Value)
Or you could do the reverse if you wanted to base it off the parameter start date.

Date calculations - how to use today's date if date not specified

I have a question regarding Microsoft Access 2010. I'm working on an inherited complaints database and I've been asked to produce a report outlining how long complaints have/ or were active.
Using the following I'm able to calculate the difference between when a complaint was opened and when it was closed and show the number of days active.
DaysActive: DateDiff("d",[COMPLAINTS]![DateRcvd],[COMPLAINTS]![DateClosed])
My issue is when a complaint hasn't been closed I don't get a value returned. Is there a way to modify the expression so that if the DateClosed is empty it will use the current date instead?
Assuming this is a query expression, the db engine supports the Date() function which returns today's date. So you can use an IIf expression to give you Date() when DateClosed is Null, or otherwise DateClosed
DateDiff("d", COMPLAINTS.DateRcvd, IIf(COMPLAINTS.DateClosed Is Null, Date(), COMPLAINTS.DateClosed))
If the query will always be run from within an Access session, you can use Nz instead of IIf ...
DateDiff("d", COMPLAINTS.DateRcvd, Nz(COMPLAINTS.DateClosed, Date()))
Note that Nz is a VBA function and IIf is supported directly by the db engine, so IIf should theoretically be faster . But the difference may not be perceptible in your context.
Try using this.
DaysActive: DateDiff("d",Nz(COMPLAINTS.DateRcvd, Date()),[COMPLAINTS]![DateClosed])
Nz will check if the date is available or not, if not , it will replace today's date for that, using DATE() function.

SSRS - weekday name

any advice appreciated
I have as a column heading the expression =WeekdayName(weekday(fields!date.value))
This returns the day of the week, however, it is returning a day of the week one day in advance, eg when I put Monday's dates in the parameter it shows as 'Tuesday' in the report.
My question is can the above expression be tweaked to show the WeekdayName one day before, eg =WeekdayName(weekday(fields!date.value -1)) ? I tried this but got an error.
Thanks.
So you want to subtract one day from the your incoming date then you can use the
= DateAdd("d", -1, yourdateField)
This way you can subtract the any number of days from your date.
But did you try to see why it is giving the day of previous date. Do check the system date time or else check with the
=WeekdayName(weekday(Today()))
and see if it gives you the correct day of week for current date.
Weekday and weekdayname functions have both another optional argument for defining the starting day of the week.
Problem is the 2 functions don' t default this argument to the same value so depending on your server settings you should explicitly set the second argument of these functions.
No need to invoke a date function. As the weekday() function returns and integer you can offset the result and use the Mod operator to keep it in bounds:
=WeekdayName((weekday(fields!date.value)+5) Mod 7)+1)
The parenthesis are important to ensure the addition comes first.
Just for reference:
OP asked again because of the weekday offset and this is the correct provided solution.
=WeekdayName(weekday(Today())) gives me tomorrow
=WeekdayName(Weekday(Today(),FirstDayOfWeek.System))

Report Builder - Set datetime parameter

I have a report that has parameters StartDate and EndDate. I would like the EndDate parameter's time part to default to the end of the day when selected from the drop down.
For instance, if a user selects 5/15/2008 from the dropdown, the value shown in the box should be '5/15/2008 23:59:59' not '5/15/2008 12:00:00'
Its pretty easy to do this in .Net using the event model and a line of code but in Report Builder 2.0, what would I need to do?
Is there code that I need to write for this or did I miss some funky expression that could handle this?
Thanks.
AboutDev
I would suggest setting the default parameter in the Report Parameters section. You can get to this from Report > Report Parameters.
This allows you to set a non-queried default. There you can enter an expression like
=DateAdd(Microsoft.VisualBasic.DateInterval.Second ,-1,dateadd("d",1,Today))
That should give you a default for the end of today.
Edit: Really only useful for a single default value.
It's been awhile since I've used SSRS, so bear with me. You'll have to do a little translation, but here's what I've done in the past.
When you define your EndDate parameter, create an additional parameter named EndDateEOD, after EndDate in your list of parameters. Make this parameter a hidden value, and set it to the last moment of the day, similar to the way that Jeremy calculates it.
Then you can use #EndDateEOD in your report query where you have #EndDate now.
When StartDate is selected, you could have EndDate default to its value, so that EndDateEOD will automatically be set to the end of the start date.
Use the parameter in a DATEADD() expression in your dataset.
Rather than
...WHERE end_date = #end_date
do something like this:
...WHERE end_date = DATEADD(ms, -3, #end_date + 1)
That will go forward a day (the +1), then go back 3 milliseconds, to the last instant of the day recordable by a datetime.
You can do something like this:
=CDate(Parameters!StartDate.Value + " 23:59:59")
The part of Parameters!StartDate.Value can be any date but not the EndDate.Value itself. Eg:
- Today()
- Last day of the month from start date:
=CDate(DateSerial(Year(Parameters!StartDate.Value), Month(Parameters!StartDate.Value) + 1, 0)+" 23:59:59")
Hope this help.