I have a table which should hide the rows based on the below conditions -
Parameter value = Active, show rows with Active status
Parameter value = Matured, show rows with Matured status
Parameter value = Active and Matured, show all the records.
I have implemented the below for now -
=iif(parameter!activeormatured.value="Active",
TRIM(LOOKUP(Fields.ProductID.Value,Fields.ProductID.Value, Fields.ProductActiveflag.value, "Datasetname")) = "Active",
iif(parameter!activeormatured.value = "Matured",
TRIM(LOOKUP(Fields.ProductID.Value, Fields.ProductID.Value, Fields.ProductActiveflag.value, "Datasetname")) = "Matured",
False)
)
)
The above expression works only for the scenario Parameter value= Active and Matured.
Array.IndexOf can be used with multi-value paramters to check the selected parameters for a given value.
This should return a TRUE value when the Lookup matches a value in the Parameter.
=Array.IndexOf(Parameters!activeormatured.Value,
TRIM(LOOKUP(Fields.ProductID.Value, Fields.ProductID.Value, Fields.ProductActiveflag.value, "Datasetname"))
) > -1
If it's not a multi-value parameter but a single value with both values, you can use the InStr function which returns the location of a string in another string. If it doesn't find the string, it returns zero.
=InStr(Parameters!activeormatured.Value,
TRIM(LOOKUP(Fields.ProductID.Value, Fields.ProductID.Value, Fields.ProductActiveflag.value, "Datasetname"))
) > 0
Related
How can I sort the values returned from a LookUpSet function inside a Join function?
Example data:
TransNo MasterTran Item Category ModifierLevel
1001000 1001000 ItemA CategoryB 0
1002000 1001000 ItemB CategoryC 1
1003000 1001000 ItemC CategoryC 1
End result I'd like to get is "CategoryB ItemB ItemC". When I use the following combination of Join and LookUpSet, I end up getting "ItemB CategoryB ItemC".
=Join(LookUpSet(Fields!MasterTransNo.Value, Fields!MasterTransNo.Value, Iif(Fields!ModifierLevel.Value > 0, Trim(Fields!ItemDescription.Value), Trim(Fields!CategoryDescription.Value)), "LineItemDetails"), " ")
This is an expression on a cell in a table. The Row Group is set to Group on TransNo, sort by TransNo. I've tried a variety of different approach to sorting for the group, but always get the same result.
Any ideas on how I can force the order of data from LookUpSet so that it's joined in the order I want?
I ended up figuring this out by seeing other questions looking to pull distinct values only from the Join(LookUpSet()) functions and modifying it. This code is based off the useful answers from this other SO question.
Go to Report Properties
Enter the Code editor and past the following function into the Custom Code box:
Public Function JoinSortAlpha(arr As Object(), delimiter As String) As String
System.Array.Sort(arr)
Dim result As String = String.Empty
For i As Integer = 0 To arr.Length - 1
If Not arr(i) Is Nothing And arr(i) <> String.Empty Then
If result = String.Empty Then
result = arr(i)
Else
result = result + delimiter + arr(i)
End If
End If
Next
Return result End Function
Go to your expression and replace the Join() function with your new function by calling JoinSortAlpha(). My new expression looks like this:
=JoinSortAlpha(LookUpSet(Fields!MasterTransNo.Value, Fields!MasterTransNo.Value, Iif(Fields!ModifierLevel.Value > 0, Trim(Fields!ItemDescription.Value), Trim(Fields!CategoryDescription.Value)), "LineItemDetails"), " ")
Here's a breakdown of what the function is doing:
Create a new function called JoinSortAlpha which will have values passed into it from the expression. In this case, the values from the LookUpSet() function.
Sort the array passed from the function's argument. It's this sort that will make it alphabetical by default.
Create a String object called result to pass the final values to.
Evaluate the array arr and write a value to the result string for each value contained in the arr array. Values in an array are given a numeric value starting at 0 and increasing by 1. Here, we're telling the array to continue populating the result string from the first value in the array (at 0) until the last value in the array which is determined by the length of the array minus 1 (because the array starts at 0 rather than 1).
If your LookUpSet() function doesn't return any values, SSRS will show an error if we don't account for that in this JoinSortAlpha function. To deal with any potential blanks being returned, we're using an If statement to determine if the string is empty in which case it just returns nothing. Otherwise, it will return the value plus the delimiter from the end of the function (a space " " in my case).
I want to CONCAT value in ID column in string variable and use the variable with IN in SQL as under:
SET #ActID = CONCAT(CAST(5 AS CHAR),',',CAST(15 AS CHAR));
SELECT * FROM `accounts` WHERE `ID` IN (#ActID);
It returns record having ID = 5 and ignore record having ID = 15.
#ActID is a comma separated list string literal and not a list of values.
So the list inside the parentheses of the IN operator contains only 1 value: '5,15'
when you compare 5 to '5,15' the result is TRUE
when you compare 15 to '5,15' the result is FALSE
because '5,15' is converted to the integer value 5 according to the rules described here.
What you want is the function FIND_IN_SET():
SET #ActID = CONCAT(CAST(5 AS CHAR),',',CAST(15 AS CHAR));
SELECT * FROM `accounts` WHERE FIND_IN_SET(`ID`,#ActID) > 0;
Change to ...= 0 for the equivalent of NOT IN.
See a simplified demo.
Note: SET #ActID = CONCAT(5,',',15); works fine.
I have a client who wants to drop a list of values into a parameter in Reporting Services, compare it to a table in her database and return the full list of values with a yes or no in the next field indicating whether the value is found in the database table. For example if her list is Duck, Horse, Chicken and only Duck exists in the table she wants the result to look like this:
Duck Yes
Horse No
Chicken No
She doesn't want to return only those values that match so a simple WHERE Animal IN (#ReportParameter1) isn't going to do it.
I can make this work for a single value parameter like this:
DECLARE #FarmAnimals AS TABLE (Animal varchar(50))
INSERT INTO #FarmAnimals VALUES (#ReportParameter1)
SELECT Animal
,'In Barnyard’ = CASE WHEN EXISTS
(SELECT *
FROM tblBarnyard
WHERE BarnyardAnimal = Animal)
THEN 'Yes'
ELSE 'No'
END
FROM #FarmAnimals
But is it possible to loop through a multiple value parameter of unspecified length and create an INSERT INTO statement for each individual value? If this isn't possible I'm happy to tell her it can't be done but I can't think of a time I've found that something was impossible in SQL.
There's no need to do it in SQL, you can just write a custom code function to iterate through the selected parameter values to get the result that you want.
On the Report menu, access Report Properties... and select the Code tab. Insert the following code:
Function IsFieldSelected (fieldValue as String, ByVal parameter As Parameter) As String
Dim Result As String
Result = "No"
If parameter.IsMultiValue Then
For i As integer = 0 To parameter.Count-1
If (fieldValue = parameter.Value(i)) Then
Result = "Yes"
End If
Next
Else
If (fieldValue = parameter.Value) Then
Result = "Yes"
End If
End If
Return Result
End Function
then use this expression in the cell that you want the "Yes/No" to access the result:
=Code.IsFieldSelected(Fields!MyField.Value, Parameters!MyParameter)
Note that you are passing the parameter object here, not the Value property (so don't put .Value on the end). We access the Value property of the parameter object in the custom code function and compare it to the field value passed.
This will work for both single- and multi-value parameters.
You can do this using the STRING_SPLIT function in SQL Server.
--Placeholder table
DECLARE #ExistingValuesInTable TABLE (Val VARCHAR(255)) ;
INSERT INTO #ExistingValuesInTable (Val) VALUES ('Duck') ;
--
DECLARE #UserInput VARCHAR(255) = 'Duck, Horse, Chicken' ;
SELECT ss.value,
CASE WHEN evit.Val IS NULL THEN 'No' ELSE 'Yes' END AS AlreadyExists
FROM STRING_SPLIT(#UserInput, ',') AS ss
LEFT OUTER JOIN #ExistingValuesInTable AS evit ON ss.value = evit.Val ;
There must be a way in SSRS to have multiple conditionals for separate data filters? I have an input report level filter #reportparameter, and data item "Checknum" I need to do something resembling the following:
if #reportparameter = "C" and Left(Fields!Checknum,2) = "NC", filter
otherwise
if #reportparameter = "E" and Left(Fields!Checknum,2) = "VR", then filter
two separate conditionals, both compound statements.
What does the SSRS Dataset look like, as far as syntax?
If you want this as part of the query, you could add it to the WHERE clause:
WHERE (#reportparameter = 'C' and Left(Fields!Checknum,2) = 'NC')
OR (#reportparameter = 'E' and Left(Fields!Checknum,2) = 'VR')
But if you want to do it in the dataset filter, your filter Expression would be like
=IIF((Parameters!reportparameter.Value= "C" AND LEFT(Fields!Checknum.Value, 2) = "NC")
OR (Parameters!reportparameter.Value= "E" AND LEFT(Fields!Checknum.Value, 2) = "VR")
, 1, 0)
The type would be Integer and the Value would be 1.
I'm expecting only the names to appear on the calendar if they exist and nothing if they don't.
I created an expression where it returns only the last name of a person by using the comma as a delimiter.
My current expression:
=iif(IsNothing(Fields!EmployeeName.Value), nothing, Left(Fields!EmployeeName.Value,-1 + InStr(Fields!EmployeeName.Value, ",")))
Current results where #Error appears if a name doesn't exist:
The error is occuring because you are passing in a number less than 0 to the left function. When your string does not have a comma in it you are passing in -1.
To handle this I added two if statements to the expression. The first will return the whole string if the index of the first comma is 0. The second checks for the -1 condition and passes a 0 to the left function when that occurs.
=
iif(
IsNothing(Fields!EmployeeName.Value),
nothing,
iif(
InStr(Fields!EmployeeName.Value, ",") = 0,
Fields!EmployeeName.Value,
Left(Fields!EmployeeName.Value,iif(-1 + InStr(Fields!EmployeeName.Value, ",") < 0, 0, -1 + InStr(Fields!EmployeeName.Value, ",")))
)
)