Rolling Averages - back to the drawing board 
Author Message
 Rolling Averages - back to the drawing board

Hi everyone,

Thanks to those who have been trying to solve this problem for me, but
we are not much closer.

I may have to go back to the module idea after all, as the query is
really not producing the rolling average I am looking for, but only
doing some kind of weird averaging thing on the whole data (when I graph
it the result follows the line of the data-not a smoothed average).

This is a module we were playing with here, which we got to work once,
but never again: Perhaps someone can see what might be the problem: At
the moment I get a figure, but it is the same number in every box and
that number is way too high - it is totalling the whole table and
dividing it all by 12.

Function RollingAvgs(Periods As Integer, Month)
           Dim MyDB As Database, MyRST As Recordset, MySum As Double
           Dim temp As Double
           Dim tempmonth As Date

           Dim StartMonth As Date
           Dim month_count As Integer
           Set MyDB = CurrentDb()
           Set MyRST = MyDB.OpenRecordset("Table2")

           On Error Resume Next

           ' initialise the sum to zero
           MySum = 0
           ' initialise the month_counter to one
           month_count = 0

           StartMonth = Month

           ' check that the month value is within correct range
           If StartMonth < #5/1/97# Then RollingAvgs = Null: Exit
Function

            ' we are going to look at the index "Month"
            MyRST.Index = "Month"

           ' setup the record set to start where we want it to, by
searching for
           ' the startmonth supplied in the parameter list.
           MyRST.MoveFirst

          Do Until MyRST.EOF
          tempmonth = MyRST![Month]
          If tempmonth = StartMonth Then
            Exit Do
          End If
            MyRST.MoveNext
         Loop

           ' we want to loop until we either reach the end of the file,
or have counted
           ' the desired number of months
           Do Until MyRST.EOF Or MyRST.BOF Or month_count >= Periods

              temp = MyRST![downtime minutes]
              MySum = MySum + temp

              ' Increment Recordset to look at next record
              MyRST.MovePrevious

              ' decrement counter so that we dont count more than the
period requested
              month_count = month_count + 1
              Debug.Print month_count

           Loop

           ' now calculate the average using the sum and the number of
months counted

           RollingAvgs = MySum / month_count
           MyRST.Close
        End Function



Tue, 05 Feb 2002 03:00:00 GMT  
 Rolling Averages - back to the drawing board
Hi Miriam

Everything works just fine with me. I assumed that [month] has a
date-format, and that only one record a month exists.
As a calculated field in a query with this syntax:
    RA: RollingAvgs(4, [month])
it results (without altering a single thing in your function) a nice and
clear running avg in the output result, based on the current record and
(max) 3 previous records:

Month downtime minutes RA
01-Jan-99     50     50
01-Feb-99     25     37.5
01-Mar-99     35     36.6666666666667
01-Apr-99     45     38.75
01-May-99     55     40

If all boxes give the same result, you maybe hardcoded the month-parameter,
instead of using a field-value.
I've tried all kinds of stuff to find out why all data is being averaged,
but I can't find any reason for it.
How and where do you call your function?
Can you try again without your errorhandling-code, to see if you get some
erromessages?

Quote:

>Hi everyone,

>Thanks to those who have been trying to solve this problem for me, but
>we are not much closer.

>I may have to go back to the module idea after all, as the query is
>really not producing the rolling average I am looking for, but only
>doing some kind of weird averaging thing on the whole data (when I graph
>it the result follows the line of the data-not a smoothed average).

>This is a module we were playing with here, which we got to work once,
>but never again: Perhaps someone can see what might be the problem: At
>the moment I get a figure, but it is the same number in every box and
>that number is way too high - it is totalling the whole table and
>dividing it all by 12.



Tue, 05 Feb 2002 03:00:00 GMT  
 Rolling Averages - back to the drawing board
Hi Eric,

thanks for replying. I must be doing something wrong if you are getting it to
work! I have simplified everything right down - got rid of everything I don't
need. I only ever want a 12 month average anyway - I simply want to get 12
months rolling averages for the whole table. Should be easy.

I am still getting the same number in every box, which is the sum of the first
12 months values divided by 12.

In my query I have simply:  Expr1:RollingAvgs()

Yes, the Table1 is oragnised with one record per month. The value I am trying
to calculate is 'downtime'.

This is the simplified code I am working with:

 Function RollingAvgs()
         Dim MyDB As Database, MyRST As Recordset, MySum As Double
         Dim temp As Double
         Dim month_count As Integer

         Set MyDB = CurrentDb()
         Set MyRST = MyDB.OpenRecordset("Table1")

' initialise the sum to zero
         MySum = 0
         month_count = 0

' we want to loop until we either reach the end of the file, or have counted
' the desired number of months ie. 12
         MyRST.MoveFirst
         Do Until MyRST.EOF Or month_count = 12

' Copy downtime from table store as 'temp'
         temp = MyRST![Downtime]
         MySum = MySum + temp
         month_count = month_count + 1
         MyRST.MoveNext
         Loop

         RollingAvgs = MySum / month_count
         MyRST.Close

         End Function

Quote:

> Hi Miriam

> Everything works just fine with me. I assumed that [month] has a
> date-format, and that only one record a month exists.
> As a calculated field in a query with this syntax:
>     RA: RollingAvgs(4, [month])
> it results (without altering a single thing in your function) a nice and
> clear running avg in the output result, based on the current record and
> (max) 3 previous records:

> Month downtime minutes RA
> 01-Jan-99     50     50
> 01-Feb-99     25     37.5
> 01-Mar-99     35     36.6666666666667
> 01-Apr-99     45     38.75
> 01-May-99     55     40

> If all boxes give the same result, you maybe hardcoded the month-parameter,
> instead of using a field-value.
> I've tried all kinds of stuff to find out why all data is being averaged,
> but I can't find any reason for it.
> How and where do you call your function?
> Can you try again without your errorhandling-code, to see if you get some
> erromessages?


> >Hi everyone,

> >Thanks to those who have been trying to solve this problem for me, but
> >we are not much closer.

> >I may have to go back to the module idea after all, as the query is
> >really not producing the rolling average I am looking for, but only
> >doing some kind of weird averaging thing on the whole data (when I graph
> >it the result follows the line of the data-not a smoothed average).

> >This is a module we were playing with here, which we got to work once,
> >but never again: Perhaps someone can see what might be the problem: At
> >the moment I get a figure, but it is the same number in every box and
> >that number is way too high - it is totalling the whole table and
> >dividing it all by 12.



Tue, 19 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Rolling / moving averages

2. committing/rolling back all changes on form

3. Roll back transaction Error handling

4. Rolling back to vbx's afer upgrading to ocx's

5. RDO Problem when rolling back transaction - Function sequence error

6. Rolling back RDC changes

7. "rolling back" on AddNew

8. Implicit roll back when using server side cursor

9. Rolling back transactions

10. COM+ question: SetAbort not rolling back transaction?

11. MTS, VB and rolling back transactions

12. SetAbort does not roll back transaction

 

 
Powered by phpBB® Forum Software