I still think my idea should have worked, but as long as you found a solution.

> I say clunky, because everytime I want to change the

> TopN value I have to get busy changing a lot of formula:

Is "change the TopN value" referring to how many of the top values you want to add together, or maybe just how many groups are in the report? Either way, it doesn't have to be a problem (up to a point).

Your first formula uses Redim to set the size of the array to 4, and that is never changed in the formulas that you posted. If your report has more than 4 groups, you'll get a subscript error. Maybe you're doing something that will never have more than 4 groups on a report, like the 4 quarters in a year. Otherwise, you should allow for more groups.

One way to handle that would be to simply increase the number in the Redim to the maximum number of groups that you think you'll ever have on a report. A more dynamic option is to increase the size of the array as necessary. To do that, just change your second formula as follows:

//array evaluate in group footer

WhilePrintingRecords;

numberVar array RT;

if GroupNumber > Count (RT) then

Redim Preserve RT [ GroupNumber ];

RT[GroupNumber] := tonumber ({#period gross});

That just uses Redim Preserve to expand RT when necessary. The Preserve option keeps the current contents (all of the values stored up to that point).

If the number of groups on the report was a concern, this should take care of it, up to a point. The limitation here is that an array can only have 1000 elements, so if you ever have a report with more than 1000 groups, that will be a problem. But since the limit using your current formulas is 4, I'm guessing that a limit of 1000 probably won't be a problem. :-)

If you're concerned about changing the formulas to add together something other than the top 3 values, that is something that you would have to deal with, but you can simplify it. As it is now, the final formula sorts the values in the array from lowest to highest and adds together the final 3 values, and it only works if there are exactly 4 groups on the report (Which brings us back to that earlier question, does your report _always_ have 4 groups on it?). You can simplify that a bit by sorting the values from highest to lowest and adding together the first 3 values. That way it doesn't matter how many elements are in the array (ie. how many groups are on the report). Just add together the first N elements to get your total. That's arguably a little simpler than adding together the last N.

To sort the array from highest to lowest, just change this line in the final formula

if RT[ counter2] > RT[ counter2 + 1] then

to this

if RT[ counter2] < RT[ counter2 + 1] then

IOW, change the > to <.

Then change the last line to:

add := RT[1]+RT[2]+RT[3]

FWIW, it's a very minor thing, but the add variable in that formula serves no real purpose. You could just forget it and the final line could be:

RT[2]+RT[3]+RT[4] // Your original formula

RT[1]+RT[2]+RT[3] // My version

Also, when you referred to "changing a lot of formula", are you perhaps using formulas like the ones you posted to add together the topN values for more than one field, and you were talking about changing the formulas for all of those fields if you wanted to change N?

If so, you could simplify that by putting the N in a formula and using that formula in the formulas that add together the values. For example, create a formula named topN that just says

3

Another option would be to put the 3 in a variable in the formula in your report header. Either way, you'd then use that formula/variable in your final formula. How you use it will depend on how your array is sorted. If you sort it in descending order, as I suggested, then you want to add together the first N values and you could just replace the final line of the final formula with this:

if {@topN} < Array_Size then

Redim Preserve RT [ {@topN} ];

Sum (RT)

That just says that if the array has more elements than you want (eg. it has 4 elements and you only want the top 3), then use Redim Preserve to make the array smaller, discarding the unwanted values. If the array actually has fewer elements than you want (eg. it has 3 elements and you want the top 4), it doesn't bother with the Redim. No point in adding more elements that will just be 0.

Then it just uses Sum to add together what's left in the array.

Actually, even if you don't use the topN formula/variable idea, this could be a good way to handle changing the number of values to add. The last lines could be:

if 3 < Array_Size then

Redim Preserve RT [ 3 ];

Sum (RT)

If you want to change how many values are added, just change those two 3's to the new value.

OK, I guess that should give you enough to chew on. :-)

James