D
Doc
I need a 7 day rolling window average for each date shown on a query, for
example:
Date range 10/1/2008 - 10/31/2008
Fields:
Daily Average
MTD Average
Previous Month Average
12 Month Average
7 day rolling window average
Explination:
Daily average comes from the primary table, just the total line average
MTD avg comes from a second query, linked in
PrevMo avg comes form a second query, linked in
12Mo avg comes from a second query, linked in
Problem:
The 7 day average is supposed to show the last 7 days average for each date.
For example, if the report was ran on 10/23/2008, a single line for every day
for the month of october would be shown, Daily average would flucuate for
each day, the mtd, prevmo, and 12mo averages would all be the same, but the 7
day rolling window should show the average as such; so that the line for the
23rd, would show an average for 10/17-10/23, likewise the line for the 22nd,
would show an average for 10/16-10/22.
I have accomplished this task with a simple program shown below, but the
query takes a couple of hours to run, I need it in a couple of minutes, max.
Any help would be AWESOME! Thanks!
Function Scrap7DayAverage(ByVal IDMachine As Integer, ByVal Dt As Date) As
Single
On Error GoTo ErrHand
Dim ScrapPerc(7) As Single, I As Integer
For I = 0 To 6
If ECount("*", "qryScrapPercDailyCurMo", "IDMachine = " & IDMachine
& " And Date = #" & Dt - I & "#") = 0 Then
ScrapPerc(I) = 0
Else
ScrapPerc(I) = ELookUp("ScrapPerc", "qryScrapPercDailyCurMo",
"IDMachine = " & IDMachine & " And Date = #" & Dt - I & "#")
End If
Next I
ScrapPerc(7) = ScrapPerc(0) + ScrapPerc(1) + ScrapPerc(2) + ScrapPerc(3)
+ ScrapPerc(4) + ScrapPerc(5) + ScrapPerc(6)
ScrapPerc(7) = ScrapPerc(7) / 7
Scrap7DayAverage = ScrapPerc(7)
ExitHand:
For I = 0 To 7
ScrapPerc(I) = 0
Next I
I = 0
IDMachine = 0
Dt = 0
Exit Function
ErrHand:
Echo True
DoCmd.SetWarnings True
eNumb = Err.Number
eDesc = Err.Description
ErrorLog "Production", "Scrap7DayAverage", "ePr-305", ActiveFrm, eNumb,
eDesc
eNumb = 0
eDesc = ""
GoTo ExitHand
End Function
example:
Date range 10/1/2008 - 10/31/2008
Fields:
Daily Average
MTD Average
Previous Month Average
12 Month Average
7 day rolling window average
Explination:
Daily average comes from the primary table, just the total line average
MTD avg comes from a second query, linked in
PrevMo avg comes form a second query, linked in
12Mo avg comes from a second query, linked in
Problem:
The 7 day average is supposed to show the last 7 days average for each date.
For example, if the report was ran on 10/23/2008, a single line for every day
for the month of october would be shown, Daily average would flucuate for
each day, the mtd, prevmo, and 12mo averages would all be the same, but the 7
day rolling window should show the average as such; so that the line for the
23rd, would show an average for 10/17-10/23, likewise the line for the 22nd,
would show an average for 10/16-10/22.
I have accomplished this task with a simple program shown below, but the
query takes a couple of hours to run, I need it in a couple of minutes, max.
Any help would be AWESOME! Thanks!
Function Scrap7DayAverage(ByVal IDMachine As Integer, ByVal Dt As Date) As
Single
On Error GoTo ErrHand
Dim ScrapPerc(7) As Single, I As Integer
For I = 0 To 6
If ECount("*", "qryScrapPercDailyCurMo", "IDMachine = " & IDMachine
& " And Date = #" & Dt - I & "#") = 0 Then
ScrapPerc(I) = 0
Else
ScrapPerc(I) = ELookUp("ScrapPerc", "qryScrapPercDailyCurMo",
"IDMachine = " & IDMachine & " And Date = #" & Dt - I & "#")
End If
Next I
ScrapPerc(7) = ScrapPerc(0) + ScrapPerc(1) + ScrapPerc(2) + ScrapPerc(3)
+ ScrapPerc(4) + ScrapPerc(5) + ScrapPerc(6)
ScrapPerc(7) = ScrapPerc(7) / 7
Scrap7DayAverage = ScrapPerc(7)
ExitHand:
For I = 0 To 7
ScrapPerc(I) = 0
Next I
I = 0
IDMachine = 0
Dt = 0
Exit Function
ErrHand:
Echo True
DoCmd.SetWarnings True
eNumb = Err.Number
eDesc = Err.Description
ErrorLog "Production", "Scrap7DayAverage", "ePr-305", ActiveFrm, eNumb,
eDesc
eNumb = 0
eDesc = ""
GoTo ExitHand
End Function