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??
Related
I am passing a unique ID as parameter in SSRS report. In the source table, unique id does not contain dashed. However, the user may insert Unique ID including dashes "-" and in some cases without dashes. Is there a way that we could remove dashes from the parameter.
For example, unique id 3120-20268-8 is stored in table as 3120202688. How I could retrieve if user pass multiple values with or without dashes in the SSRS Report.
When is used below query, it gives record against single value only. However, gives error when more than one values are provided.
select * from Table
where Unique_ID in (REPLACE(#Unique_ID,'-',''))
For more than 1 values, it gives errors mentioned below:
The replace function requires 3 argument(s).
Query execution failed for dataset 'ATL_List'.
Thanks
One of the simplest mechanisms for this is to create an expression based parameter to hold the sanitised input. This parameter would be hidden so the user is not aware of it, but the rest of the usage of the parameter is the same.
NOTE: You could do something similar with a query based default value, but this case is easier to do via a simple expression
Single Value Parameter
Create a new parameter:
set it to hidden
Set the default value expression:
=Str(Parameters!inputID.Value).Replace("-","")
Multi-Value Parameter
This is only slightly trickier, in the expression we can join the selected values together into a CSV string, then process that value and then split it back:
Set the parameter to multi-value, but still hidden:
Set the default value expression:
=Join(Parameters!inputID.Value,",").Replace("-","").Split(",")
Without going to detailed, if we made the sanitised parameter temporarily visible, just to demonstrate the conversion, it should look like this:
The parameter MUST be hidden!
NOTE: DO NOT make your sanitised parameter visible as in the above screenshot in your deployed report! Doing so will mean that it will not pickup changes made to the input value after it has rendered the first time.
remember that we have exploited the default value, we haven't arbitrarily defined en expression to always execute.
The output when the parameter is hidden is calculated when the report is rendered, it's just harder to visualise the behavior in this static post:
In your DataSet query you would just use the sanitised parameter:
SELECT * FROM Table WHERE Unique_ID IN (#sanitisedMultiValue)
You should be able to use the replace function in your report to format the parameter value after it has been entered, something like the below
replace(Fields!Paramater.Value,"-","")=FieldinYourTable
I have a dataset which looks like this
Name Spend
"First Aid" 2
"Healing Arts" 0
"Surgeon" NULL
I then have three separate textboxes which will be filled with the value of the column which matches the name.
Example: show value of spend in textbox if value of name equals First Aid
for this I've made following expression
=Lookup(Fields!skill_name.Value, "First Aid", Fields!skill_spend.Value, "Skills")
My problem is however that I get an error saying that skill_name is missing its dataset, which doesn't make sense to me as it is informed in the end of the expression (skills)
I think you may be misunderstanding the purpose of Lookup and how it is used. The purpose of the Lookup function is akin to a JOIN in SQL in some ways. Basically, you would have two datasets that each have a matching field with the other. In that scenario, the expression would match on the skill_name field and lookup the skill_spend value and the expression would look something like the following.
=Lookup(Fields!skill_name.Value, Fields!skill_name.Value, Fields!skill_spend.Value, "Skills")
As the documentation shows, the first reference to skill_name is the field you are referencing from the current dataset. The second reference is to the dataset from which you are attempting to look up a value. The third expression is the field you are looking up and the dataset should be the one you are attempting to look up a value from, not the current dataset scope.
Lookup(source_expression, destination_expression, result_expression, dataset)
From the best I can tell, you have a single dataset but separate textboxes that need the correct spend value. I think the following expression will work.
= IIF(Fields!skill_name.Value = "First Aid", Fields!skill_spend.Value, Nothing)
This expression should get the skill_spend value associated with the row "First Aid" only and leave the textbox blank otherwise.
=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.
I have added a Filter in Row Group-> Group Properties to perform sum of quantity only for those transactions which have done before a certain date.
Whenever I am selecting the 'Cdate' as Expression field from my dataset1, the type is showing as Date/Time but after saving it when I check,I found it as 'Text'. As a result the filter for 'cDate' is not working during report generation.
Note that, I can't filter the data in dataset side or tablix side as I have to show all the column items. This is a matrix report.
OK this is going to be a bit confusing because your dataset contains a field with the same name as one of the built-in expression functions ("CDate" - Convert to Date).
I sometimes run into these datatype issues when using filters and I find the best way to handle it is to force both the filter field and the filter value to be the same data type.
So in your case try setting the Filter expression to:
=CDate(Fields!CDate.Value)
then select the operator as "<=" and set the value using an expression as well:
=CDate(Parameters!MyParameter.value)
and see if that works.
I understand you so that your date field in the dataset is called CDate, try casting it as date, so instead of selecting it in your filter, type the following into the filter
=CDate(Fields!CDate.Value)
I have a report which displays a table full of raw data.
Prior to entering this report, the parent report asks you to select a 'Service' & 'Department'
Depending on which Service/Department you select from the parent report, this RAW data will be filtered to show the relating data.
Straight forward enough, and it works, great.
I have a new requirement now.
If the chosen Service is equal to 'Service X' I need the data to be filtered again on that Service, department, but also to add aditional filter, on their 'team'.
so that the data will also be filtered where the team matches the user running the report's team.
I have a dataset already created which returns the user running the reports 'team'
And also a new parameter called 'team' which defaults to the user running the reports AD number'
The new requirement is, if the Service = X, then filter the data on the department but also on THAT users 'team', if the Service is not equal to X, do nothing.
I think I need to alter the Filters section of the Tablix Properties but am not sure what I need to put in the Expression, Operator, Value
So far I have tried =IIf(Fields!Service.Value = "Service X", Fields!Team.Value, nothing) in the Expression, set the Operator to In and tried filtering on the 'team' from my new dataset which stores the current users 'team' but it is not working.
Does anyone have any suggestions?
For these sorts of conditional filters I've had best results with using the IIf statement (or whatever) to return a string and filtering based on that, e.g. something like:
=IIf(Parameters!Service.Value <> "Service X" or Parameters!Team.Value = Fields!Team.Value
, "Include"
, "Exclude")
Then you can set the Operator to = and the filter value to Include. Just seems to a bit more robust in my experience.
Reading over this, you could even set the IIf statement up as a Calculated Column in the dataset and filter on that.