Two datasets issues in ssrs? - reporting-services

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.

Related

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.

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.

Error in finding sum of a group and Conditional Summing in SSRS Reports

I have an SSRS Report, in the database there is a column by name Total_running_hours.
There are more than one record for a single Cycle_number like more than 1 row with same Cycle_number but different Block_numbers and the value in Total_running_hours field will be same for all the rows with same Cycle_number. Eg. 1 Cycle number with 4 diff block_numbers contain same Total_running_hours for all 4 rows.
Now the problem is, in the group footer if I put this field then it will show the Total_running_hours value only once which is correct, but my final requirement is,
I need to get the sum of this field in the Report footer which need to display the sum group wise. No matter how many rows are there for a single Cycle_number it has to take only once and display the result.
I tried in different ways like
=sum(ReportItems!textbox204.Value) // name of text box in Group footer
Error: Report item expressions can only refer to other report items
within the same grouping scope or a containing grouping scope.
=sum(Fields!total_running_hours.Value,Group_name)
Error: The scope parameter must be set to a string constant that is
equal to either the name of a containing group, the name of a
containing data region, or the name of a data set.
Can any one please help me in getting the sum Group wise
Thank you in advance.
I found solution for this Problem.
We cannot simply sum the Total_Running_hours value as this would give us duplicates and the incorrect answer. We cannot sum the reporting services group as it goes out of scope
There is no SUM DISTINCT available in Reporting Services 2005 so we can't get the distinct value that way.
Since the query may not return a particular Cycle_Number Type we cannot use that as a filter.
The solution found was to add a column of the row number within a windowed set partitioned by the Cycle_Number like this
ROW_NUMBER() OVER (PARTITION BY Cycle_Number ORDER BY Cycle_Number ) AS 'RowNumber'
Then in the reports’ footer total column we put an expression that only takes the first row’s value to sum and converts all other rows to zero in that windowed set.
=SUM(IIF(Fields!RowNumber.Value=1,Fields!Total_Running_hours.Value,0))
After using this if u found any error in textbox like #Error
Then try this
=SUM(IIF(Fields!RowNumber.Value=1,CDbl(Fields!Total_Running_hours.Value),CDbl(0.0)))

Retrieve Row Count on SSRS / MySQL

I'm using SSRS linked to MySQL by ODBC. My query sums the payment amount by customer, and sorts by amount desc. I want to use this to create a LeaderBoard in SSRS, showing the Rank, and only including the top 10 customers.
Option 1:
Do an additional query on my group query in SQL, adding the Row Number.
Option 2:
Add a calculated field in SSRS.
Option 1 seemed bulky, so started with Option 2; I added a calculated field to the dataset called "Rank", defined as =RowNumber("DataSet1")
I added a calculated field to the dataset called "Rank", defined as: =RowNumber("DataSet1")
But I got the following error:
The expression used for the calculated field 'Rank' includes an aggregate, RowNumber, RunningValue, Previous or lookup function. Aggregate, RowNumber, RunningValue, Previous and lookup functions cannot be used in calculated field expressions.
So, I then added it to the actual tablix, and I was able to get the Rank to show correctly.
(When I did this, it automatically added an extra column to my dataset.
I then wanted to filter the top 10 customers.
I first tried by "bottom 10" on this new field, but it didn't work. (Seems this field is all zeros in the actual dataset.)
I then tried by "top 10" on the payment amount, but received an error that the filter only supports Integers.
So I tried to convert the payment amount to Integer in MySQL, using CAST and Convert, but they don't support conversion to Integer, and SSRS didn't like 'SIGNED' or any of the other options.
I then started trying Option 1, which was building the query into MySQL.
I added:
SET #rank=0;
SELECT #rank:=#rank+1 AS Rank, ...
This works in MySQL, but I get an error when I paste that query into the report definition on SSRS.
Any ideas?
Rather than setting #rank=0 in a separate statement, try setting it in a subquery cross-joined to the main query - like so:
SELECT #rank:=#rank+1 AS Rank, ...
FROM (SELECT #rank:= 0) r
CROSS JOIN ...

Lookup function in SSRS report

I have one dataset Dataset1 and in that I am displaying data based on grouping. The data is like this
CityColumn CountColumn
City1 5
City2 3
The query of above datase is like this :
select count(*) as "CountColumn" from City group by CityColumn
Here in above dataset I have counted using grouping on CityColumn.
Now I have created another Dataset Dataset2 and in that The data is like this
CityColumn
City1
City2
City3
Now in dataset2 I have add one calculated field called TotalCount and used the Lookup Function the function is like this
=Lookup(CityColumn, CityColumn, CountColumn, "Dataset1")
but It gives me an error like
Lookup includes aggregate, rownumber, runningvalue, previous or lookup function. Aggregate, rownumber, runningvalue, previous or lookup function cannot be used in calculated field.
The first two values of the lookup function must refer to an identifying value in a column. In your case the City names must be in both datasets. Think of that as a primary key. The third value is the one you want to display from the second dataset. So it should look more like this:
=Lookup(Fields!CityColumn.Value, Fields!CityColumn.Value, Fields!CountColumn.Value, "Dataset1")
Make sure that Dataset1 has the column named CountColumn that you are trying to lookup. Keep in mind that this only looks up individual rows, not aggregates. If you want to work with aggregates you can do that on top of the lookup function.
EDIT:
Since Lookup functions are not allowed in calculated fields, you'll need to use it in the Value expression in your pie chart. It should look like this:
Note that the lookup function has to be in an aggregate like a sum function for it to work as a chart value.
Instead of adding it as a calculated field in the DataSet, simply add the expression into an empty column within the details rows of the report.