Reporting services expression for default value - reporting-services

the following error is displayed when deploy reports,
"The Value expression for the report parameter ‘Year’ contains an error: [BC30201] Expression expected "
The problem is in default value expression for parameter Year.
=Switch(
CInt(System.DateTime.Now.Month) >3,
Year(Today()),
CInt(System.DateTime.Now.Month) <=3,
{Year(Today.AddYears(-1)),Year(Today())}
)
Any idea?

I'm pretty sure you can just rewrite the above as:
=IIf(Month(Today) > 3, Year(Today), Year(DateAdd(DateInterval.Year, -1, Today)))
i.e. If Today is in April to December, use the current year, else use the previous year.

Related

SSRS Time expression filter not working with Parameter boolean value

Background:
Trying to achieve a filter in dataset, if the user selects Day shift from the parameter, any data entry between 06:00:00 and 18:00:00 is filter in the report. Furthermore, any data entry between 18:00:00 and 06:00:00 is reflect in the report for Night shift
Process:
I have got a parameter with 2 Boolean values "True" and "False". I have labeled those values as "Day shift" and "Night shift" in the available value Parameter window option. Default value is true.
Converting datetime to time, in the dataset query
SELECT
CONVERT(time, SWITCHOFFSET(CONVERT(datetimeoffset, LastModified),DATENAME(TzOffset, SYSDATETIMEOFFSET()))) as ShiftTime
FROM table abc
Filter expression:=Fields!ShiftTime.Value
Operator: in
Value: =IIf( ( TimeValue(Fields!ShiftTime.Value) >= "06:00:00" And TimeValue(Fields!ShiftTime.Value) <= "18:00:00" ) , Parameters!ShiftType.Value(0), Parameters!ShiftType.Value(1) )
Problem: [BC30516] Overload resolution failed because no accessible
'IIf' accepts this number of arguments
Not sure which part I am going wrong with, I am thinking it is the datatype but unsure.
Solution which I built on
I'm not sure what report designer you are using, but using the Report Designer extension for Visual Studio gives me very different errors.
And it gives errors both in the output window, and the expression window when building it.
Firstly to debug this, add it as a column to your report before you try and implement it as a filter.
Then check the relevant documentation for the function. It turns out the TimeValue function takes a string, not a DateTime, and returns a datetime.
Then, you can't access the available parameter values from the parameter, you can only access the selected parameter values. So instead of Parameters!ShiftType.Value(0) (which has a red line under it) you should just have true (or false depending on which way around your logic is) i.e. the value of the parameter.
Finally, I don't know what implicit conversion SSRS does from strings to a time, so I would do an explicit time compare instead of a string compare. Given we know that TimeValue returns a DateTime with the minimum date value and the time component we can compute the required limites for day/night shift.
The following is what worked for me:
=IIf(TimeValue(Fields!ShiftTime.Value.ToString()) >= DateTime.MinValue.AddHours(6) And TimeValue(Fields!ShiftTime.Value.ToString()) <= DateTime.MinValue.AddHours(18), true, false)
And actually re-visiting it, thats way too convoluted, you are already returning a time value which we can compare directly as
=IIf(Fields!ShiftTime.Value >= new TimeSpan(6,0,0) And Fields!ShiftTime.Value <= new TimeSpan(18,0,0), true, false)
And then your filter should be:
Filter expression: {as shown above}
Operator: =
Value: =Parameters!ShiftType.Value
Note: when you say "between" I don't know whether the intention is to include both 6am and 6pm within the day shift window, but I haven't modified your logic in that regard.

SSRS IIF Expression- IFF not Declared?

I'm trying to write an IFF statement in SSRS report inside Visual Studio.
For this scenario, In my data I have the following two fields, MaxCompareDateLapse and MaxCompareDateLapse2. Both of these return integer values. I also have a parameter which has the following values available for the use to select: 3, 6, 9, 12
I'm entering an IFF statement in an expression and I'm getting back the error 'The Value Expression for the textrun 'MaxCompareDateLapse.Paragraphs[0]' contains an error: [BC30451] 'IFF' is not declared. It may be inaccessible due to it's protection level'
I've tried the following two expressions and they both give me back the same error. The first one below is ultimately what I want to get it, and the second one I simplified a bit to see if it would work but I still get the error.
Code #1
=IFF(Fields!MaxCompareDateLapse.Value <
Parameters!pMonthsNoActivity.Value, Fields!MaxCompareDateLapse2.Value,
IFF(Fields!MaxCompareDateLapse.Value >=
Parameters!pMonthsNoActivity.Value, Fields!MaxCompareDateLapse.Value))
Code #2
=IFF(Fields!MaxCompareDateLapse.Value <
Parameters!pMonthsNoActivity.Value,
Fields!MaxCompareDateLapse2.Value,
Fields!MaxCompareDateLapse.Value)
So for example 1. If MaxCompareDateLapse =1, Parameter = 6, and MaxCompareDateLapse = 20, then 20 would be returned. If MaxCompareDateLapse = 7, Parameter = 6, and MaxCompareDateLapse = 5, then 7 would be returned
Thanks in advance - still pretty new to this stuff
You should replace the IFF with IIF as that is the correct syntax.
Refer to foll. example:
=IIF( Expression to evaluate,
what-to-do when the expression is true,
what-to-do when the expression is false )
You can nest using the 2nd and/or 3rd arguments based on your requirements. Example if you want to nest another IIF check if the above expression is false:
=IIF( 1st Expression to evaluate,
what-to-do when the 1st expression is true,
IIF( Another Expression (2nd) to evaluate,what-to-do when that 2nd expression is true,what-to-do when that 2nd expression is false )
)
Hope this helps.

SSRS dataset filter

Hi I'm trying to build a report in ssrs which gives the output based on current weekending date where end day is saturday. I'm pulling my data from SSAS Cube and I have a weekending date field so I applied a filter in the query designer to get the current weekending date using a parameter I used this expression:
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today)
When I'm executing my query, it throws an error saying the restriction imposed by the constrained flag in the STRTOSET function were violated.
Please let me know how can I fix this.
The error is thrown because you are passing a string that doesn't correspond with a valid member in your cube.
Note the Query Designer builds a MDX query based on the dimensions and members you select, this query uses the STRTOSET() function to convert your string parameter in a valid member.
...
STRTOSET(#CurrentWeekendDate,CONSTRAINED)
...
If you pass the current weekend date to your parameter it produces:
STRTOSET('2016-01-10',CONSTRAINED)
As 2016-01-10 is not a valid member in your Date dimension it throws the error.
You have to pass something like this depending on your cube:
[Date].[Date Key].&[2005-01-01T00:00:00]
So in SSRS you have to set your parameter to Text and use this expression:
CORRECT EXPRESSION:
="[200 Date].[Week Ending Date].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-ddThh:mm:ss"
)
& "]"
UPDATE: If [Week Ending date] level doesn't include time:
="[Date].[Date Key].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-dd"
)
& "]"
Go through this tutorial if you get stuck.
Let me know if this helps.

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