lbass, Thank you very much for your reply! I've learned a lot from many of your posts on this forum.
The updated version of the formula didn't quite work for me, but pointed me in a couple of useful directions. In my case, I was using a TopN n=20, so the distinctcounts don't match the groupnumbers very well. So my groupnumbers never get high enough to set the variables.
So my next stop was to ignore the distinctcounts and just set the groupnumber targets manually. As you programmed, the formula for median of an even number of values = Avg((N/2)+((N/2)+1)). So for a Top20 report, Median = (10th + 11th) values /2. (Think you meant to type (ave1 + ave2) in the last line in the group header). I ended up with this shortened version of your code:
whileprintingrecords;
numbervar medianx;
numbervar ave1;
numbervar ave2;
if groupnumber = 10 then ave1 := sum({Table.salesamt},{Table.salesperson});
if groupnumber = 11 then ave2 := sum({Table.salesamt},{Table.salesperson});
medianx := (ave1 + ave2)/2;
The code for the report footer was exactly as you posted:
whileprintingrecords;
numbervar medianx;
If I wasn't using a TopN report, I think your code would have worked with this minor tweaking:
whileprintingrecords;
numbervar medianx;
numbervar ave1;
numbervar ave2;
//This formula for median where sample size n is odd
if remainder(distinctcount({Table.salesperson}),2) <> 0 then
(if groupnumber = (((distinctcount({Table.salesperson})-1)/2)+1 then
medianx := sum({Table.salesamt},{Table.salesperson}));
//This formula for median where sample size n is even
if remainder(distinctcount({Table.salesperson}),2) = 0 then
(if groupnumber = distinctcount({Table.salesperson})/2 then ave1 := sum({Table.salesamt},{Table.salesperson}));
if groupnumber = (distinctcount({Table.salesperson})/2) + 1 then ave2 := sum({Table.salesamt},{Table.salesperson}));
medianx := ((ave1 + ave2)/2);
Ideally, what I'd like to do is base the median off the TopN value of N - but the only way I can figure out to do that is by using a parameter value {?TopN}. That way I can use the formula for median, mode, max, etc and save it to a repository so it works no matter what TopN value I pick... I can't seem to get it to work using a grand running total of gropunumber since the group running total changes on each pass. If anybody ever figures out a way to pass the N value of a TopN report directly into a formula, please let me know.
Thanks again LB...