title: SSRS subtotalling columns with expressions
body: <p>Using Visual Studio 2010 and SQL Server 2012</p>
  
  <p>I have made an SSRS report that requires a custom method to subtotal and arrive at a grand total for Percent of Assets and other calculations derived from Percent of Assets. I cannot simply use the Add Total Function because it is grayed out as is typical when summing expressions. I cannot manually calculate the subtotals because I get a nested aggregate error.</p>
  
  <p>The software I'm using to make this SSRS report provides various stored procedures and functions to make reports using derived computations. The stored procedure contain a Percent of Assets column but because the report is filtered to show only common stock, the percentage of Assets function is incorrectly still counting unfiltered assets in its calculation. The assets under consideration make up 59.7 % of total holdings but I need the report to behave as if they were 100% of holdings.</p>
  
  <p>To understand some of the math in this report please look at the following report output. Note that the report I want only includes common stock which make up <strong>59.7</strong> Percent of total assets as mentioned.</p>
  
  <p><img src="https://i.imgur.com/hmCFJAO.jpg" alt="Appraisal"></p>
  
  <p>This report represents the desired output which is currently being calculated in a different reporting program. The report has two tables</p>
  
  <p><img src="https://i.imgur.com/Mo1O7AT.jpg" alt="intlexample"></p>
  
  <p>% Company Sales is a hard coded value that represents the percent of sales that are either domestic or international. You see this and % Portfolio Assets in both the international and domestic tables.</p>
  
  <p>% Portfolio Assets is calculated by multiplying the percent of assets of a given holding by the hard coded company sales value. In the first example McDonald's 2.1% Portfolio Assets is 6.6% * 32%. </p>
  
  <p>6.6% is the percent of Assets McDonald's represents in the portfolio is if you only count common stock (exclude cash)</p>
  
  <pre><code>McDonald's Value     Total Portfolio Value       %
   4,950.00         /   73,919.50    =       6.6000%
  </code></pre>
  
  <p>Domestic/ International Percentage breakdown</p>
  
  <pre><code>Total % Sales       %Split     Region
  6.60%   32%        2.1%       Domestic
  6.60%   68%        4.5%       International
  </code></pre>
  
  <p>Now here is my report output. It differs slightly from the design layout I will be showing because I added columns and changed the headers for explanatory purposes in this preview example. So far I am just trying to get the Domestic table to work. Once that is working I will build the international table. I have a few extra colums that I'm using for troubleshooting.</p>
  
  <p><img src="https://i.imgur.com/HWDGkRj.jpg" alt="SSRS1"></p>
  
  <p>I have tried several methods to Calculate Portfolio Assets. I can get the individual row values to be correct but I cannot get the subtotal value to be correct using different methods due to a nested aggregate error (summing and already summed expression when a data scope is used) The grand total is also important but for now I'm just trying to get subtotal working.</p>
  
  <p>Here is one method to get the row detail value for Percent of Assets. This is used in the calculated Percent Column. This column is the one that won't allow me to subtotal without a nested aggregate error.</p>
  
  <p><img src="https://i.imgur.com/uLrycgQ.jpg" alt="SSRS1"></p>
  
  <p>When I try to use the following expression to get the subtotal I get the nested aggregate error </p>
  
  <p>=<strong>SUM</strong>(Fields!MarketValue.Value/<strong>Sum</strong>(Fields!MarketValue.Value, "DataSet1"))</p>
  
  <pre><code>ERROR Error 2   [rsInvalidNestedDataSetAggregate] The Value expression for the text box ‘Textbox39’ has a nested aggregate that specifies a dataset scope.  Inner aggregates cannot specify a dataset scope.
  </code></pre>
  
  <p>If use the PercentAssets field which is included in the Stored Procedure I can subtotal and get grand total without errors but the value is the filtered value which only sums to 59.7 if I multiply this filtered percent by the company sales the value is wrong. </p>
  
  <p><img src="https://i.imgur.com/3USi5Ju.jpg" alt="SSRS1"></p>
  
  <p>Last I tried to use custom VBA code to get my subtotal and grand totals. However this provides a running total of the calculated percents not the subtotals I need for the SectorName groups.</p>
  
  <p>Here is the VBA Code I used which I found on the web here:
  <a href="http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1d3c5ec1-89cb-4108-8637-ff434027b1ec/sum-an-expression-value-ssrs" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1d3c5ec1-89cb-4108-8637-ff434027b1ec/sum-an-expression-value-ssrs</a></p>
  
  <pre><code>Dim public nettotal as Double
      Public Function Getvalue (ByVal subtotal AS Double) AS Double
                  nettotal = nettotal+ subtotal
                  return subtotal
      End Function 
  Public Function Totalvalue()
                  return nettotal
  End Function
  </code></pre>
  
  <p>Code deployed
  <img src="https://i.imgur.com/wzKU6zR.jpg" alt="codedep"></p>
  
  <p>In summary I need to multiply the calculated Percent column's values by the Company sales. I need to subtotal the result of these calculations.</p>
  
  <p>If you look in the Information Technology Sector Grouping you can see the original report correctly arrives at 32.8 as the subtotal.</p>
  
  <p>My report incorrectly arrives at 100% because that is the running total tally of the Calculated percents.</p>
  
  <p>Here is a link to the original rdl</p>
  
  <p><a href="https://dl.dropboxusercontent.com/u/87501202/InternationalDomestic.rdl" rel="nofollow noreferrer">https://dl.dropboxusercontent.com/u/87501202/InternationalDomestic.rdl</a></p>