SSRS IIF Expression- IFF not Declared? - reporting-services

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.

Related

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

Report Builder SUM IIF - More than 1 DataSet

I am trying to add the following expression to a TextBox on a Report Builder report using the following code:
=SUM(IIF(Fields!TaskDescription.Value,"DataSet1") = "Running", 1, 0)
I have more than 1 dataset which I think is causing the issue, but the above gives me the following error message:
The scope parameter must be set to a string constant that is equal to
either the name of a containing group, the name of a containing data
region, or the name of a dataset.
What am I doing wrong?
The Scope, in your case the DataSet, need to be the last parameter in the aggregate, i.e. after the IIf:
=SUM(IIF(Fields!TaskDescription.Value = "Running", 1, 0), "DataSet1")
i.e. =Sum(<Expression>, <Scope>)
The expression counts the occurrences of the value Running in the TaskDescription column in the DataSet1 DataSet.
Edit after comment
A quick test showing the expression in action. A simple DataSet with your column:
I've just added a textbox to a blank report with the above expression:
Works as expected on the sample data:

IIF Statement Not Returning a Value in SSRS

I'm very confused by this situation, i've written quite a few IIF statements and always get them to work. I have two columns in my dataset called CATEGORY and PCT, what i'm trying to do is return the PCT value for only one specific value in CATEGORY.
For example, I have the following table of data
Category PCT
A .50
B .75
I have placed a textbox on my report and have written the following expression to return the PCT value if Category = B, like so:
=IIF(Fields!Category.Value = "B", Fields!PCT.Value, " ")
For some reason this expression returns empty every single time. When I just put Fields!Category.Value in the textbox as a test then the value A returns which is as expected.
I'm really thrown-off by this and I know i'm missing something very simple - can someone help?
Its important that we understand the context of the textbox as the expression seems valid.
If you placed a single textbox on your report and used your above expression (with references to the datasets) ONLY the first row of your dataset will be evaluated. In which case the expression will always evaluate to the same result.
Can you confirm that the control you are using is indeed a textbox? If it is then i believe you do need a reference to the dataset and the expression will look more like this:
=iif(First(Fields!Category.Value, "datasetName") = "B", First(Fields!PCT.Value, "datasetName"), " ")
This would only evaluate the first row in your dataset.
If you were to do this in a tablix using your original expression then it should work.

SSRS report (rdlc) value expression and null checking

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