The added or subtracted value results in an un-representable datetime - reporting-services

I have the following expression to check parameter date values and replace from date if necessary:
=IIF(Parameters!FromDate.Value is nothing,
IIF(Parameters!ToDate.Value is nothing,
DateAdd("d",-30,Now()),
DateAdd("d",-30,Parameters!ToDate.Value)),
Parameters!FromDate.Value)
Only when the todate is null, an error will get appear:
The added or subtracted value results in an un-representable datetime.
Did anybody face such a problem?

The problem is that IIF is a function, not a language construct so it evaluates both parameters before passing them to the function. That means DateAdd("d", -30, Parameters!ToDate.Value) gets evaluated even when Parameters!ToDate.Value is Null, thus giving you this error.
Try this instead:
=IIF(Parameters!FromDate.Value is Nothing,
DateAdd(DateInterval.Day, -30, IIF(Parameters!ToDate.Value is nothing, Today, Parameters!ToDate.Value)),
Parameters!FromDate.Value)

Related

SSRS default param expression today() vs now()

I have a parameter of type date/time. I am creating a default value as expression. When I put
=IIF(Hour(TODAY()) < 1,
DateAdd("d", -1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
it works well and return a value (although always the same, since the hour is always 0 (12AM))
But I want to base this default value on the CURRENT date time attime of report. So when I replace today() with NOW(), it errors out and says that it "doesn't have expected type".
=IIf(Hour(now())<1,
DateAdd("d",-1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
Thanks
I tried multiple formulas to convert now() into something, but nothing worked.
It seems as though the expression didn;t evaluate the second part of the IIF because the HOUR(Today) was always 0. The Format would convert the date into a string which would cause the error. Evidently, the DateAdd was converting the string back into a date in order to subtract the day.
I think you can do without all the converting.
=IIF(Hour(NOW()) < 1,
DateAdd("d", -1, Today()),
Today())

SSRS Expression for DateAdd to return value and blank

Hi all, I have an expression in SSRS that calculates the Due date from the Last Fit Test. The data is correct if the Last Fit Test has a date. However, if the Last Fit Test is Null or blank, it gives an error "########" (it supposes to return empty or blank). How can I fix that expression?
=IIF(IsNothing(Fields!LastFitTest.Value) = True,"",DateAdd(DateInterval.Year,1,Fields!LastFitTest.Value))
Try this ...
=IIF(
IsNothing(Fields!LastFitTest.Value),
"",
DateAdd(DateInterval.Year,1,IIF(IsNothing(Fields!LastFitTest.Value), '1900-01-01', Fields!LastFitTest.Value))
)
The problem was the IIF always evaluates both the true and false condition so when you had no value in LastFitTest the false side could not evaluate even though it would never be used.
All we have done here is made the date that get processed in the DateDiff function always return a valid date. The 1st Jan 1900 means nothing, you can put any date you like in there as it will never be used in the final result.
The only other change was to remove the = True. IsNothing returns True/False so you don't have to explicitly say =True.

Error when trying to DATEADD milliseconds in SSRS

So I have an enddate parameter in my report and I have the following code under Default Value to get me the last day of the previous month.
=DateAdd("d", -1, DateSerial(Year(Now()), Month(Now()), 1))
This works fine, returning 04-30-2016. That's fine and dandy but when I go to use the Subscription piece in SSRS it has the datetime field as: 04-30-2016 12:00:00 AM. So I tried doing the following to my code to have it shave off three milliseconds:
=DateAdd("ms", -3,DateAdd("d", 0, DateSerial(Year(Now()), Month(Now()), 1)))
When I try to run my report I get the following error:
The DefaultValue expression for the report parameter "enddate" contains an error: Argument 'Interval' is not a valid value. (rsRuntimeErrorInExpression)
How do I fix this error and get my report running as it should?
The AddMilliseconds function will work for you in this case.
=DateAdd("d",-1,DateSerial(Now.Year,Now.Month, 1)).AddMilliseconds(-3)
This gives you the last day of the previous month substracting three milliseconds.
Let me know if this helps.

Comparing todays date against first day of the week

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()))

Expression giving error in SSRS

I am using below expression in SSRS but is not giving expected result.
=IIf(Fields!Completed_Date.Value Is Nothing,
Nothing,
IIf(First(Fields!TIMEFORMAT.Value,"dsPreferences") = True,
Format(DateAdd("n",Parameters!UTCOffset.Value,Fields!Completed_Date.Value),"MM/dd/yyyy hh:mm:ss tt"),
Format(DateAdd("n",Parameters!UTCOffset.Value,Fields!Completed_Date.Value),"MM/dd/yyyy HH:mm:ss")
)
)
It has to check if Date is null.If it is null just leave field blank.If it is not null then it has to return value according to Time zone value that it is getting from another dataset.This expression works fine when there is non null date but it returns #error when there is Null date.
Try using this syntax:
=IIf(IsNothing(Fields!Completed_Date.Value), "", <yourFalsePart>)
This syntax probably doesn't solve your issue because Completed_Date is also used in the false part. IIf operator will always evaluate both expressions before deciding which one to use: so if Completed_Date is Nothing it will broke your expression anyway.
Try using a Custom Function as explained here: SSRS expression giving error with iif condition