How to properly set a SUM AGGREGATE on a group-level column - reporting-services

I have a SSRS report, and I'm trying to sum one of the columns.
The first column is a countdistinct(field1) and works perfectly.
The second column is in the same group as the first. When the first expands, the second column is part of the first group.
I'm trying to get the value here 24 in the group total.
if I perform a [SUM(CDEC(FIELD2.value))] it results in 72 because technically the 12 is repeated through all six records.
[SUM(MAX(FIELD2.value))] results in a 12, because the MAX() function gives max value of ALL the records.
How do I get 24, here?
Thanks
UPDATE #1.
here's the field setup for those columns
Someone mentioned using a scope in my sum(). How do I determine what my scope is? Thanks!

You have to add the scope in the sum expression in your tablix otherwise it always takes the dataset data. Let´s say you have a dataset (Dataset1), with the fields SalesOrder, SalesPrice. If you group now in your tablix by SalesOrder (GroupingBySalesOrder) you can add different scopes for the aggregate functions:
=Sum(Fields!SalesPrice.Value) 'This takes the Default scope "Dataset1"
=Sum(Fields!SalesPrice.Value, "Dataset1") 'The same result like above
=Sum(Fields!SalesPrice.Value, "GroupingBySalesOrder") 'The scope is now your grouping. Different result like the other two

Related

Expression to calculate % in a report

I am building a report(report server project) using the Data Tools. I have a column Quantity it has a Total.
I need another column that calculate the share(%) of each line in the Quantity comparing to the Total.
The expression would be: Line_1_Share = Quantity_of_line_1/Total.
I tried =[Sum(Total/Quantity)] but it does not even accept as a valid expression.
If you right-click the textboxes that contain your working 'Quantity' and 'Total' values and look at the expressions you will see the correct format.
For exmaple your 'Quantity' expression might be something like
=Fields!Quantity.Value
or if it is in a grouped row it might be
=SUM(Fields!Quantity.Value)
your 'Total' expression might also be
=SUM(Fields!Quantity.Value)
When you use SUM() (or any similar aggregate) then the scope of the expression decides what is included in the sum. The scope can be a single row, a row group or an entire dataset. If you do not specify a scope then the position of the textbox determines the scope.
So, if you have a simple table with no grouping other than the total line and your dataset name is dataset1 then your expression would need to be
=Fields!Quantity.Value / SUM(Fields!Quantity.Value, "dataset1")
The above reads .... "For the current row, take the Quantity and divide is but the sum of all Quantities that are within the entire dataset called dataset1"
If this does not help, post your current report design including and row and/or column groups.

SSRS - add percentage of total?

I am just starting out in SSRS and have a dataset that looks like the below.
I have built this in a matrix table like so
I want to show what percentage each of the rows total is of the grand total, so for the attached image I would want to show what percentage 35 (counselling calls) is of 47 (total) and what percentage Legal calls (12) is of total (47)
I have tried =Fields!Calls.Value/sum(Fields!Calls.Value) but that just gives me 100% for both?
Would appreciate any help
You need to specify the scope of your SUM() expressions.
I'll briefly explain how scopes work and then get to the answer.
SSRS always evaluates an expression based on it's scope which is usually defined by the physical location of the expression within table/matrix. As you can see from your design all 4 textboxes show the same expression, [SUM(Calls}] which in fact is actually =SUM(Fields!Calls.Value). However they give different results because the scope of each is different. The first is in the category rowgroup and the month column group for example, the second one is in the category row group but in the total month group, and so on...
OK, now to the answer !
Assumming
your rowgroup is called 'CategoryGroup` (you will see the name in the rowgroup panel under the main designer)
your dataset is called DataSet1
You expression should be
=SUM(Fields!Calls.Value, "CategoryGroup") / SUM(Fields!Calls.Value, "DataSet1")
This basically reads..
Take the sum of the call column for all rows that are within the current CategoryRowgroup and divide by the sum of the call column across the whole dataset.
Notes
The scope names must be enclosed in quote and are case sensitive
The scope names must match either a row or column group, or a dataset name exactly.
the , "CategoryGroup" scope argument can probably be omitted as this should be the scope of the textbox anyway.

How to Sum the aggregates of a child group

This should be easy, but I am stuck.
I have a table listing some figures about Qualifications - to achieve which a dataset that is essentially a row per Student is being grouped on Qualification with a Parent Grouping on "Measure" (which is just a bucket of qualifications).
One of the columns is trying to work out the number of students (well, more properly the number of students with a value in a particular field, weighted by another field) in each Measure/Qualification. In the screenshot below, it's the "Pred. Avg" column on the right hand side.
So for the Qualification Row Grouping, that column is calculated by:
=CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual") * Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP")
This works fine - the values of 35 and 11.5 in that rightmost column are correct for those rows. What the top row should be doing is simply adding up the values in the other rows to give me the number of students in this Measure, in this case to give 46.5. To do that the expression I am using is:
=Sum(CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual") * Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP"), "Measure")
However as you can see in the screenshot, this returns 2917 instead.
So my question is; Why doesn't that work, and given that it doesn't work how can I, within a parent group, aggregate the results of aggregates inside a child group?
EDIT:
OK so, I have determined that the following works correctly:
=Sum(CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual"), "Measure")
The problem there is that the Qual row that returns 11.5 is weighted to 0.5. I.E. it actually returns 23, and the Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP") is for that row returning 0.5 and altering it to 11.5...so the question becomes; "how do I force that ...*Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP") into the "Qual" scope, like the CountDistinct() is already in?
The issue here is that you're trying to aggregate values using that Lookup function which only returns one value. There are a couple ways you could go about doing this. One option would be to use the LookupSet function to get the applicable weightings. An even better option is to combine the data in your dataset so that the weighting is available without using a lookup. That way the function can recalculate an any grouping level without you having to force a scope on it. Also, CountDistinct ignores "Nothing" so you can do without the extra IIf statement. Hope that helps.

SSRS Report Builder IIF error with aggregate

I've been struggling with an IIF error when trying to create a variable aggregate.
I'm using Report Builder 3.0
I have a report where users determine what fields are in a report. These fields are passed as a multivalue parameter. I use a lookup from a different dataset to determine its placement in the report, and using the same order dataset to determine if the field is numeric or not (meaning I want to sum the value based on row-level grouping). The headers pull in using the same field lookup, which works fine. At the row level I'm trying to return either the sum of the field value based on row grouping, or if non-numeric, return the value. Based on similar posts I understand that both the true and false parts are processed. I attempted to offset this error by nesting another IIF. If I remove the sum function the data returns non-numeric data fine. However if I include the sum function numeric data is processed fine however non-numeric data returns #error.
What am I missing?
Here is a definition of the data I'm referencing below:
lookupvalue: returns the fields selected by the user based on predefined order in a stored procedure.
Fieldisnumeric: indicates if the field selected is numeric or not, 0 is false, 1 is true
Fielditem: the field item in the tablix being referenced
dtsselectedfields: the dataset I'm looking up the column order and numeric properties of a field.
The number 1 indicates the first position in the variable count of fields selected by the user. Additional fields are hidden based on the count of fields passed in the parameter and are incremental (e.g 1-n).
=iif(Lookup(1, Fields!LookupValue.Value,
Fields!FieldIsNumeric.Value,"dtsSelectedFields")=0,
Fields(Lookup(1, Fields!LookupValue.Value, Fields!FieldItem.Value,
"dtsSelectedFields")).Value,iif(Lookup(1, Fields!LookupValue.Value,
Fields!FieldIsNumeric.Value, "dtsSelectedFields")=0,
Fields!MV.Value,sum(Fields(Lookup(1, Fields!LookupValue.Value,
Fields!FieldItem.Value, "dtsSelectedFields")).Value)))
****edit 12/1/2015****
For additional clarity, I'm providing additional details. Below is 'dtsSelectedFields' dataset.
FieldItem_____LookupValue_____FieldIsNumeric
Item1Desc__________1__________________0
Item1Total__________2__________________1
Item2Desc__________3__________________0
Item2Total__________4__________________1
Let's say I have one column of data, and this column would first look for a LookupValue of 1. This would return the FieldItem, 'Item1Desc'. Because this field is not numeric, I would want to return the value of Item1Desc. However let's assume my first selection was actually 'Item1Total' and I don't want to return the non-numeric Item1Desc field. In this case, because 'FieldIsNumeric'=1 indicating a numeric field, I want to take the sum of this field.
Is it possible to nest an aggregating function in an IIF statement on only one part of an IIF statement? I.e. the true part or false part?
And if so, what am I doing wrong?
An example of the tablix:
sample layout
Column 1 Header____________Column 2 Header___________Column 3 Header
Column 1 Data______________Column 2 Data_____________Column 3 Data
Sample data
Product___________________Country of Origin_________________Units
ABC Envelopes___________________China______________________15
LMN Packets_____________________India_______________________30
In the example above, user selects 3 columns, 'Product', 'Country of Origin', and 'Units'. There are other fields available that would cause multiple rows if I grouped by them in the stored procedure (for example acquisition price). Based on the lookup I return the column description as a header. The row-level detail is described as above (e.g. Return the product name and country of origin, but sum up the units).
As a workaround for my issue above, I found an (ugly?) solution.
As mentioned above, a user can select any number of columns and the report organizes them in columns based on a predefined order according to a stored procedure. (E.g. a product description would come before the sum of the units if those two columns were chosen, but a product ID may come before the product description, but only if the ID was chosen.)
For every possible number of columns a user can select, I added two columns in the report (i.e. two for each field).
The first two columns will reference the lookupvalue=1. I then set the expression of each field in the detail to 1) a sum of the value, or 2) the value itself. I then set the column visibility to the results of the 'FieldIsNumeric' column. So the summed numeric column which would return an error for non-numeric data would be hidden when FieldIsNumeric=0, and the non-numeric column referencing the lookup value =1 would be shown, and vice versa.
Needless to say additional columns would follow the same logic in sets of two, each referencing the sequential lookup value (e.g. columns 3 and 4 would reference lookupvalue=2, columns 5 and 6 would reference lookupvalue=3, and so forth. Each column within the matching pair would be displayed or hidden based on the returned value of FieldIsNumeric in the same lookup dataset.
I'm definitely open to suggestions, but thought I'd post this as a workaround solution.

SSRS: Get values from a particular row of DataSet?

My dataset currently has 12 rows of data. Each representing data for a month. I would like to have variance of a column between to rows, the rows being last & last but one i.e., latest month and previous month's data.
It could have been simple if I were to work on tablix but thats not the case. I want those values for a textbox.
Any ideas on it anyone?
I hope you are using SSRS 2008R2:
R2 introduced the Lookup function that is perfect for this scenario.
=Lookup( Fields!ProductUID.Value ,Fields!ProductID.Value,Fields!Price.Value,"PriceDataSet")
The Lookup function above will evaluate the first parameter ("Fields!ProductUID.Value") in the current dataset, then look for a matching value in the field specified in the second parameter ("Fields!ProductID.Value") in the dataset specified in the fourth parameter. The value of the third parameter is then evaluated in that row of the dataset and returned.
A little convoluted, but very helpful.
In your case, you can use this in a textbox with a calculated a static number:
=Lookup(
Month(DateAdd(DateInterval.Month, -1, GetDate())),
Fields!MonthID.Value,
Fields!Name.Value,
"DataSet1")
This should calculate a number for last month, then look for a match in DataSet1.
In this example I have a tablix with Statecode and name as below
enter image description here
Suppose you want to display the name of state of CA, write an expression as -
=Lookup(
"CA" ,
Fields!StateCode.Value,
Fields!StateName.Value,
"ReportData"
)
This will return 'California' in the text box
I ran across this post while trying to solve a similar problem but with columns of double data type. Not sure why but SSRS did not want to return my first row using LOOKUP in combination with ROW_NUMBER in SQL(If someone can solve that all the better). I ended up using a SUM(IIF) instead. Hopefully, this is useful for someone else.
=Sum(IIF(Fields!RowNum.Value=1,CDBL(Fields!MyNumericColumn.Value),CDBL(0)))
Note: If SSRS complains about data types, just cast both parts of the IIF to the desired data type.