Search code examples
jasper-reports

Jasper Sum isn't including the last value


I am using Jaspersoft Studio 5.6.2 and have a report with a column showing elapsed days between two dates.

I want to show at least one day if the date range is less than 24 hours so I created a variable to convert the field value to indicate that. I also want to avoid negative values:

Name: day_rounded
Class: java.lang.Integer
Calculation: No Calculation
Expression: $F{days}==0?1:$F{days}<0?0:$F{days}
Increment: None
Reset: Report

Here is the variable I created to total them:

Name: totals
Class: java.lang.Integer
Calculation: Sum
Expression: $V{day_rounded}
Increment: None
Reset: Report

$F{days} is the difference between two dates from the query.

...
DATEDIFF(dd,irr.xxxstart_dt, ISNULL(irr.xxxend_dt,$P{end_dt})) AS days
...

I ran the report with the $F{days} value alongside the $V{day_rounded} value and they look OK.

When i put the $V{totals} into the Summary band (it is in the Page Header as well) the value does not include the one on the last row. For instance if I use a calculator and sum up all the days listed I get 1494. The report shows 1485 and the days value for the last row is 9 - so I can see it is not including that one. I ran the report for other date ranges and the total always is exactly the value in the last row less than it should be.

As far as I can tell the variables are declared correctly. Any idea why I am missing the final row value in the sum?


Solution

  • Here is the jrxml I used to solve this:

        <variable name="page_total" class="java.lang.Integer" resetType="Page" calculation="Sum">
        <variableExpression><![CDATA[$V{day_rounded}]]></variableExpression>
    </variable>
    <variable name="paged_summed" class="java.lang.Integer" incrementType="Page" calculation="Sum">
        <variableExpression><![CDATA[$V{page_total}]]></variableExpression>
    </variable>
    

    I totaled my days values per page and had the RESET set to 'Page' for that variable. I summed them up and set the INCREMENT to 'Page for the other variable.

    Now my totals are all adding up. Thank you @tobi6!!