Conditionally grouping tablix in SSRS reporting - reporting-services

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.

Related

Group by column & row in one part of expression, group by column only in another part of expression

I am trying to find a way to use the Group By functionality to create a table where the numerator of a fraction is grouped both by column and row, and the denominator is grouped only by column.
Here's my existing expression:
=Round(Sum(Fields!Days_In_Step.Value)/CountDistinct(Fields!ID.Value),1, MidpointRounding.AwayFromZero)
When grouped by rows (groupName) and columns (month/year) the numerator (Sum(Fields!Days_In_Step.Value)) gives me good data, but the denominator (CountDistinct(Fields!ID.Value)) is also grouped by row (groupName) and I don't want that.
I have a SQL solution but am trying to do this entire within SSRS expressions, if possible.
edit
Sample Data:
It would look like this. The background is that these groupings are counts of days and the "all" are counts of tickets, so we are trying to see who is sitting on their tickets longer.
Here is a mock-up including a sample data set using a pivot table:
Edit 2
Here is a full sample data set:
https://docs.google.com/spreadsheets/d/1rYPMcrLNB-FZN64Fn2-y3FtnM2iQo2VMH7YTdfiVnKM/edit?usp=sharing
I need to group on month as well as year, and I do not want to see "Exclude" in the group rows, however they cannot be filtered out of the tablix without being removed from the overall population, which is required for the denominator.
Your problem is caused by the scoping of aggregate functions. When you use aggregate function they run under the scope where it is placed in the tablix by default. In your case Sum() and CountDistinct() functions are running in both row groups (Owner Group) and column group (Month Group).
Fortunately, you can specify the scope that you want your aggregate function computes the aggregation, simply add the group name in the function:
CountDistinct(Fields!ID.Value,"MonthGroup")
The whole expression is like this:
=Round(Sum(Fields!Days_In_Step.Value)/
CountDistinct(Fields!ID.Value, "MonthGroup"),1, MidpointRounding.AwayFromZero)
Replace "MonthGroup" by the actual name of your group in columns
group.
This is result using the sample data you provided:
I've used this expression to show you how it is produced:
=Sum(Fields!Days.Value) & "/"
& CountDistinct(Fields!Ticket.Value,"MonthGroup") & "=" &
Sum(Fields!Days.Value)/CountDistinct(Fields!Ticket.Value,"MonthGroup")
Note my column group is named MonthGroup.
UPDATE: Scoping multiple groups in CountDistinct function.
Firstly I am not filtering the dataset, I prefer hide the Exclude rows using the below expression in the Hidden property of the Row Visibility window:
=IIF(Fields!Group.Value="Exclude" OR Fields!Group.Value="-1",true,false)
To count distinct id grouping by Month and Year but not by Group you can create a child group below Month group as you can see below:
My group is called Group2 and I used this expression in the Group on textbox.
=Fields!End_Month.Value & "-" & Fields!End_Year.Value
It will create a group per every Month-Year combination. When you create the group it will be added as a column group so you will have to delete the row so you will be prompted if you want to delete the group and row or delete the row only. Delete only the row leaving the group.
Now the expression you have to use is
=Round(Sum(Fields!Days.Value)/CountDistinct(Fields!ID.Value, "Group2"),1,MidpointRounding.AwayFromZero)
Replace Group2 by the name of the created group in your case.
This is the whole recreation of your scenario:
Let me know if this helps.

Need custom groups based on strings in ssrs

Ok so I can do this in crystal reports but my users can not view live data which is a problem so I want to move the report to ssrs.
What I have is pretty simple. It's a report that lists businesses and number of clients. I need two groups one that has a specific business name like "Business1*" and the second group has businesses with an identifier at the end like "*[1234]".
The rest of the business can all be in one group and each group needs a total at the end. My issue is with creating this custom groupings.
If you are doing this in SQL then you can have a calculated field in your SQL statement to impose a sort order:
SELECT CASE WHEN BusinessName LIKE 'Business1%' THEN 1 WHEN BusinessName LIKE '%[1234]' THEN 2 ELSE 3 END AS SortOrder, *
FROM MyTable
ORDER BY SortOrder

SSRS having multiple dataset Error

I have a Dataset1 which has all records.
I have Dataset2 and Dataset3 for Dropdown filteration.
Dataset1 has a query as follows : select * from vw_shoppinghistory where storename =#storename and city = #city
Dataset2 has a query as follows : Select Distinct StoreName from vw_shoppinghistory
Dataset3 has a query as follows : Select distinct city from vw_shoppinghistory where storename = #storename
On selecting store and then the city should pull the records.
I am facing an error as
" When report contains multiple datasets, field references outside of a data region must be contained within aggregate functions which specify a dataset scope "
You are referencing a field in a dataset on a control that doesn't count as a data region (data regions being things like charts, tables, lists, etc). Most commonly you'll see this on textboxes.
To fix this, you need to do 2 things:
Use an aggregate expression. You can't reference a field outside of a data region without performing aggregation. You can use functions like SUM, FIRST, MIN, MAX, etc.
Specify a dataset when referencing a field. For example, your code that's causing this error is going to look something like this:
=Sum(Fields!city.Value)
What you'll need to do is specify the dataset the field comes from, like:
=Sum(Fields!City.Value, "Dataset1")

Dataset in Custom Code

How do I calculate the formula Sum([PRODUCT_VAL] ) for Distinct( [ID]) in SSRS?
[PRODUCT_VAL] and [ID] are two columns in the dataset.
I think I need to iterate through rows of a DataSet in custom reports code, to find Distinct [ID] and make SUM for those values [PRODUCT_VAL], but is it possible?
Or is there a better solution?
It sounds like you are trying to do a sum where the ID is a certain value, similar to the SUMIF function in Excel? Two ways you can achieve this (let's say 1 is the value of ID that you want to select on):
SSRS Expression
=SUM(IIF(Fields!ID.Value = 1, Fields!PRODUCT_VAL.Value, 0.00))
SQL in your dataset
SELECT ID, PRODUCT_VAL, CASE WHEN ID = 1 THEN PRODUCT_VAL END AS ProductValForID1
FROM MyTable
and then sum on the ProductValForID1 field.

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.