Create a dataset which is a subset of another dataset in SSRS - reporting-services

In my SSRS report, I already have a dataset A(by running a SQL script), and parameter P1 use all the records in A. Now I want to get a subset of A, and use another parameter P2 to refer to it.
Is it possible that get the whole and the subset of the dataset at the sametime and only run the script once?
I guess creating a shared dataset is a possible way, but the dataset A is just for locally use and shouldn't be shared.

Short answer
No, it is not possible.
Alternative
You can modify your query in order to it returns one column for populate the P1 parameter and other column to populate P2. Example:
select 'Foo' P1, 'Foo' P2
union all
select 'Bar', 'Bar'
union all
select 'Foobar', null
Returns:
P1 P2
Foo Foo
Bar Bar
Foobar null
Use P1 column for populate the P1 parameter and P2 for populate P2 parameter.
Note the subset column (P2 in my case) has less values than P1,
if your parameter is set to allow NULL values, it will show the NULL
option in select list otherwise it won't.
This solution could work for you but if you need the dataset runs only once I am unsure of that, I think SSRS will run the query for every parameter even if both parameters are being populated from one dataset.
Let me know if this helps.

One way I've achieved this is with grouping. If Dataset A already has all the stuff you want, you can group that dataset with criterion P2 = TRUE. This splits Dataset A into two groups -- one where P2 condition is true, and the other where P2 condition is false.
For instance consider a dataset with two columns, Label and Amount. I want to subset my data where Label = "LabelNameOne". I create a group around my dataset with expression =Fields!Label.Value = "LabelNameOne", which then automatically creates a subset for me. Assuming you wanted it to filter on a user-chosen parameter at run time, you just sub in that parameter in your grouping expression: =Fields!Label.Value = Parameters!P2.Value.

Related

BO Webi - Need Variable(s) to Remove Nulls for Crosstab Table

I'm looking to create a variable to remove unnecessary null values from my query results. I will also need that variable to create a crosstab to count the acuity levels by day. Some of the query results return a line with a numeric result and line with a null result. Others will just return one or the other. The caveat is that I need to keep the null results if they are the only result for that patient. Please see sample data below and a sample cross tab. The cross tab is using the current acuity dimension which includes the additional null values I don't want. The other fields on the cross tab are Tracking Date and # of Patients (see below).
=FormatDate([Start Tracking Date & Time];"MM/dd/yyyy")
=Count([Financial Number])

MS Access - Query - unique values

I have a query where fields are as follows:
UniqueID | RefNum | FirstName | Surname | Aim |.....
UniqueID - is a unique field (no duplicates)
RefNum - contains duplicates
What I'm trying to do is to create a new query (based on the above or amend this one) to extract only records with unique RefNum (remove duplicates from the RefNum field)
The way I did it was select 'Group By' RefNum in the Query Design View and selecting 'First' for the rest of the fields. It achieves what I need.
The problem is that if I switch to the Datasheet View (and subsequently export it to excel to be sent out) the field names are 'FirstOfUniqueID', 'FirstofFirstName', 'FirstOfSurname', etc. Is there a way of keep the original field names (not prefixing them with 'FirstOf') or is there another way of achieving it?
The query designer automatically assigns an alias for a field expression which is based on an aggregate function. So, if you switch from Design View to SQL View for your query, you will see something like this included in the SELECT field list ...
First(FirstName) AS FirstOfFirstName
You can change the alias to something else, and you have a lot of flexibility. However, at least in some cases, when you attempt to re-use the base field name as the alias, Access complains about a "circular reference". I don't know whether that would happen here, but you can try it like this ...
First(FirstName) AS [FirstName]
Whether or not that does what you want, I'll suggest you consider a different query strategy which almost completely avoids the field name alias issue. First test this query to confirm it returns suitable RefNum/UniqueID pairs. If your base query is named Query1 ...
SELECT q1.RefNum, Min(q1.UniqueID) AS MinOfUniqueID
FROM Query1 AS q1
GROUP BY q1.RefNum
Assuming that one returns the correct rows, join it back to the base query to select only the base query rows which match ...
SELECT q.*
FROM
Query1 AS q
INNER JOIN
(
SELECT q1.RefNum, Min(q1.UniqueID) AS MinOfUniqueID
FROM Query1 AS q1
GROUP BY q1.RefNum
) AS sub
ON q.UniqueID = sub.MinOfUniqueID
If you switch the view of your query to SQL view, you will see for example AS FirstOfFirstName.
Change this to AS FirstName and follow this on the other fields.
If you prefer doing this in design view you can do so by adding FirstName:
in front of the fieldname and so on:

Conditionally grouping tablix in SSRS reporting

My tablix has following columns
Country, City, College, totalstudent,passedstudent,failedstudent
I have a parameter "GroupBy" with values "country/city/college". So, when i select one of the parameter, my tablix should be grouped by that parameter and only so that parameter column + total,passed,failed columns
For eg. If user selects GroupBy = "City" then, the tablix will show
City, totalstudent, passedstudent, failedstudent
The values in total, passed and failed is total sum grouped in that city.
The similar logic should be applied to country and college also.
So, far i am able to show the all columns with grouping applied by country, then city and then college. (which is one of simple ssrs grouping)
(My need is to only apply the one grouping based on parameter and only show that particular columns)
Note: I have a raw dataset that has all these values retrieved using inline sql from my sql database.
(Dataset1: Country,City,College,totalstudent,passedstudent,failedstudent)
You can group your table by an expression. In your group properties you would write something like this:
=Switch(Parameters!GroupBy.Value = "City", Fields!City.Value
, Parameters!GroupBy.Value = "College", Fields!College.Value
, ...)
The other columns should be able to just use regular Sum functions and work with any grouping.
I tried the different approaches (I went through the last/3rd approach)
Used multiple tablix for the different grouping conditions and used the parameter to hide/show the correct tablix in the report
Pitfall: It is awfully slow since I have 4 different grouping conditions and putting in 4 different tablix.
Using the dynamic grouping concept as illustrated at ( http://www.advancedssrs.com/2014/01/how-can-i-use-dynamic-grouping.html) which is good. But It was not suitable for my situation as it was also comparatively slow while i try to sort the columns in SSRS. (I need the sorting feature in all columns)
Created a new parameter (groupby) which is used to mimic the scenario of tabs. So, when the user selects one of the value ( Country/City/College), I am grouping my SQL result based on this groupby parameter value using conditional grouping statements and returning result back to SSRS.
SELECT
CASE WHEN #GroupBy = 'Country' THEN CountryName
WHEN #GroupBy = 'City' THEN CityName
WHEN #GroupBy = 'College' THEN CollegeName
END AS GroupTitle,
SUM(totals) AS totalstudent,
SUM(passes) AS passedstudent,
SUM(fails) AS failedstudent,
FROM #temp
GROUP BY CASE WHEN #GroupBy = 'Country' THEN CountryName
WHEN #GroupBy = 'City' THEN CityName
WHEN #GroupBy = 'College' THEN CollegeName
END
ORDER BY 1
DROP TABLE #temp
Now, I use the GroupTitle in my first column and rest of the aggregated values in the remaining columns in a single tablix.
The output looks like:
When creating your dataset, don't directly write your query in the box --- instead, use expression.
="SELECT " + Parameters!GroupBy.Value + ", passedstudent, failedstudent
FROM Table GROUP BY " + Parameters!GroupBy.Value
Your "passedstudent" and "failedstudent" will be some aggregation, either COUNT or SUM, depends on your case.

SSRS Multiple Value parameter filter based on dataset

Say I have a dataset in SSRS called DataSet1 that looks like this:
CREATE TABLE #data
(ID int, Value int, UserID varchar(2))
INSERT INTO #data VALUES
(1, 1000, 'AA'),
(2, 2000, 'AA'),
(3, 3000, 'BB'),
(4, 2000, 'BB'),
(5, 1500, 'BB'),
(6, 1800, 'BB'),
(7, 1700, 'CC')
..and that my report just presents this as a table.
Let's then say I want to add a parameter in that report, that let's the user filter the table by UserID. I want it to be a multiple value parameter where they can choose which users to include in their report. In this case I want the list to be AA, BB and CC.
So far I have done it by creating an extra SQL query based dataset like this:
DataSet1:
SELECT ID, Value, UserID
FROM table
DataSet2:
SELECT DISTINCT UserID
FROM table
And then have the parameter get its available values from DataSet2. However the query I have in my particular report is a very long and complex query, that I would prefer to not use in two datasets. If I have to go back and change something in the query, I would have to maintain that in two places, and so on.
In short: Is there a way to have the available values of my parameter by something like this:
SELECT DISTINCT UserID FROM DataSet1
Thanks!
I decided to write my comment on your answer, M.Ali, as an answer to my own question, as I found a way to solve this. Maybe I didn't explain my problem precisely enough. I am aware of how the above thing works, and how to pass parameters based on one dataset, down thru the SQL to create another dataset, multiple values allowed or not. I appreciate your answer though!
The problem I have is that the query that would define my list of values for the parameter, and the query for the actual dataset, are the same. And it's a HUGE query. In other words, where it says 'TABLE' in the code sample, I have several hundreds of lines of code. And my goal was to not have this whole query be defining both datasets. If I in the future have to change the query, I would have to do it in more than one place. Here's how I solved it:
I placed the main query in a shared dataset instead of embedding it into my report. And I then added a row_number function to my query like so:
SELECT ID, Value, UserID, rn = ROW_NUMBER() OVER(PARTITION BY UserID ORDER BY UserID)
FROM *my huge query*
This means that there is only one 'rn = 1' row per UserID. Then I went back to my report. My original DataSet1 would then just point to the shared dataset. The DataSet2 (the parameter one) would also point to the shared dataset with the one difference that I added a filter to that dataset saying 'rn = 1'. I then made a parameter with 'allow multiple values' that took its values from DataSet2. And it works like a charm. This way I can just go to the shared dataset when I need to update the query, and both DataSet1 and DataSet2 will be updated accordingly!
Success :)
Again, thanks for your answer!
You will have to have a separate data set for your parameter query and yes you got that part right, for the given data set:
SELECT DISTINCT UserID
FROM TABLE
This query will be used to populate the drop down list of your parameter. (Multiple values allowed or not).
For you Main query:
if you allow Multiple Selection for #UserID parameter you will write you main Data set query with IN operator as below:
SELECT ID, Value, UserID
FROM table
WHERE (UserID IN (#UserID))
If you only want users to be able to select only one value at a time the you can write the above query with a where clause something like WHERE UserID = #UserID.
Make sure you map the variables correctly. But you will have to have a separate query for your drop down list.

Two datasets issues in ssrs?

I have a column name 'APPs % of total' and it requires two different data sets to be populated .This doesn't seem to work. Any tips will be appreciated. Thanks
=(Fields!AppQty.Value/Fields!AppQty.Value,"second dataset")
The problem is: how does SSRS know which row to pull from the second dataset to get the field? So you have to use aggregation or lookups:
Method 1: Simply aggregate at the current level
There's usually no need for a secondary dataset just for your sums. You can aggregate at the group level within the current dataset by using the following formula:
=Fields!AppQty.Value / SUM(Fields!AppQty.Value, "table1_Group1")
where table1_Group1 is the group where the data is summarised.
Method 2: Aggregate the entire dataset
Aggregate at the dataset level for the either the current dataset or a secondary one:
=Fields!AppQty.Value / SUM(Fields!AppQty.Value, "SomeDataset")
Method 3: Lookup the value from another dataset
You'll need a dataset that sums the values at a group level. You usually achieve this result using method 1 and grouping, but here for completeness. So, let's say you are grouping by DepartmentId, you would have a dataset that aggregates like so:
SELECT DepartmentId, SUM(AppQty) AS AppQty
FROM MyTable
GROUP BY DepartmentId
Then lookup the appropriate value for the department from the current row (in the current table's dataset):
=Fields!AppQty.Value / Lookup(Fields!DepartmentId.Value, Fields!DepartmentId.Value, Fields!AppQty.Value, "SummaryDataset")
So the Lookup matches the DepartmentId from this dataset with the DepartmentId in the SummaryDataset and returns the AppQty value.