I am writing code to determine the average sales over a 12-month period, with one field per month (derived via crosstab). However, some items were only made available for sale at some point inside the 12-month period, and thus we only have a few months of sales data for those items. We don't want to average all 12 months' sales, including zeros, but would instead need to start in the month following the item's creation date. My array consists of the 12 sales months and the last is the item's creation date.
My function in the query is called like this:
Code so far follows:
At this point, I have determined the name of the period (the field name, essentially) at which to start the average. I have the 12 periods' sales data, but need to know the syntax of how to determine the name of the field for comparison.
In other words, if the create date for an item was 200905, then I will need to average the sales in 200906, 200907, 200908, 200909, and 200910. 5 periods instead of all 12. I'm not sure how to determine which I in FieldArray(I) to begin my For/Next loop that will sum the values in.
(I know how to determine the average once the start month is determined, but I feel like I'm missing a code word for the field name. I also know that there's a possibility that I will have to make changes to the month if it is less than 10 -- add in a zero, etc, and that shouldn't be a problem.)
Many thanks!
My function in the query is called like this:
Code:
Average Monthly Demand: AvgDem([200811],[200812],[200901],[200902],[200903],[200904],[200905],[200906],[200907],[200908],[200909],[200910],[creatdt])
Code so far follows:
Code:
last = UBound(FieldArray)
creation = FieldArray(last)
creatmonth = Right(creation, 2)
creatyear = Left(creation, 4)
'Determine the month following the item creation
Select Case creatmonth
Case 12
newmonth = 1
newyear = creatyear + 1
Case Else
newmonth = creatmonth + 1
newyear = newyear
End Select
startper = newyear & newmonth
At this point, I have determined the name of the period (the field name, essentially) at which to start the average. I have the 12 periods' sales data, but need to know the syntax of how to determine the name of the field for comparison.
In other words, if the create date for an item was 200905, then I will need to average the sales in 200906, 200907, 200908, 200909, and 200910. 5 periods instead of all 12. I'm not sure how to determine which I in FieldArray(I) to begin my For/Next loop that will sum the values in.
(I know how to determine the average once the start month is determined, but I feel like I'm missing a code word for the field name. I also know that there's a possibility that I will have to make changes to the month if it is less than 10 -- add in a zero, etc, and that shouldn't be a problem.)
Many thanks!