Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 923
Warren Avatar asked Oct 22 '22 08:10

Warren


1 Answers

Say the underlying DataSet (which I've called FruitDataSet) looks like this:

enter image description here

I've created a simple report based on this data:

enter image description here

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:

enter image description here

There are two things to note about the expression:

  1. 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.

  2. 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.

like image 68
Ian Preston Avatar answered Oct 24 '22 12:10

Ian Preston