Percent of Group, not total - sql

It seems like there are a lot of answers out there but I can't seem to relate it to my specific issue. I want to get the breakdown of yes/no for the specific Group. Not get the percent of the yes for the entire population of data.
I have tried the following code in the "What I'm Getting" % of Total cell =
=FormatPercent(Count(Fields!SessionID.Value)/Count((Fields!SessionID.Value), "Tablix1"),)
=FormatPercent(Count(Fields!Value.Value)/Count((Fields!SessionID.Value), "Value"),)

It should just be a case of changing the Scope in your expression to make sure the denominator is the total for the group, not the entire Dataset or Tablix, i.e. something like:
=Count(Fields!SessionID.Value) / Count(Fields!SessionID.Value, "MyGroup")
Where MyGroup is the name of the group, i.e. something like:
If this is still not clear, your best option would be to add a few sample rows, and your desired result for these, to the question so we can replicate your exact issue.
Edit after more info added
Thanks for adding more details. I have created a Dataset based on your example:
And I've created a table based on this:
The group is based on the Group field:
The Group % expression is:
=Fields!YesNoCount.Value / Sum(Fields!YesNoCount.Value, "MyGroup")
This is taking the YesNoCount value of each row and comparing it to the total YesNoCount value in that particular group (i.e. the MyGroup scope).
Note that I'm using Sum here, not Count as in your example expression - that seems to be the appropriate aggregate for your data and the required value.
Results look OK to me:

Related

Summing the value of the aggregate function Last()

enter image description hereI am trying to design a report in ssrs that returns the last opening stock for each product in the dataset. To achieve this I used
=Last(Fields!CustProdAdj_new_openingstockValue.Value).
This works fine. But where I encountered a problem is in getting the sum of all the opening stock for each product. I tried using
=sum(Last(Fields!CustProdAdj_new_openingstockValue.Value))
but I got the error message
[Error on Preview]
Please is there another way to go about this
I have tried using aggregate(), runningValue(), to no avail
This is the dataset
This is the report layout
On previewing having used max()
This is probably easier to do directly in the dataset query but assuming you cannot change that, then this should work...
This assumes your data is ordered by the CustProdAdj_createdon column and that this is a date, datetime or some other ordered value, change this bit if required.
=SUM(
IIF(Fields!CustProdAdj_createdon.Value = Max(Fields!CustProdAdj_createdon.Value, "MyRowGroupNameHere"),
CustProdAdj_new_openingstockValue,
0)
)
Change the MyRowGroupNameHere to be the name of the rowgroup spelled exactly as it is in the rowgroup panel below the main design panel. Case sensitive and include quotes.
What this does is, for each row within the rowgroup "MyRowGroupNameHere", compare the CustProdAdj_createdon date to the max CustProdAdj_createdon across the rowgroup. . If it is the same then return the CustProdAdj_new_openingstockValue else return 0.
This will return the value required on only 1 record within the group.
For example, if you had 1 row per day then only on the last day of the month would a value be returned other than 0 because only the date of the last record would match the maximum date within the group.
Then it simply sums the results of this up.

SSRS Divide Sums from Different Scopes Returns 0

I have a grouped dataset. From the parent group down, the group names are:
Company > Plant > Details. In the end, I want to be able to take the sales of each plant and see what percentage it is of the entire company's sales. Let's say the company has a total sales of $500 and the sales of "Plant A" were $100 and "Plant B" sales were $400. I would image that I would need an expression at the Plant grouping level that was like:
=Sum(Sales)/Sum(Sales, "Company")
And I would get .2 for Plant A and .8 for Plant B. But if I do that, I get 0. I am at a complete loss. Any help with this would be greatly appreciated.
Additional information:
My exact setup is a little more complex than the example I gave below, but I believe the general idea still holds the same. I have a total of 6 groups:
The circled group is the equivalent of the "Plant" Group in my example. Here is the row group in my tablix for the GroupBy group (the one with the arrow pointing to it).
The expression that is circled in the picture above is the expression in question to get my percentage (right now really just a decimal, not formatted to be a percentage yet).
=sum(Fields!ActualCurrent.Value)/sum(Fields!ActualCurrent.Value, "Company")
Fields!ActualCurrent.Value is the equivalent of "Sales" in my example above. The expression above returns 0 for all groups. But yet, if I change it to
=sum(Fields!ActualCurrent.Value)+sum(Fields!ActualCurrent.Value, "Company")
It will produce the equivalent of $600 for "Plant A" and $900 for "Plant B."
I can't seem to find how it reacts as expected when adding the two sums, but produces 0 when I divide them.
It would be useful to see where are you using that expression to determine what is wrong here, but I think you can use this guide to get your desired result.
Create a tablix like this:
Note I've added Company and Plant fields as groups. Also I've deleted details group. Right click details and select Delete group and set Delete group only option.
Now in the percentage column use the following expression:
=FORMAT(
SUM(Fields!Sales.Value,"Plant")/SUM(Fields!Sales.Value,"Company")
,"P2"
)
The sum of every plan divided by the sum of the whole company group. It is not necessary but I am using FORMAT function to format the float value returned by the expression to percentage format using two decimal places.
It should show something like this:
UPDATE: Try scoping the sum to your specific group: GroupBy
=sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company")
UPDATE 2: Format the cell to show decimal digits.
Use thiss expression:
=FORMAT(
sum(Fields!ActualCurrent.Value, "GroupBy")/sum(Fields!ActualCurrent.Value, "Company"),
"F2"
)
It will format the value returned by the expression as a float with two decimal digits.
If you want to show the value in percentage format replace F2 in the expression for P2 (Percentage format with two decimal digits.)
Let me know if this helps.

Interactive sorting based on totals

I have the following report that looks like this in design mode:
The field I'd like to sort on is the <<Expr>> field highlighted in yellow TOTAL row. That expression reads like this:
=ReportItems!Textbox12.Value+ReportItems!Price3.Value
When previewed, the report looks like this:
I'd like to be able to sort the TOTAL row for all of the report. My desired result would have the group with the $38.52 total first, $0.92 total second, and so on. Is this possible?
I would set the Sorting on the Row Group for Patient Name. Sort expressions cannot reference ReportItems so I would get the Expressions inside those Report Items references and copy them into the Sort Expression.
And that is seriously the worst color scheme I have ever seen in 30+ years in IT ... it literally makes my eyes water.

SQL Report Builder: get value from group within tablix

Please forgive the vague title of this question. Perhaps the below will ask my question better.
Consider the below aggregated table:
Fruit Units FruitSales%
----- ----- -----------
Apples 10 ?
Oranges 20 ?
Bananas 10 ?
NonFruit 10 ?
TOTAL 50 ?
I need the FruitSales% column to be: Fruit / (Total - NonFruit)
If NonFruit is a product name of its own, how do I get its value for use in other calculations in the tablix?
I imagine my formula for the FruitSales% is something like:
Sum(Fields!Units.Value) / (ReportItems!txtTotalUnits.Value - SumIf(Fields!Fruit = "NonFruit", Fields!Units.Value)
However, SumIf does not exist and even if it did, it would be specific to the current row.
And while I'm here, ReportItems!txtTotalUnits.Value, I have obviously named that text box, but is there a cleaner way to reference it?
Say the underlying DataSet (which I've called FruitDataSet) looks like this:
I've created a simple report based on this data:
The Fruit Sales % expression is:
=Sum(IIf(Fields!fruit.Value <> "NonFruit", Fields!units.Value, 0))
/ Sum(IIf(Fields!fruit.Value <> "NonFruit", Fields!units.Value, 0), "FruitDataSet")
This gives what I think is the correct results:
There are two things to note about the expression:
By running the Sum against an IIf expression, you can control what gets included in the totals - here I'm setting NonFruit explicitly to 0.
By setting the Scope of the aggregate expression, you can get overall totals to use to work out total percentages - in the report I'm getting a total using FruitDataSet and comparing this to the group-level total to get a % value.
The way you're referencing the textbox total is fine; the only other option would be to use an expression each time you want the total - if this is outside a Tablix you would need to explicitly set the Scope, e.g. the DataSet.

Sum of distinct values in field SSRS 2005

I'm working on SSRS report builder that is using a dataset calling a SQL Server 2000 database.
The query is getting sums of a few different fields and is also pulling out all records that have to do with that client number. I want to get the sum of the sum but it is way over because of the detail rows. Basically what I want is the sum of the distinct sum column values.
=Sum(Fields!tot.Value, "table1_Group3")
I saw that you can get sums by the groups and I tried the expression above but it comes back with an error:
The Value expression for the textbox 'tot' has a scope parameter that is not
valid for an aggregate function...
table1_Group3 is the name of the group that holds the sum value in the report.
Any suggestions on how to get the distinct values to sum them in this report.
=Sum(Fields!tot.Value, "table1_Group3")
The code above will give you the sum of "tot" for all rows in the current "table1_Group3." This means that this expression only makes sense somewhere within table1_Group3. Otherwise, SSRS doesn't know which is the current instance of that group.
Sounds like you would like to sum this value across multiple groups, but only take one "tot" from each instance of the group. (Are you sure that all rows in that group will have the same "Tot?")
If tot is the total of other fields in your returned data, then simply add those up in your formula. This may have the added benefit of simplifying your SQL query as well.
Some other options that could work:
- Change your SQL query so that only one row per group gets the Tot field set.
- Use Embedded code in the report to keep a running total which is added to only once per group, such as in the group header.
(If upgrading to 2008R2 SSRS is an option, then the Lookup function could be used here, maybe even to look back at the same dataset.)
change the query/ dataset to sum(distinct tot) using the temp table on the sql server
I suppose you need to write sum(distinct columnName).