Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Evaluating the Median of formula-derived values Post Reply Post New Topic
Author Message
rybad80
Newbie
Newbie
Avatar

Joined: 18 Oct 2010
Location: United States
Online Status: Offline
Posts: 7
Quote rybad80 Replybullet Topic: Evaluating the Median of formula-derived values
     Posted: 23 Dec 2011 at 6:25am

I'm looking to find the median value of a list of times (durations).  I am using the formula previous({endtime})-{starttime} to derive the {@duration}.  Then, I was looking to find the median value of the formula field called {@duration}.  Since this field is using the 'previous' condition, you cannot just summarize the field using median({@duration}) . 

I was thinking if I could build an array from the duration values and then evaluated that array, it could work.  I'm just not sure how to do that, nor can I find it after much searching on forums.
 
Any help would be greatly appreciated.
 
thanks!
IP IP Logged
rybad80
Newbie
Newbie
Avatar

Joined: 18 Oct 2010
Location: United States
Online Status: Offline
Posts: 7
Quote rybad80 Replybullet Posted: 23 Dec 2011 at 6:41am
Here is what I have so far
 
WhilePrintingRecords;
Numbervar Array u :=[{@turnovermins}];
numbervar array y := 0;
Numbervar Array Final;
Numbervar m;
NumberVar n;
numbervar p := 0;
NumberVar MedianValue := 0;
for m := 1 to ubound(u) do (
if u[m] <> 0 then (
p := p + 1;
redim preserve y

;
y

:= u[m]
));
//sorting
for p := 1 to ubound(y) do
  (
    redim preserve Final[ubound(y)];
    Final

:= Maximum(y);
    for n := 1 to ubound(y) do
      (
        if y[n] = Final

then
           (
             y[n] := 0;
             Exit For;
           );
      );
   );
MedianValue := Final[round(p/2,0)]; ;

 
 
 
Problem is, the array seems to be resetting on each row, so in essence, each row has an array with only 1 value in it.
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 27 Dec 2011 at 3:47am
redim is for setting the dimension(size of the array) and you are doing that every row.  Preserve just keeps the existing values.
 
with that said:
for m := 1 to ubound(u) do (
if u[m] <> 0 then (
p := p + 1;
redim preserve y;
y := u[m]
));
 I would think would be like:
for m := 1 to ubound(u) do (
if u[m] <> 0 then (
p := p + 1;
redim preserve y

;
y := u[m]
));

 redim preserve Final[ubound(y)];
//sorting
for p := 1 to ubound(y) do
...
 
as you only need to set the size of Final once
 
for sorting, wouldn't you want to arrange the values in Final in order?  I don't see that happening in the code.
 
HTH
HTH
 
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum