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.
Related
I have multiple tables with some rows and columns in sql.
Columns are like (Key, column1,column2,column3), I did unpivot tables in sql.
And got dataset in ssrs like : TableName, Key,ColumnName,ColumnValue.
I've created multiple matrixes like below structure for each table by setting filter = "TableName":
----------------------
Key | [ColumnName]
----------------------
[Key] | [ColumnValue]
------------------------
Question: How I can do sort (order by) based on column in SSRS matrix. I have as I said multiple matrixes. 1st matrix I want to sort by Column1, 2nd matrix by Column2, 3rd Matrix by Column2 and Column3 Where I need mention to sort by certain column name?
So far I've tried to sort my first Matrix.. tried it with "Row Group Properties ----> Sorting ---> Expression : SUM(IIF(ColumnName="Column1",ColumnValue,0)), but it didn't work. Also I've tried COUNT instead of SUM. No luck so far.
I solve it by this...
So if we want to get like "Order by Column1" then after unpivot Put your dataset in the matrixes. Go the the matrix which you want to Sort by specific column (in my case column1) Row group properties---->Sorting----> Add---> Expression:
MAX(
IIF(Fields!ColumnName.Value="Column1",Fields!ColumnValue.Value,Nothing)
)
and if you want to ad also Column2 to this matrix to sort, just after set 1st expression hit "Ok" and in the same window click --->add---> expression----> same query but with your column name (ex. Column2).
Hope this will help to some one.
I have an ssrs report that has 2 fields from 2 different datasets. In the pic below I have a count from a query for Total Bound Policies and another query for Total Bound Policies With At-Fault Endorsements and I'm trying to calculate a % value in another column and after googling for solutions I cannot find a solution using lookup or just a division calculation. Any help/direction would be appreciated. Thanks.
Here is my design layout:
Here is my last expression that is getting an error:
If I'm understanding the issue correctly, Total Bound Policies and Total Bound Policies With At-Fault Endorsements each contain a single result which is a count of each. I can think of a couple of solutions for this problem, the easiest one of which being adjusting the query to bring all of the data into a single query to return your counts AND a value for the percentage. It's almost always better to do as much of the calculations in SQL as possible.
DECLARE #a INT = (SELECT COUNT(*) FROM Table1)
DECLARE #b INT = (SELECT COUNT(*) FROM Table2)
SELECT #a AS Count1, #b AS Count2, CAST(#a AS DECIMAL) / CAST(#b AS DECIMAL) AS aPercent
As you can see, in SQL, I'm casting each count as a decimal, as division with INT values will return a whole number, meaning you'll have either 0% or 100%. Using some random tables from my database brings back these results when I run this query:
If this solution isn't a viable option or if I misunderstood the question, another option that could help is to declare each field with it's corresponding dataset. For example, (Fields!TotalBoundPolicies.Value, "TotalPoliciesBound") would get the field from dataset TotalPoliciesBound.
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.
Not sure how to do this so was hoping that someone could help, I have a multivalue parameter we shall call 'Week', this has a drop down of 1 through to 4. My data set example is :-
select total from tableA where Week in (#Week)
what I want to do is divide the total by the number of options I pick from the drop down, e.g. if I picked Week 1 & 2, I would want the TSQL statement to use the count of 2 as the value to divide by e.g.
select total/2 from tableA where Week in (#Week)
is this possible?
Thanks P
Have a look into using the Average function,
SELECT AVG(total) FROM tableA WHERE Week IN(#Week)
I would use a user-defined split function to convert the multivalue parameter into rows in a table. These have been posted several times. Then you can simply do something like this:
select sum(A.total), count(distinct Weeks.items)
from tableA as A
inner join dbo.Split(#week) as Weeks on Weeks.items = A.Weeks
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.