Switch expression in SSRS : Argument 'VarExpr' is not a valid value" - reporting-services

=switch(
Fields!HEORG_REFNO.Value=7535,"public",
Fields!HEORG_REFNO.Value=7539,"public",
Fields!HEORG_REFNO.Value=7609,"public",
Fields!HEORG_REFNO.Value=7541,"public",
true,"private"
)
I want to group my result based on weather the clinic is private or public, hence to do so i am trying to create a new field based on the above switch condition.
if(heorg_refno=7539 or heorg_refno= 7609 or heorg_refno=7541) then it would be public otherwise the clinic should be private.
Any suggestions what am i doing wrong in the switch statement.

One reason for this could be that Fields!HEORG_REFNO.Value does not actually contain integer values, but String values. In that case you'd have to compare against strings:
=Switch
(
Fields!HEORG_REFNO.Value="7535","public",
Fields!HEORG_REFNO.Value="7539","public",
Fields!HEORG_REFNO.Value="7609","public",
Fields!HEORG_REFNO.Value="7541","public",
true, "private"
)
You need to check the type for the HEORG_REFNO column in the SSRS dataset.
Also in my experience it is a good idea for stuff like this to create a fake column in your data set which you can filter over instead of putting too much stuff into expressions.
The latter leads to confusion because sooner or later.

Related

What is the SSRS Multi Value Data Type and how to use

I have a multi-select.
I think the underlying datatype is int || array(int). This is pretty frustrating that you have to do a check to see if a multi-value is present before jumping into an index. But how does this value get passed to SQL?
It's easy enough to use in a IN (#variable) statement. How else can it be used? Is it a string or a table. From my investigations it appears to be single table row with many un-named columns but I'm not really sure.
Finally, when you want to simulate a multi-select in a query inside visual studio, for example to "Refresh Fields" how do you do that? For example "1,2,3", {1,2,3} or #{1,2,3}. It's not (123) because that is -123.
It dpends what you are trying to do and in what context.
As you said, if you have a datset query that is a SQL script (as opposed to a stored proc) then you can use IN(#paramName). In this instance SSRS take the parameter values (not the labels) and injects them into the sql statement as a string e.g. '1,2,3'. The result would be IN(1,2,3). If you want to pass in a list of, say, countries then you would have to set the parameter values to be the same as the parameter labels So rather then Value =1, Label = Spain you would have Value = Spain and Label = Spain. Used in an IN() would generate something like IN('Spain', 'France').
If you try to do the same with a stored proc e.g. EXEC myProc #myParam, then the parameter values would be passed as a sing string which would then need to be split out by the proc.
If you just want to get a list of selected parmeter values or label shoing in your report then you can simply do something like
=JOIN(Parameters!myParam.Value, ",")
or
=JOIN(Parameters!myParam.Label, ",")
where "," is the delimiter
If you pop this expression in a text box, you'll get a list of the selected parmater values/labels
I think it's a kind of madness but I found a workaround to get a table of values from the results from SSRS. I query the IDs against a source table using IN(). I hope there is a better way of doing this?
SELECT [TblFeeBillingCycleID]
FROM [TblFeeBillingCycle]
WHERE [TblFeeBillingCycleID] IN(#intCycleId)

MDX - Dynamic exclude/include

I am creating an SSRS report that will be used by a wrapper application. This application will be passing a string parameter which optionally specifies if the query should be filtered for "Regular Employees" or "Temporary Employees" (temp is a set of employee types) and I'm struggling with the best way to code this into the MDX query.
Possible string parameter values:
"Regular Employee"
"Temporary Employee"
(null)
My parameter expression that modifies the parameter value being passed into the query:
=SWITCH(
Parameters!t.Value = "Regular Employee",
"{[Employee Type].[Hierarchy].[Employee Type].&[2]
,[Employee Type].[Hierarchy].[Employee Type].&[3]}",
Parameters!t.Value = "Temporary Employee",
"[Employee Type].[Hierarchy].[Employee Type].&[1]",
1=1,
"[Employee Type].[Hierarchy].[Employee Type].&[99999]"
)
Note that I'm specifying the inverse and using an exclude so that my catch-all actually excludes nothing
And finally the MDX query:
SELECT
[Measures].[Headcount] ON COLUMNS,
Except([Employee Type].[Hierarchy].children, {StrToSet(#t)}) ON ROWS
FROM [GER]
Now, this works just fine, I just don't like the maintainability of this query.
Assumption that [99999] will never be a valid employee type key
Have hardcoded everything to exclude for the case of the single type for "Regular Employee". Breaks if data model is updated and includes another employee type in the future.
Breaks if any of the employee type keys change in the future
So I realize it's ugly, it's just the only working example I have at the minute. Assuming that I can't modify the wrapper application to change how the parameter value gets passed, can anyone see a better way to write the MDX for this scenario that is more future proof? I really wish that the keys for this data model used the string value and not incrementing numeric keys. That would be safer imo and I should be able to achieve this type of filter by referencing names .... somehow??

Writing The EXpression in SSRS To Display any one gender based on condition

i would like to display 'his' if the sex field is male and want to display 'her' if the sex field is female...how would i write the expression for this condition in SSRS reports
You want to use Switch() or IIf(). I would recommend Switch() because I think it's easier to expand if you later need to deal with scenarios other than just "his" or "her", and it means you don't accidentally show something if the sex is NULL or an empty string or something.
Basically Switch() takes any even number of parameters, where the first in each pair is a condition, and the second is the data to return if the condition evaluates to true. You can use this to check the contents of your sex field and return his or her;
=Switch(Fields!sex.Value = "male", "his", Fields!sex.Value = "female", "her")
You can use the IIF operator to get what you want. Set the expression as,
=IIF(IsNothing(Fields!sex.Value),"defaultvalue",IIF(Fields!sex.Value = "male","His","Her"))
You should use the IsNothing to make sure to handle the NULL value from the database.

SSRS Report - Dataset Filters

I've written a report for SSRS and Im using dataset filters with expressions to filter the report info. I seem to either have this expression wrong or the filter is not working correctly:
=IIf(Parameters!DoctorID.Value = "All" Or Parameters!DoctorID.Value = "", "*", Parameters!DoctorID.Value)
What I want to accomplish with the above code is if DoctorID = ALL or "" (blank) then I want to omit it from the filters so I return information for all doctors. However, whenever the value of DoctorID = ALL, I'm returning no rows what so ever. It should be the case that i'm getting ALL rows since DoctorID is not a specific number.
Does the "*" (star) not denote an omitting of that filter? Am I doing something wrong here?
Thanks!
The filter formula you provide is only half the equation: what is the operator and what are you comparing this to? And yes, I haven't seen SSRS use asterisk as a wildcard.
Consider putting your filter into the query for the dataset. The SQL WHERE clause can get pretty powerful. I would write your filter into the query as
...
WHERE
#DoctorID = 'All' OR #DoctorID = ''
OR #DoctorID = myTable.DoctorID
This will also let you move to a multiple value parameter pretty easily.

Code not running when Field is NULL - SSRS 2005

I have a textbox in my SSRS 2005 report. The expresssion for this textbox is:
=IIF(IsDBNull(Fields!fOrgID), Code.SetMyVar("null"), Code.SetMyVar(Fields!fOrgID.Value))
I have also tried IsNothing(Fields!fOrgID) and a few other variations of checking for nulls.
I have modified the SetMyVar function for testing and it now looks like this:
Public Function SetMyVar (var as String)
MsgBox(var, VbOKCancel, "Test1")
If var Is Nothing Then
Return "NOTHING"
Else
MyVar = var
Return var
End If
End Function
I also have the public variable MyVar:
Public Shared Dim MyVar as String
When my database query returns data, this correctly evaluates, a messagebox is displayed with the value, the textbox gets set with the value, and the world is generally a happier place.
When my database query does not return a value though, I get the error:
The query returned no rows for the data set. The expression therefore
evaluates to null.
and the SetMyVar function never appears to be ran (you never get the messagebox popup). As expected, my emotions range from anger, sadness, and bitter hatred of SSRS.
I read something about SSRS evaluating both sides of an IF statement, so perhaps that is why I get the error (likely then on Code.SetMyVar(Fields!fOrgID.Value))... not sure how I get around that though.
Thoughts? Suggestions? Words of comfort?
From the sound of things, it seems likely that the issue is that SSRS is having a problem displaying zero records. I'd recommend one of the following:
1) Use a control that handles zero records appropriately (Tables do. I think Lists do as well).
2) Modify your query to return a single record with blank values if it would otherwise return zero records.
An answer to the original question:
=IIF(IsNothing(Fields!fOrgID),
Code.SetMyVar("null"),
Code.SetMyVar(IIF(IsNothing(Fields!fOrgID),"Foo",Fields!fOrgID.Value)))
The error was from both sides of IIF being evaluated. The extra IIF in the statement above will avoid Code.SetMyVar from ever being called with a null value.
I believe you're right about about Iif always evaluating both of its value arguments (at least, it does in Visual Basic). I'm not sure why you're getting this precise error (unless strings can't be assigned a value of DBNull?), but you almost certainly want to attack this problem with a different method.
The reason for this is that your current code will likely always call both set methods regardless of the conditional value.
Formula that worked for my SSRS 2008 reports.
=IIf(String.IsNullOrEmpty(Fields!NullableFieldwithPossibleBlankStrings.Value),"Yes","No")
I tried this too (also tried a version with IsNothing)...
=Code.SetField(IsDBNull(Fields!fOrgID))
And changed the function to be one that accepts a boolean. I figure this above function would always return a true or false, but in the event of a NULL, I again get "The query returned no rows for the data set. The expression therefore evaluates to null.".
I need to pass back to my code if the field is null or not (as this will let me know if the datasource is null or not).
Let me know if you can think of a better way because I cannot.