How to write SSRS expression with OR condition involving a field and a parameter? - sql

I have a set of tablix results that im trying to filter based on OR condition but im having difficulty combining the two into one custom expression. [IsSafetyObservation] is just a boolean field and [Department] needs to be part of the multi-selected parameter values of [#Department]. Here's a screenshot of what it looks like as two separate filters but this is doing AND condition. When I tried to write custom expression I'm not using the write syntax because I get a red line when I try to reference the #Department parameter
tablix filter screenshot
custom expression attempt

You'll need to combine your expressions into one as you tried but instead of using IN, you should use InStr. InStr searches for a string inside another string and returns the position or 0 if not found.
First you would need to combine your parameters into a string using JOIN.
JOIN(Parameters!Department.Value, ",")
This will combine your parameters into a single string that you can now search for the Department field in the Department parameter.
InStr(JOIN(Parameters!Department.Value, ","), Fields!Department.Value) > 0
Also, SSRS doesn't always work well with Booleans (ugh) - so an expression of = (1 = 1) may work in some places for a Boolean result but not others so it's best to specify:
=IIF(InStr(JOIN(Parameters!Department.Value, ","), Fields!Department.Value) > 0
OR Fields!IsSafetyObservation.Value = False,
True,
False)

Related

How can I add a where statement on list conditionally in report query?

How can I add a where statement in iReport query with a condition that a list is not empty:
WHERE
CASE WHEN length($P{list}) > 0 THEN table_x.id IN ($P!{list}) END
I tried to display the report passing an empty list, but it does not work.
Jasper report has it's own query system and I presume your $P{list} is a class that extends java.util.Collection for example a java.util.List, since you state "passing an empty list"
To create a prepared statement IN query on Collection in jasper report you use:
WHERE $X{IN, table_x.id, list}
Do note that if list is null or it is an empty list this query will return all records, see using-parameters-queries
JasperReports handles special characters in each value. If the parameter is null or contains an empty list, meaning no value has been set for the parameter, the entire $X{} clause is evaluated as the always true statement “0 = 0”.
To not show any records, you need to add at least one null value to the List in java before you pass parameter to jasper-report.
if (list.isEmpty()){
list.add(null);
}
Please also note that you should avoid using $P!{param} since this creates
an sql through string concatenation an open your application to SQL injection attacks, always try to use prepared statement instead.
Your ELSE clause is NULL, and that is false. I would recommend avoiding the CASE:
WHERE length($P{list}) = 0 OR table_x.id IN ($P!{list})
If i understood correctly, try using this:
WHERE (CASE WHEN length($P{list}) > 0 THEN table_x.id IN ($P!{list}) END) <> ''

Expression Criteria for Access

I've created a database with about 15000 records and I want to create a form that filters the records according to entries inputed in textboxes through a Query. For the most of my columns i've used this Expression :
Like "*" & [Forms]![Testform]![Testtxt] & "*"
I use "Like" so that if the user decides not to input anything in a textbox, the query ignores that parameter. However when it comes to Dates (I have two columns that contain Dates) I can't make it ignore the empty textbox when the user decides not to write anything. Can you help me make the expression below show all the records in the query if the DateTesttxt is empty ?
> [Forms]![TestForm]![DateTesttxt]
If you're using query designer, just add another condition
Is Null
so visually in query builder it will be something like:
Criteria: > [Forms]![TestForm]![DateTesttxt]
or: Is Null
UPDATE:
This should work as well as a single expression:
Is Null Or > [Forms]![TestForm]![DateTesttxt]
and it is more correct solution in case if you have conditions on more than one column

Replace null with zero in crosstab query in Telerik

I just created 2 cross table using wizard function of Telerik Standalone report designer tool
since ISCED 5 has values for private and public its showing properly
using same query I created second cross tab and
but since ISCED 6 table doesnt have values for "public" section its showing like this
how to show as zero for public section 2nd cross tab (when no values for specific row)
You should modify the value of the field using an expression to evaluate your condition.
Select the textbox containing the data corresponding to the column you like to format when the value is null.
In the property pan on the right select "Value"
Write in there your expression, it should be something like this:
=Iif(Fields.MyField IS Null,"0",Fields.MyField)
You should also consider if the value instead of null is empty and eventually cover this case in the expression if applicable.
= Iif(Fields.MyField IS Null OR Fields.MyField = "", "0", Fields.MyField)
More information on conditional formatting can be found here.
Let us know if this works for you.

How to create list as a parameter in SSRS?

I have a report in 2005 SSRS which I want to add a parameter to. The parameter would be comprised of a group of zip codes, but be selected as a single item in the list.
For example, I would like to have 5 zip codes as one selection in the list and 3 for another, etc:
Select 11111,22222,33333,44444,55555,66666 AS Boondock
Select 77777,88888,99999 AS Timbuck
Select Zip Codes NOT IN (11111-99999) AS Everything Else
So my selections in the dropdown would be:
Boondock
Timbuck
Everything Else
Can anyone help me with how I should go about creating this parameter?
Create a simple string parameter to present to the user. Let's call it ZipCodeSet.
Create a dataset that examines the #ZipCodeSet parameter and returns the appropriate list of zip codes. Call it ZipCodeSelection.
Create an internal multivaue parameter that uses ZipCodeSelection as both its Available Values and Default Values. Call it SelectedZipCodes.
Use SelectedZipCodes in your report's datasets.
The easiest solution here would probably to use a Calculated Field on your dataset, called LocationDescription, for example:
=SWITCH(Fields!ZipCode >= 11111 and Fields!ZipCode <= 66666, "Boondock", Fields!ZipCode >= 77777 and Fields!ZipCode <= 99999, "Timbuck",True, "Everywhere Else")
The lone true statement at the end is due to the SWITCH expression reading left-to-right and exiting once it evaluates one of the switches as TRUE. This way for each of the items in your table of ZipCodes you will always end up with a TRUE result.
I assume you're evaluating a range of ZipCodes, and not exact values of 11111,22222, and so on? If so, the switch will have more values. A sample of your data would help if you want an exact answer.
Once you have built your Calculated Field, you can then set up a Parameter (called #LocationParameter) with available values based on a query of your LocationDescription field, then just filter your dataset using:
Expression:
= Fields!LocationDescription
Operator: =
Value:
#LocationParameter
(if you want multiple selections on your parameter, change the operator to IN)
Hope that helps.

How to exclude one value from a grouping sum, based on a value of another field?

How do I exclude one value from a grouping sum, based on a value of another field?
ie I open Report=> Report Properties=>Code and insert my Custom Code, but how would I change the below code to exclude a numeric value of another field for the below case?
Public Function ChangeWord(ByVal s As String) As String
Dim strBuilder As New System.Text.StringBuilder(s)
If s.Contains("Others") Then
strBuilder.Replace("Others", "Other NOT INCL")
Return strBuilder.ToString()
Else : Return s
End If
End Function
I'm assuming you want to exclude a numeric value from a sum where the string value of a cell on the same row includes "Others", and that the function you've supplied is used as the grouping criteria for a table in the report. Apologies if this isn't correct.
It's not going to be possible to do this without using a second piece of logic, either a function or an Iif condition. I don't have SSRS available to test this at the moment, but (assuming your value column is an integer, the code will look something like:
Public Function ExcludeOthers(rowDesc As String, rowVal as integer)
if ChangeWord(rowDesc) = "Other NOT INCL"
Return 0
else
Return rowVal
end if
End Function
Then, in the cell where you want the conditional sum to appear:
=Sum(ExcludeOthers(Fields!desc.Value,Fields!val.Value))
Alternatively, you could do this without the function by using Iif in the cell where the conditional sum will appear:
=Sum(Iif(ChangeWord(Fields!desc.Value) = "Other NOT INCL",0,Fields!desc.Value))
Depending on the nature of your source data, you could also do this by adding calculated columns to the report's source query.
I would favour the second or third option - custom code seems like overkill for this purpose.