Multiple IF statement in SSRS - reporting-services

How to write multiple if statement in SSRS. I have written like this.
=IIf(Parameters!MyDuration.Value="ThisMonth" & Parameters!Transactions.Value="Sale", Sum(Fields!ThisMonthSal), True,False)
this is throwing an exception that multiple parameter used. Please guide me how to write this multiple if statement.

The & operator concatenates two strings. You're looking for the And operator. Refer to this msdn article.
In addition, note that IIF has three parts (see decision functions here), and works like this:
=Iif(test, truepart, falsepart)
I'm not sure what you're trying to do with the "SUM" bit (or what you're trying to achieve at all, the question's not clear on that), but something like this would work:
=IIf(Parameters!MyDuration.Value="ThisMonth" And Parameters!Transactions.Value="Sale",
Iif(Sum(Fields!ThisMonthSal.Value) = 0, "Zero sum", "Non-zero sum"),
"Not thismonth or sale")
Finally, you're not entirely clear about the error you're getting. If you're also having a problem with accessing the parameter you may have to investigate that seperately. In any case, the above should explain the Iif bit.

Related

How to get around '#Error' when a function in MS Report Builder is evaluating a null value?

I hope my title is clear enough. I'm working in MS Report Builder, using a function that applies a regular expression to a queried value in order to get back a certain substring. The regex works fine, so I'll demonstrate a simpler version here to make this less wordy. Here's the gist of my equation:
=IIF(Len(Fields!CourtLocation.Value) < 1, "none",System.Text.RegularExpressions.Regex.Match(Fields!CourtLocation.Value, "(?:[A-Z]{2,4})").Value))
The main purpose is to get that substring, but I added the IIF so that on those occasions when the CourtLocation.Value is empty (I tried Is Nothing in my expression as well), the function returns "none" rather than "#Error"
I've been looking around for a solution, but nothing has worked; it seems like most other people who talk about this are using a mathematical equation rather than trying to get a string. Can anyone help me get rid of that stupid "#Error" value?
You could try this (untested)
=System.Text.RegularExpressions.Regex.Match(
IIF(
Len(Fields!CourtLocation.Value) < 1,
"none",
Fields!CourtLocation.Value
)
, "(?:[A-Z]{2,4})"
).Value
This way the IIF is performed on the string that you want to pass to the regex function, so it always gets a valid value to process
Iif evaluates both sides, so you can nest two Iif statements to avoid the error.
Did you already read this one?
https://sqldusty.com/2011/08/01/ssrs-expression-iif-statement-divide-by-zero-error/
I'll copy the text into the answer if that solves it for you.

what countrows().equals() means in SSRS

I can't understand the structure of the written code
(CountRows("client_code").Equals(Parameters!client_code.Count)
can anyone explain this please?
And where can I find a reference for all the functions that I can use to write Expression in SSRS??
In SSRS we use "=" operator for comparison rather than equals.
Expression mentioned by you below
(CountRows("client_code").Equals(Parameters!client_code.Count)
Rather it should be like
=IIF(CountRows("client_code")=Parameters!client_code.Count,true,false)
What your mentioned code does: It Returns a count of rows within the specified scope, Scope can be Group as well. and it compared with your Report Parameter count.
Here you will find complete list of functions and expression for SSRS.

Change Case in SSRS 2013

I am using a parameter to change the background color of my field when the field string contains the parameter string .
I have used IndexOf, Contains, and instr. All three work, however they are all case sensitive. (i.e. when I search 'Dol' Dollar Tree and Doldrum are highlighted but not Sandolski etc.)
It is not the stored procedure, the correct records display, however the SSRS functions are what is my challenge.
I have tried toLowerInvariant but was receiving an error Help please.
As I was writing this I checked my error and learned the issue..
I assumed the syntax was to pass my string as a parameter:
toLowerInvariant(Parameters!Param1.Value)
However the correct toLowerInvariant syntax is (in ssrs):
Parameters!Param1.Value.toLowerInvariant()
An explaination on toLowerInvariant can be found here: string.ToLower() and string.ToLowerInvariant()
And also I have found that this comparison is best done with a Switch (if you are comparing multiple parameters to the field).
I have not noticed a performance impact between Field.IndexOf(#Param), Field.Contains(#Param), or Field.Instr(#Param)
My final code:
=switch(
instr(Fields!Client.Value.toLowerInvariant(),Parameters!Client_Firm.Value.toLowerInvariant()) >= 1, "Cornsilk",
instr(Fields!Client.Value.toLowerInvariant(),Parameters!KeyWord.Value.toLowerInvariant()) >= 1, "Cornsilk"
)

How can I test and handle a #Func! error from within a query?

I have an Access query that has a some what complex field on it. Basically, I'm searching for a certain value, based on other derived criteria. The function either works, or it gives a #Func! error. There are a few reasons why I may get an error. That is fine, because in those cases I want to return Null.
How can I test and handle a #Func! error from within a query? Also, I tried to wrap the expression in an IsError() method, and handle that case. That still didn't work.
If, for example, you are using Instr with a field that may or may not be null, and that is causing the problem, you can either select only those fields that are not null, or concatenate an empty string with the field.
SELECT Mid(AField & "",Instr(Afield & "","x")) FROM Table
I don't think this is the optimal solution, however, my hope is that the "higher powers that be" within Microsoft that manage Microsoft Access understand exception handling and would provide some way of testing and handling/ignoring errors, where appropriate.
Regardless, in my case I found a hack to the problem. I save the query just as it was and I exported it to a table. Since this was a one-off exercise, I was able to extract the necessary data and then filter out all Null values from the newly created table.
It worked, but it feels like a really poor hack.
OMGGGGG I know im 6 years late but This is the first time I'm posting on a blog because I actually solved the issue!! You have no idea how excited I am. OK so it turns out this is exactly what I was trying to do: Mid(AField,Instr(Afield,"x")). In my case I knew the InStr function did not always find x in Afield, and in those cases ideally I wanted a Null. But What it actually returns is a 0. In access, functions like Right, Left and Mid CANNOT "START" AT 0, They must start at 1 (or -1 I suppose). Which is what is causing the #Func! error!
One solution....if your Instr fuction=0 do one thing other wise do something else.
If I think of another solution, I let you know.

Adding optional filter in SSRS 2008

I am trying to add an optional filter on a text field in a report. I have tried setting it up in the dataset but it treats it as a required filter so if it is not populated, no results are returned. It needs to use the Like operator. Any advice?
As I was typing out a work-around to this problem, I realized an incredibly easy solution (now that I understand better how it works).
Here's what I did:
Since Hong pointed out that all filter conditions must be met, I reversed my thinking. I moved my existing "IN" filters to the query and fed the parameter directly to the query. Then I created by "LIKE" text filter on the report which a default value of "*" so it would immediately return everything.
Here's what I could've done:
Just the last part. Added the "LIKE" filter with a default value of "*" so it immediately returned everything.
I also agree that most of the time it's best to send the params back to SQL. Since that's not what the OP is asking, here is the best option I have found for doing this. And it is actually quite simple.
Add your parameter with the appropriate data type. Let's use the
example of a "City" in this case (a text/string field).
Check "Allow Nulls" on the parameter.
Add a filter to either a tablix, table or dataset.
In the expression, select the field you want to filter on. Select the appropriate operator, in my example of a data set with Cities, in the Value put in this:
=IIF((Parameters!City.Value Is Nothing), Fields!City.Value, Parameters!City.Value)
I don't think you can make an optional filter in DataSet Properties/Filters, adding filters there means returning results that match ALL filter contiditions, so it is "AND" logical relation among all filters, not "OR".
My sugguestion is to use filter in query designer of the dataset, where you can define "OR" relations to filter out data. For instance: Your_Text_Field="SomeValue" OR Your_Text_Field is Empty.
Although I agree that most of the time it is best to send the parameters back to the stored procedure or data layer to reduce the amount of data returned, I have a case where it is just as easy to do the parameter handling in the RDL file via a filter. Due to this unique situation I found this solution which gives you a way to create an Optional filter in the RDL file.
http://www.andrewshough.com/development/sqlserver/ssrs/optional-filter-in-ssrs/
It is a great blog post with easy step by step instructions on how to create an optional filter.
Please Note: This is NOT my blog but I though this solution was great for what I needed and I hope it helps someone else when they google for "optional filter in SSRS" like I did.
I found a post which solved my problem setting the filter for a report-consumer to a) all multivalue fields being selected so the user b) could specify his/her selection if necessary.
Kasim 8 Dec 2010 8:55 AM #
In reports when we want to default the multivalue parameter to 'Select All' following are the steps.
Open the Report parameter window from the Report menu.
Select the Report parameter from the left handside of the window.
Select 'Multi-value' checkbox and appropriate 'Available values'.
Under default values select 'From Query' radio button'.
Select the appropriate 'Dataset'.
Select appropriate 'Value Field'.
Save the Report and select Preview Tab. You will find all the items selected in the multivalue >parameter list and the result displayed for all the selected items.
found on: http://blogs.msdn.com/b/bimusings/archive/2007/05/07/how-do-you-set-select-all-as-the-default-for-multi-value-parameters-in-reporting-services.aspx
(The Post came up in the comments quite in the middle.)
You can accomplish this by using an expression on the dataset filter.
Check this