Home RAP

Get percentaje of total

edited January 2010 in RAP

Hi,

In the following report:

Sold %
Banana 624 35.33%
Apple 323 18.29%
Melon 425 24.07%
Berry 394 22.31%
TOTAL 1766 100.00%


I want to calculate the percentaje of unit sold. I've read that it is not a
good idea to use DBCalc if you want to get the values of sum so I've get the
total with Variables:

Variable1: Units Sold
Variable2: Total Units Sold
Variable3: Percentaje of Units Sold

Units Sold: Variable1.OnCalc
- Value := Data['UnitsSold'];
- Variable2.Value := Variable2.Value + Data['UnitsSold'];

Column Sold is displayed correctly.

The problem comes to calculate the percentaje. I am using Variable3.OnCalc

- Value := 0;
- if Variable2.Value <> 0 then
- Value := Data['UnitsSold'] * 100 / Variable2.Value;

but with lookAhead Data['UnitsSold'] is always the last value (394) and
without lookAhead Variable2.Value is the cummulative sum instead of the
total sum. i.e.
624 Variable2.Value 624
323 Variable2.Value 947
425 Variable2.Value 1372

Is there a way to get the current data and the total in a Variable?
To complicate a bit more I would also like to use it with groups.

Fruits Sold %
Banana 624 35.33%
Apple 323 18.29%
Melon 425 24.07%
Berry 394 22.31%
TOTAL 1766 100.00%

Others 12 44.44%
Garlic 15 55.56%
Cinamon 27 100.00%

Thanks,

Jose Maria Sanmartin

Comments

  • edited January 2010
    Problem solved:

    I've seen an example where you use the following function within the report:

    function GetLookAheadTotal: Variant;
    var
    lsTotal: String;
    lsKey: String;
    liPos: Integer;
    begin

    {retrieve the look ahead total and convert from Text to Float value.
    note: the lookahead values are cached as Strings. The content of the
    string
    is exactly what is rendered to the page. Therefore it is a
    formatted value }
    lsKey := DBCalc1.GetLookAheadKey;
    lsTotal := DBCalc1.GetLookAheadValue(lsKey);

    {remove '$' from formatted value}
    liPos := Pos( '$', lsTotal);

    if liPos > 0 then
    Delete(lsTotal, liPos, 1);

    {remove ',' from formatted value}
    repeat
    liPos := Pos( ',', lsTotal);

    if liPos > 0 then
    Delete(lsTotal, liPos, 1);

    until (liPos = 0);
    Result := StrToFloat(lsTotal);
    end;

    Thanks,

    Jose Maria Sanmartin
This discussion has been closed.