SSRS Optional Parameters - reporting-services

I have run into a couple situations where I have a single report, but the user requires two ways to run it. For example, they want to either enter an employee id and pull up a single employee record, or they want to enter the company and department, or multiple companies and departments, and return employee records for all selected departments & comapnies.
I know how to do the cascading parameter thing, so I can do either way, but I dont want to have 2 reports, I would like to have one report with optional parameters. I envision two tabs or check boxes or soemthing when they first open the report, that say, "Click to view single record" and "Click to view multiple records" then which ever one they choose, they can enter the parameter(s) and run.
I have been researching and I am leaning towards sub reports and/or using ISNULL in the parameters and marking them as 'allow null'. STill playing with it, but if someone has a link to a nifty tutorial, I would be much obliged. Thanks.

What you can still squeeze comfortably out of SSRS:
A multi-value company parameter based on a dataset;
A cascaded (from company) multi-value department parameter based on its own dataset;
An optional multi-value employee id parameter, based on a dataset that might filter on company/department;
An optional custom employee id parameter, plain INT input;
Your datasets would be something as follows.
For #Company:
SELECT CompanyId, -- Param value
CompanyName -- Param display in SSRS
FROM vw_AllCompanies
And for #Department:
SELECT DepartmentId, -- Param value
DepartmentName, -- Param display in SSRS
FROM vw_AllDepartments
WHERE CompanyId = #CompanyId
And for #EmployeeId:
SELECT EmployeeId,
FullName
FROM vw_Employees
WHERE (DepartmentId = #DepartmentId AND CompanyId = #CompanyId)
OR (#DepartmentId IS NULL AND CompanyId = #CompanyId) -- Optional
Then your main dataset would do:
SELECT * -- Okay, don't use "*", but select actual columns :)
FROM vw_AllMyData
WHERE EmployeeId IN (#EmployeeId) -- The cascaded param
OR EmployeeId = #SomeCustomEmployeeId -- The custom INT input param
In my experience, this is slightly clunky, but probably the best you can get out of basic SSRS. If you want more flexibility I recommend you built something in your app around it, and pass the ID as a final parameter to the report.

Related

Depending Parameters in a text box (SSRS 2008-R2)

I am working on a SSRS Report and I have two Parameters in the Prompt section, EmployeeID and EmployeeName respectively.
EmployeeName prompt depends on the EmployeeID prompt selected, can the EmployeeName be populated in a textbox once EmployeeID is selected.
Right now EmployeeID is being shown in a drop-down and the user have to go to the drop-down and select it, can it be done in a textbox?
Any help would be greatly appreciated!
Create your main report dataset (dsMain for exmaple) with a query something like
SELECT * FROM myTable WHERE EmployeeID = #EmployeeID
I'm assuming you have the first parameter setup and working correctly to return your list EmployeeIDs. For this example we'll call this parameter EmployeeID/
Create another dataset (called for example, dsEmployeeName) with a query something like
SELECT EmployeeName from myEmployeeTable WHERE EmployeeID = #EmployeeID
In the second parameter (the one you want showing in a text box), leave the 'Available Values' blank and set the 'Default Values' to be from a query. Choose dsEmployeeName as the dataset and choose EmployeeName as the Value.
NOTE This will only work the first time round. If you choose another value from the drop down, the name will not be updated.
I don't know your exact requirements but if you can get both the ID and name together, why do you need two parameters, one of which does nothing really as it's not passed to the report?

Searching a database in Report Builder 3.0

I am not sure if this is possible in Report Builder or not. I want to put a text box in a Report Builder report and then search the database for a specific person and display that in a subreport. Is this possible and, if so, how would you suggest I do it? I have tried researching it online without success.
This part will produce a parameter into which your user can type whatever they want
You need to set up your report to use parameters. You can set these parameters up to require user input either from manual entry or by picking from a pre-defined list.
Assuming you are returning your data using a SQL query, you can then reference these parameters in your dataset script. If for example you had a parameter called FirstName and another called Surname, and you only wanted to return values in your data set that matched both exactly, you would reference these parameters like so:
select PersonID
,FirstName
,Surname
,OtherDetails
from PersonTable
where FirstName = #FirstName
and Surname = #Surname
If you would rather have more of a 'search' type function, you can use the SQL like operator and wildcards, though bear in mind this will have a potentially very detrimental effect on your query performance:
select PersonID
,FirstName
,Surname
,OtherDetails
from PersonTable
where FirstName like '%'+#FirstName+'%'
and Surname like '%'+#Surname+'%'
This part shows you how to change that parameter so it provides a drop down menu. This part is optional.
If you want to provide a list of available options to select from, you can create a parameter that has a list of 'Available Values'. These can either be manually typed in by yourself - hard coding them in to the report design - or you can make them data driven by basing the parameter on a second dataset.
To get that list of people, you would want to return the ID of the person you are looking for as well as the details that are end-user friendly to be visible in the report:
-- select distinct so we have no duplicates
select distinct PersonID as Value -- Value is what is used to the query. This ideally will be a uniquye identifier
,FirstName
+ ' '
+ Surname as Label -- Label is what the user sees. This can be a bit more verbose and detailed
from PersonTable
order by Surname -- Specify how you want the options ordered
,FirstName
If you set this dataset as the source by selecting Get Values From A Query in the parameter options, you will see the drop down list appear when you run the report. Users can then select one, click Run Report and have their selection impact the data that is returned.

Reporting Services Cscading Parameter refresh

I have googled it a lot and found that usually it cannot be done. I came across one of the hacks here:
http://www.bp-msbi.com/2011/04/ssrs-cascading-parameters-refresh-solved/
But its not working for me in ssrs 2005. Just wondering if anyone else tried it in 2005.
Or is there any other hacks that can be tried.
As per this article the dependent parameter gets refreshed only when its values are invalidated by the selection in the first parameter. If we can invalidate the dependent parameter every time a parameter changes we will enforce a complete refresh. An easy way to do this is to attach a value such as a GUID obtained with the NEWID() T-SQL function.
So basically we want to introduce a fake parameter in between two real parameters. This fake parameter is supposed to return new values everytime as the storedproc behind it will add a guid to the resultset everytime that proc is called. So it forces complete refresh of the other parameters.
Now the main issue I am facing is :
Setting the default value of this fake parameter.
For the available values the storedproc behind the fake param runs and it returns data in the format say : result1,result2_GUIDFROMSQL
Now it looks like the same storedproc is called again to set the defult value if i ask it to get the default value from query. But as the storedproc is run again new guid comes and so the old value cannot be found so its not being set as desired.
I just need to figure out a mechanism to pass this guid from introduced param to the next param.
Thats where I am failing.
My issue can simply be replicated by creating a param whose datasource is this querystring.
select getdate() id, #name nid
So in this case how to set a default value for this param.
Below I'm going to present a detailed scenario and then I'll show the example implementation based on Visser and Abbi's answers.
Imagine that I have a report where each row is a project. A project has a Status column with values "In Progress" or "Complete" and a Project Manager column whose values are a person's name. Here are the rows in the table:
Project A (Status = In Progress, Project Manager = Bob)
Project B (Status = In Progress, Project Manager = Tom)
Project C (Status = Complete, Project Manager = Jack)
Project D (Status = Complete, Project Manager = Tom)
Project E (Status = Complete, Project Manager = Jill)
I want to have 2 parameters on my report
Show Completed Projects? - This is a boolean parameter
When false will only show "In Progress" projects A & B
When true will show "In Progress" projects A & B in addition to "Complete" projects C, D, & E.
Project Manager - This is a multi-value text parameter whose options and default values will need to change based on the Show Completed Projects? parameter upon which it is dependent.
If Show Completed Projects? is set to false then only "Bob" and "Tom" options will show up because they are the project managers for the in progress projects Project A & B respectively.
If Show Completed Projects? is set to true then in addition to "Bob" and "Tom" you will also have "Jack" and "Jill" show up as options because they are project managers for the inactive projects Project C & Project E respectively.
Now for the implementation:
Show Completed Projects? parameter
Project Managers dataset query (See Visser and Abbi's answers for details on how this generates a key that will change based on the independent parameter and will force SSRS to reload the default values)
SELECT
[ProjectManager_Key] =
pOuterAlias.[ProjectManager_Key] + '_' +
CAST(ROW_NUMBER() OVER(ORDER BY pOuterAlias.[ProjectManager_Key] DESC) AS NVARCHAR(MAX)),
[ProjectManager] = pOuterAlias.[ProjectManager]
FROM
(
SELECT
[ProjectManager_Key] =
pInnerAlias.ProjectManager + '_' +
CAST(ROW_NUMBER() OVER(ORDER BY pInnerAlias.ProjectManager ASC) AS NVARCHAR(MAX)),
[ProjectManager] = pInnerAlias.ProjectManager
FROM
(
SELECT
[ProjectManager]
FROM
[dbo].[Project]
WHERE
Status = 'In Progress' OR
#ShowCompletedProjects = 1
) pInnerAlias
) pOuterAlias
ORDER BY
pOuterAlias.[ProjectManager]
Project Manager parameter
General
Available Values
Default Values
Projects dataset
Query
SELECT
*
FROM
[dbo].[Project]
WHERE
(
Status = 'In Progress' OR
#ShowCompletedProjects = 1
) AND
Project Manager IN (#ProjectManager)
Parameters (Make sure to note the [#ProjectManager.Label] portion which will make it match the project on the actual project manager value from the database and not the key that we generated.
Finally I was able to resolve this. This link was a helpful start.
http://www.optimusbi.com/2012/07/16/multilevel-cascading-select/
Basically what it does is : Writing parameter query in a manner so that the dependent parameter changes its value every time you change its parent parameter.
The query adds rownumber with a "_" preceeding it. So every time user selects other values the rownumber changes and hence the query resultset.
Then when using the resultset remove the text after the underscore to get the real codes.
There is a workaround to fix this issue for all situations.
Note that the previous answer provided, writing parameter the query in a manner so that the dependent parameter changes its value every time you change its parent parameter, works for some cases but not all cases. If the dependent parameter's "Available" values change as a result of another parameter AND the dependent parameter is visible, it works, If the dependent parameter is hidden or internal, or if the "Available" values do not change as a result of the other parameter, it will not work.
The foolproof workaround is to define a custom function in the report and call it in an expression for the dependent parameter. The custom function takes the parameter's (the parameter that the dependent parameter depends upon) value as an argument, and simply returns its value. Even though function is simply taking the value and returning it, SSRS does not inspect the code and assumes that the code could do anything (generate a random number, pull files from disk, etc ...). So SSRS calls the function every single time the value changes regardless of whether the dependent parameter's "Available" values change, and regardless of whether the dependent parameter is visible, hidden, or internal.
Something like this:
public function returnArg(ByVal TheArg As String) As String
return TheArg
end function
Assume you have two parameters:
Parameter1
Parameter2
And that Parameter2 depends upon Parameter1.
Set Parameter2's value as an expression that includes the call to Parameter1 with the function, like:
=CODE.returnArg(Parameters!Parameter1.Value)
Now in this case Parameter2 simply displays the value in Parameter1, but this logic can be extended to more complex expressions and more than one parameter. As long as there is a CODE.returnArg(...) function call for each parameter in the expression, SSRS will always refresh the value.
#Abbi's second link is no longer valid. The article can now be found at:
http://www.optimusinfo.com/multilevel-cascading-select/
Unfortunately the images are broken and the article is a little incomplete without them. There is one image I was able to locate in its moved location (I could not find the others even with trying appropriate variations): http://www.optimusinfo.com/wp-content/uploads/2012/07/Multilevel-Cascading-with-Select-All-18.jpeg
This gave me the last clue I needed to get the technique working: when you're setting up the dataset parameter properties, you need to manually specify the parameter value as e.g. [#City.Label] rather than the usual plain old [#City] (which corresponds to [#City.Value].
This is because the technique alters the "Value" to a custom value which can no longer be looked up in the DB! So you need to use the user-friendly, db-existing "Label" as the parameter. (Probably should be obvious but...) The rest is all pretty standard if you understand report builder.
Also note that this link as it stands doesn't work with multi value cascading parameters. However it can easily be modified to do so: just alter the parameter stored procs to join to a value splitting function. I adapted mine from the udf_splitvarible function given at SQL Server - In clause with a declared variable
The modification is fairly simple but I'll give an example. This is from the article (with terrible formatting fixed).
SELECT
l11.CompanyName1+'_'+ CAST(row_number() over( order by l11.CompanyName1 desc) AS VARCHAR(50) )as CompanyName11
,l11.CompanyName
FROM
(
SELECT
l1.CompanyName+'_'+ CAST(row_number() over( order by l1.CompanyName asc) AS VARCHAR(50) )as CompanyName1
,l1.CompanyName
FROM
(
SELECT DISTINCT CompanyName
FROM Customers
WHERE City IN(#City)
)l1
)l11
ORDER BY l11.CompanyName ASC
Change:
SELECT DISTINCT CompanyName
FROM Customers
WHERE City IN(#City)
to:
SELECT DISTINCT CompanyName
FROM Customers
INNER JOIN udf_SplitVariable(#City,',') v ON City = v.Value
This all applies to SQL Server 2008/Report Builder 3 also.
I wasn't able to understand/apply #borkmark's answer. The limitations don't seem to apply to my case.

SSRS - Toggle subreports by parameter value

Requirements are as follows:
Grouping hierarchies should be swappable, for example Country > City > Department and Country > Department > City can be selected by the end user.
For the end user, there should be only one report.
The hierarchy that the user wants can be selected in a parameter value.
The data for the report should only be loaded once, since the query contains cpu intensive calculations.
I'm trying to do this by adding subreports for each hierarchy. Since hidden subreports are automatically loaded, I cannot toggle visibility of multiple subreports, or the data will be loaded twice. The ReportName property of the Subreport does not allow expressions. Can I use custom code to solve this, or what is a viable solution?
There are quite a few ways to do this.
If your dataset is embedded in your report, then I think the simplest way is:
Add your parameter, let's call it GroupingOrder, and hardcode your options: say set the Value to 1 for label of Country > City > Department and value of 2 for Country > Department > City
Add two calculated fields to your dataset within SSRS. (Right click on the dataset name and select "Add Calculated Field...". Name one "MiddleGroupName" and the other "InnerGroupName." Set the Field Source to formulas such as this for MiddleGroupName:
=IIF(Parameters!GroupingOrder.Value = 1, Fields!City.Value, Fields!Department.Value)
Use these calculated fields as normal groups in your report.
For bonus points, set the column titles based on your parameter: the column title for the middle group could be:
=IIF(Parameters!GroupingOrder.Value = 1, "City", "Department")
Another method could involve moving these calculations into the Grouping logic itself.
But I would steer clear of subreports in this case. They tend to hurt performance and create debugging difficulty.

Reporting Services 2005 Filter

Is there a way to do "and" "or" filters in SSRS 2005?
I have a table pointing to a dataset (stored procedure) that looks like this:
name type amount
License Plate Credit fees ($150.00)
Lieu Tax fees $1,012.12
Finance Tax City taxes $1,839.90
Finance Tax County taxes $306.65
Finance Tax State taxes $3,434.48
The user would like to see all rows with:
type = 'taxes' or
type = 'fees' and name = 'Lieu Tax'
The reason I need to do this in the report and not in the stored procedure is because
we will be creating multiple reports pointing to the same stored procedure depending on
how each client wants to lay out the display and business rules.
Requirements Clarification
I was hoping there was a way to do it in the report instead of the proc. The plan is to have many custom reports pointing to the same proc with different requirements. The idea was for report builders (who don't know SQL) to create the reports instead of us programmers always having to get involved.
Each possible condition combination is either a pass or a fail. You use a SWITCH to evaluate each possible condition, and return a 1 or a 0. Then you use an "=" and "=1" in the filter condition.
=SWITCH (TYPE = "TAXES", 1,
TYPE = "FEES" AND NAME = "Lieu Tax"), 1,
1=1, 0
)
You can handle your entire filtering in a single expression this way. Works like a charm.
You should modify your stored procedure to accept parameters like type and name and then from your report you should invoke this stored procedure with the right values based on user requirements. Reporting services reports do have a feature called report parameters. Therefore, you should convert your report to accept the type and name parameters from the user & pass this onto the stored procedure. To create multiple reports for multiple users, you will just create multiple linked reports from this one template report with different parameter values.
Change proc to be
SELECT
xxx
FROM
xxx
WHERE (a.id = #aID OR #aID IS NULL)
AND (b.id = #bID OR #bID IS NULL)
AND (c.id = #.....
Just pass in either a value or NULL from the report
Yes there is.
Open the table properties for the table or matrix with the data.
There's a 'filters' tab there allowing you to filter data after the query has been executed.