Format numbers in data field of SSRS Matrix with two different number types using an expression - sql-server

So, I have an SSRS report with a matrix. The columns, rows and data field are fed by expressions that refer to parameters. Effectively giving the report a pivot table style effect.
The data field has two values in the #prmDataField parameter to pick from when running the report. One is called LengthOfStayand in the databse is all integers. The second is called FinalCostand in the real world would be currency (GBP in this case) but of course in the database it's just a float.
There's a 4th parameter to apply an aggregate function to the data in the data field that's as follows;
=SWITCH
(
Parameters!prmFunction.Value="Avg",AVG(Fields(Parameters!prmDataField.Value).Value)
,Parameters!prmFunction.Value="Sum",SUM(Fields(Parameters!prmDataField.Value).Value)
,Parameters!prmFunction.Value="Max",MAX(Fields(Parameters!prmDataField.Value).Value)
,Parameters!prmFunction.Value="Min",MIN(Fields(Parameters!prmDataField.Value).Value)
)
My problem: I'm hoping there's a way to enter an expression into the Number format property of the data field textbox of the matrix so the data on the report will format depending on which of the datafield parameter values have been selected to run the report with. So if you pick LengthOfStaythe report will display the numbers as appropriately formatted integers. If you pick FinalCostthe report will display the numbers as currency instead.
I attempted this;
=SWITCH
(Parameters!prmDataField.Value=Fields!Length_of_Stay.Value,"G-int32"
,Parameters!prmDataField.Value=Fields!Spell_Final_Cost.Value,"C")
But it didn't work and I got the following warning back once the report had run;
Warning 1 [rsRuntimeErrorInExpression] The Format expression for the textrun ‘Textbox17.Paragraphs[0].TextRuns[0]’ contains an error: Input string was not in a correct format.'
Is it even possible to have two different number types for formatting depending on a parameter value or any other value?

Related

Why does the SSRS throw #Error for displaying Multi Values

I am using a split_string delimiter to be able to choose from multiple values from the SSRS report. I am able to select Multi Values in the SSRS report but the SSRS displays #Error where the multi value should be.
Picture for reference
This is what my expression for the report looks like
The count and ismultivalue in the expression works just fine
For Example:
I am using isMultiValue in the expression and it works fine
but Label and value throws #Error
In your report expression you are referecing the Parameter rather than the value that is passed to your Dataset, so successfully using a string_split function in the Dataset is actually unrelated here.
This is because multi valued parameters are Objects and not simple scalar values. When you do a count on the Parameter you are counting the items in the Object, which is a valid operation.
However, the Parameter object can't show you a single Value or a Label as it doesn't know which Value or Label to show you, even if there is only one item in the Parameter object.
What you need to do instead, is use a zero based Index value to specify which item you want to display:
="Bkt: " & Parameters!BKT.Label(0)
or tell SSRS to display all the items within the Parameter object as a list:
="Bkt: " & join(Parameters!BKT.Label,", ")

How to hide columns that are not returned with dataset results using function IsMissing in SSRS

I have a stored procedure which drops either 1 or 2 columns from the result set. In my SSRS report, I have all possible columns that may come from the result set as dataset fields. I am trying to use the IsMissing function to change the column visibility, but it is not working. How can I make this functionality work?
I tried using examples from
https://sqlserverbi.blog/tag/hide-columns/
and
https://www.allaboutmssql.com/2013/01/ssrs-ismissing-visibility-function.html
where I right click the column, select Column Visibility and add an expression to the Show or hide based on an expression line.
Here is how I have been trying to use the expressions:
When I want to hide 2 of the columns I use:
=Fields!LINE_30_A.IsMissing And Fields!LINE_30_B.IsMissing
and when I want to hide only one of them I use:
=Fields!LINE_30.IsMissing
I have also tried the expressions like this:
=IIF(Fields!LINE_30_A.IsMissing And Fields!LINE_30_B.IsMissing,True,False)
I ran my stored procedure in a way that has dropped 1 of the columns from the result set. In this case the expression Fields!LINE_30.IsMissing should have been evaluated and I should be seeing the columns for Fields!LINE_30_A and Fields!LINE_30_B. But I only see the column for Fields!LINE_30 (which shouldn't be showing at all) and the other 2 columns are hidden. I also receive these 2 warnings:
1) Warning [rsErrorReadingDataSetField] The dataset ‘DataSet1’ contains a definition for the Field ‘LINE_30’. The data extension returned an error during reading the field. There is no data for the field at position 11.
2) Warning [rsMissingFieldInDataSet] The dataset ‘DataSet1’ contains a definition for the Field ‘LINE_30’. This field is missing from the returned result set from the data source.

SSRS Format Numeric not summarizing in excel

I need to set a format for a column when the column "KPI" has a value with '%' I need that the value has the format for a percentage otherwise the currency format, so I have something like this:
=IIF(InStr(Fields!KPI.Value,"%")>0,Format(Fields!DIA.Value, "P"),Format(Fields!DIA.Value, "C"))
That expression works properly, but when I try to export that report to excel, that column is not summarizing when I select several columns as you will see in this picture:
As you can see excel is recounting but not summarizing, I have tried formatting each field from SQL Server and with this formula:
=IIF(InStr(Fields!KPI.Value,"%")>0,Format(Fields!DIA.Value, "###.#%"),Format(Fields!DIA.Value, "###,###,###,###.##"))
But the same happend when I export that report to excel, is there another way to handle this?
In order to make sure that Excel interprets a value as numeric, you can use the RenderFormat global variable to detect when the report is being rendered for Excel and not include the problematic formatting characters:
=IIF(InStr(Fields!KPI.Value,"%")>0,Format(Fields!DIA.Value, "###.#%"),IIF(OR(Globals!RenderFormat.Name="EXCELOPENXML",Gl‌​obals!RenderFormat.N‌​ame="EXCEL"),Fields!‌​DIA.Value,Format(Fie‌​lds!DIA.Value, "###,###,###,###.##")))
If its critical that you display a thousands separator in your generated output you can use the Language global variable to detect the user's region and use the appropriate character. That check would take the general form Globals.Lanuage="en-US", depending on what particular regions are involved.
Here's an example of using the RenderFormat.Name variable to check the renderer in VS2013:
When doing a preview of the report, the format will show that the render type is RPL:

Filter Not Working in SQL Server Report Builder

I'm attempting to create a filter on a tablix in Report Builder 3.0. I click on the Tablix, choose Properties and then click on Filters. Clicked on Add and created my filter:
Expression [SYSTEM_CODE]
Operator =
Value 1005
I don't get an error message but when I run the report, nothing is showing when I know there's valid data for that system code. It doesn't matter what value I choose, I don't see anything when I run the report. It also doesn't matter if I enclose the value in single or double quotes. There is no grouping on this report so there's no filter there either.
I'm pulling my hair out because this should fairly simple.
I had a similar issue and maybe what I found will work for you. I created a simple query to show you what I found (note the Convert statement is intentional here):
Select getdate() as "MyDate", CONVERT(Decimal(12,0), 1005) As "System_Code"
I then created a filter for the System_Code value. Note the Float value in the Type field that you cannot change.
Screen shot of disabled Type field
When I ran the report, I got this error:
The processing of FilterExpression for the tablix ‘Tablix1’ cannot be
performed. Cannot compare data of types System.Decimal and
System.Double. Please check the data type returned by the
FilterExpression.
I know, it says Decimal to Double (I blame Microsoft as this baffled me too). This is happening because this type of parameter can only process Integer and Float values. Report Builder doesn't let you change them as it infers this type from the dataset (based on my own experience). If you set the query to return an Integer, the filter will work:
Select getdate() as "MyDate", CONVERT(Integer, 1005) As "System_Code"
However, checking the Type field will still be disabled, and may show as float. Selecting another value in the drop down expression field then selecting back to the System Code value will change the Type value from Float to Integer.
The funny thing is, if you try another Convert data type value, i.e. BigInt, you will get that you cannot convert from Int64 to Float (/sigh Microsoft).
The closest I could get to seeing why this is happening, after many hours of searching, was this page on Microsoft's site which discusses that the value expected is an Integer or a Float. Even though I am not performing a Sum function, the effect appears the same.
Sum Function (Report Builder)

SSRS 2012 Lookup Function Returns No Data

I have an SSRS 2012 Report that is based on a MetricsData dataset that contains the field "REPOSITORY". The report also has a second dataset, PlanITData, containing the fields:
RepositoryName
AppName
AppOwnerName
I put an expression into one of the report columns containing the following Lookup:
=Lookup(Fields!REPOSITORY.Value,Fields!RepositoryName.Value,Fields!AppName.Value,"PlanITData")
I expected it would look up the REPOSITORY from MetricsData (the dataset that should be in the scope of the report and does, in fact, display in one of the report columns), find the matching RepositoryName from PlanITData and return the AppName from PlanITData. I get all blanks in this column. I've run the underlying query for the PlanITData dataset with known values and it returns the appropriate values. Do I not understand the Lookup function? This is my first time trying to use it...
Edit: I just saw this warning. I'd swear it wasn't there earlier. No idea what this means:
Warning 1 [rsLookupOfInvalidExpressionDataType] The Value expression
for the textrun ‘LOBGROUP.Paragraphs[0].TextRuns[0]’ uses a lookup
function with an expression that returned a data type that is not
valid for the lookup function. The data type must be an RDL Variant
type.
Apparently the expression above is correct. I kept playing with the datasets and suddenly it started working. Apparently there was an issue with how the datasets were constructed but I'm not sure what it was...

Resources