Calculate percentage in reports designer

rbnsncrusoe
rbnsncrusoe Member Posts: 2
Hello!

I’m working with a report which shows total sales amount per country. It also includes a grand total of all sales at the bottom of the report.

The end user has requested that I include the percentage of total sales for each country. I am at a loss how to do this in Nav.

Obviously, I have to divide each country’s sales by the total sales, but the total sales are not calculated until the end of the report (in the footer). Is there something I can do to make it loop in the header, grab the total and then use that variable in the body? Have any other ideas, cause I'm at my wits end.

Thank you very much!
:D

Comments

  • kine
    kine Member Posts: 12,562
    As you know, you need to calculate the value for total before you will start the iteration. it means some new record variable which you will use to calculate the sum. And how it depends on the structure, if you are going in one level or you have some other nested levels, fitlers (for example if some records are skipped through code etc.). If there are just filters from user, you can copy the filters and use CALCSUMS to sum the fields but it can be more complicated in other cases.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Savatage
    Savatage Member Posts: 7,142
    The Aged Accounts Receivable does this - You can see how it's done there to get an idea

    CalcPercents(Total : Decimal;Amounts : ARRAY [4] OF Decimal)
    CLEAR(PercentString);
    IF Total <> 0 THEN
      FOR i := 1 TO 4 DO BEGIN
        Percent := Amounts[i] / Total * 100.0;
        IF STRLEN(FORMAT(ROUND(Percent))) + 4 > MAXSTRLEN(PercentString[1]) THEN
          PercentString[i] := PADSTR(PercentString[i],MAXSTRLEN(PercentString[i]),'*')
        ELSE BEGIN
          PercentString[i] := FORMAT(ROUND(Percent));
          j := STRPOS(PercentString[i],'.');
          IF j = 0 THEN
            PercentString[i] := PercentString[i] + '.00'
          ELSE IF j = STRLEN(PercentString[i]) - 1 THEN
            PercentString[i] := PercentString[i] + '0';
          PercentString[i] := PercentString[i] + '%';
        END;
      END;
    

    4 is for the current-30-60-90 - so I would assume you change that number to the number of countries you deal with.