Getting cumulative sum for rows (groups)
<p>Imagine the following data set:</p>

<p><a href="https://i.stack.imgur.com/oYjSV.png" rel="nofollow"><img src="https://i.stack.imgur.com/oYjSV.png" alt="Cumulative data set"></a></p>

<p>What's the proper way to get the values of <em><code>TOTAL_CUMULATIVE</code></em> in JasperReports?
i.e: in <em>"VW"</em> row it should sum all values (<code>COST - EXPENSE</code>) until <em>"VW"</em>, which means: (500 - 150) + (400-200) = 550.</p>

<p>column xml code:</p>

<pre><code>&lt;textField isStretchWithOverflow="true" pattern="#,##0.00" isBlankWhenNull="true"&gt;
        &lt;reportElement x="746" y="0" width="65" height="13"/&gt;
        &lt;box topPadding="1" leftPadding="1" bottomPadding="1" rightPadding="1"/&gt;
        &lt;textElement textAlignment="Right" verticalAlignment="Middle"/&gt;
        &lt;textFieldExpression&gt;&lt;![CDATA[$V{BALANCE_BY_ACCOUNTING}]]&gt;&lt;/textFieldExpression&gt;
&lt;/textField&gt;
</code></pre>

<p>The variable:</p>

<pre><code>&lt;variable name="BALANCE_BY_ACCOUNTING" class="java.lang.Double" resetType="Group" resetGroup="groupByAccounting" calculation="Sum"&gt;
    &lt;variableExpression&gt;&lt;![CDATA[$F{cost} - $F{expense}]]&gt;&lt;/variableExpression&gt;
&lt;/variable&gt;
</code></pre>

<p>In the <em>"VW"</em> row, the code sets <code>TOTAL_CUMULATIVE</code> column value to: 200. It should not. It should set to the cumulative sum 550. The same applies to the other rows. In <em>"MERCEDES"</em> row it sets value 600 (should be 11500) and so on.</p>