SSRS report (rdlc) value expression and null checking - reporting-services

Just wondering if the following expressions used for Value are equivalent (the column "Contrib" in DataSet "MyDataSet" is nullable) :
option 1 :
=Format(First(Fields!Contrib.Value, "MyDataSet"), "C2")
option 2 :
=IIF(First(Fields!Contrib.Value, "MyDataSet") Is Nothing, "", Format(First(Fields!Contrib.Value, "MyDataSet"), "C2"))
i.e. does SSRS have special processing, so that in option 1, it internally checks for a null value, and effectively ends up doing something similar to option 2. From trial and error, they seem to give the same results (when "Contrib" is null/not null), but just wanted to be sure.

In my experence Reporting Services will by default output a blank value if the value is Null.
As you are using IIF to modify results I think your issue may be similar to a problem that I have with preventing divide by zero errors on calculated columns.
The IIF will evaluate all of it's operands so an error will occur even if the divide by zero occurs in "false" part. I capture a divide by zero by showing "-" in the report and I use a nested IIF to replace the divisor with a value of 1 but this result is never shown in the report.
=IIf(Fields!LastYearMonthToDateSales.Value = 0, "-", (Fields!ThisYearMonthToDateSales.Value - Fields!LastYearMonthToDateSales.Value) / Abs(IIf(Fields!LastYearMonthToDateSales.Value = 0, 1, Fields!LastYearMonthToDateSales.Value)))

Related

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 #Error in combination with IIF and Cdbl

hope someone more experienced can give me answer/solution to my problem.
In a database I have two columns, one to control do we show data and the data to be shown is in the second column.
I have data and expression as below:
ShowPriceOnMatrix | RSP
-------------------------------
0 | 1.48
1 | 10.26 euro -> This one fails with #Error
1 | 4.59
0 | 7.12
=IIF(Fields!ShowPriceOnMatrix.Value = 1, CDbl(Fields!RSP.Value), "")
This expression sometimes gives me #Error.
The problem is that the RSP field was defined as nvarchar and I cant change it now so I'm trying to convert to number and show as. Some users when input price on the filed they also type a text ex. "12.45 euro" or similar. When the expression hits a value with text the whole IIF fails with an #Error even if the first column is stated not to show the price.
The problem with IIF is that it is not skipping the second parameter if the first one evaluates to false - and therefore produces #Error in my case.
I've tried to use
IsNumeric(Cdbl(Fields!RSP.Value))
and
IsError(Cdbl(Fields!RSP.Value))
but both of them give an #Error also. From SSRS explanation for those functions, they should return a Boolean value but somehow I'm getting #Error :)
Just convert the field to numeric using
VAL(Fields!RSP.Value)
Also, you might want to consider using SWITCH instead of IIF. SWITCH stops at the first expression which evaluates to True, whereas IIF will evaluate all expressions regardless of if they will be used or not.
In your case, with a single IIF, SWITCH is not relevant but in cases where nested IIFs are used or situations where you may get a divide by zero error, SWITCH is often easier to read/debug. So something like this where we have a hypothetical situation that we want to test for divide by zero and return zero or if the thing we are dividing by is negative we want to return -1, otherwise do the calc...
=IIF(Fields!MyDivisor.Value = 0, 0, IIF(Fields!MyDivisor.Value <0, -1, Fields!MyNumber.Value/ Fields!MyDivisor.Value))
would become
=SWITCH (
Fields!MyDivisor.Value =0, 0,
Fields!MyDivisor.Value <0, -1,
True, Fields!MyNumber.Value/ Fields!MyDivisor.Value
)
each expression/result pair is evaluated in order until it hits the first True expression, so if MyDivisor was zero it would return 0 and stop, if it was negative, it would return -1 and stop. The final True acts like an else (as True always returns True ! ) so it would only do the calculation if all other expressions returned false.
You can use this expression to extract your numeric values:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9,]", "")
But you have to take care of the . in 1.48. With , and the language DE it worked for me. With . it would be:
=System.Text.RegularExpressions.Regex.Replace(Fields!RSP.Value, "[^0-9.]", "")

Error when trying to use Where clause in SSRS expression

This SO link, WHERE Caluse in SSRS expression, verifies that my syntax is correct yet I get this error:
"An error occurred during local report processing.
The definition of the report is invalid.
The Value expression for the text box 'txt1A_EMA' uses an aggregate expression without a scope. A scope is required for all aggregates used outside of a data region unless the report contains exactly one dataset."
My expression:
=Sum(IIF((Fields!COU_EMA.Value, "CA_Summary") = 1, (Fields!Amount.Value, "CA_Summary"), 0))
There are 3 Datasets in my report. The one I want is "CA_Summary" and the expression above was built with the expression builder.
The field, (Fields!COU_EMA.Value, "CA_Summary"), is a boolean (1 or 0) and I get error either with a 1 or a "true" (no quotes).
I probably could fix it if I knew what the definition of "scope" is. . . maybe.
The scope is where you want to look in order to get the values you're looking for. It could be a data set, group, etc. In your expression it appears CA_Summary is the scope.
I would try modifying the expression a bit. Since you say COU_EMA is bit field, maybe use TRUE. This is also assuming the matrix is using the data set CA_Summary.
=Sum(IIF(Fields!COU_EMA.Value = TRUE, Fields!Amount.Value, 0))
If your matrix isn't using that data set and it still doesn't work try
=Sum(IIF(Fields!COU_EMA.Value = TRUE, Fields!Amount.Value, 0),"CA_Summary")

Dealing with blank or null in ssrs

In my report, I have an expression which is used to pass a parameter to a child report parameter which is set to allow null values. The expression is :
=IIf(Parameters!Lead.Value = "False",Nothing,Fields!Paid.Value)
The above expression returns values only when Fields!Paid.Value is not blank. Therefore when Fields!Paid.Value is blank I get an error
"the value provided for the report parameter is not valid"
How do I modify my expression to parse these two conflicting issues?
What I want is to be able to return values when the Fields!Paid.Value is blank or when it is not. So at all time when the expression runs corresponding values are returned without the error stated above.
Thanks for helping out.
The first thing you do, wherever you have used the "Paid" parameter, set it to allow null value. Allow null only not blank.
The second thing about the expression, use something like this,
=IIF(Parameters!Lead.Value "FALSE", Nothing, IIF(IsNothing(Fields!Paid.Value),0,Fields!Paid.Value)

SSRS 2008 Conditional data driven sum

The matrix's odd rows contain strings, representing integer numbers. The even rows contain strings, representing dates in mm/dd/yyyy format. The order is not guaranteed, it could be the other way around if someone changed the category names.
The grand total row has to be added to this matrix, but the following expressions throw an #Error:
=Sum(Iif(IsDate(Fields!Data.Value), 0, CInt(Fields!Data.Value)))
=Sum(Iif(InStr(Fields!Data.Value, "/"), 0, CInt(Fields!Data.Value)))
Converting 0 and field value to some other numeric data types did not work too.
Interesting enough, the expressions partially work for the grand total column, i.e. they calculate the numbers' row total, but #Error on the dates rows.
Protecting the row grand total as follows did not help either:
=Iif(Fields!Results.Value = "# of items", CStr(Sum(CInt(Fields!Data.Value))), "")
What is the correct way to implement data driven conditional totals?
I could do this in plain SQL and dump into a tablix in a heartbeat but this has to be wrapped in SSRS and used with an existing matrix which must not be changed otherwise.
The #ERROR you get on the date rows is due to the CInt conversion failing.
This is because IIF is a function, not a language construct so both the true and false parameters get evaluated before being passed to the function regardless of the value of the boolean condition parameter. This means that:
=Sum(Iif(IsDate(Fields!Data.Value), 0, CInt(Fields!Data.Value)))
will always attempt the conversion to integer regardless of the result of the IsDate function.
Try using Val instead of CInt. The problem with CInt is it errors when the string to be converted is an inappropriate form; Val doesn't have that problem - it simply grabs whatever numbers it can. So you can use the expression:
=Sum(Iif(IsDate(Fields!Data.Value), 0, Val(Fields!Data.Value)))
and then simply format it like an integer.
Note that the Val function is still being run even when the field is not numeric but this expression succeeds because the Val function doesn't raise errors like Cint. We simply make the calculation and discard the result when the field is a date.
This expression, combining the tips from both answers with additional protection, works:
=Iif(
IsNumeric(Fields!Data.Value),
Sum(Val(Iif(InStr(Fields!Data.Value, "/"), "", Fields!Data.Value))),
0
)