I think you and I were talking about different years. I was not suggesting that you test 2002 dates--I was talking about 2003 dates. Let me clarify by adding in the years here:
"I think the datepart function works as intended. For 2003, where January 1, 2003 was a Wednesday, if you use:
datepart("ww",Date(2003,12,31),crMonday, crFirstFourDays)
...the result will correctly be 53. The formula counts the Monday of the first week containing four days in January as Week 1, and since there are five days in the week starting Monday, December 30, 2002, through Sunday, January 5, 2003 (January 1 - 5), it meets the criteria to be counted as week 1. The week starting Monday, December 29, 2003 thus becomes week 53. If you test the formula by substituting day 28 or 29, you will see that the count changes to 53 on December 29, 2003.
The formula ordinarly evaluates for the year the date is in. If you change the formula to:
datepart("ww",Date(2004,01,01),crMonday, crFirstFourDays)
The result will be 1, since it is week 1 of 2004, using these criteria, even though it is in the same week as 12/31/03, which correctly evaluates to 53.
You can see that using crFirstFourDays does have an effect by changing the first day of the week in the formula. The following:
datepart("ww",Date(2003,12,31),crThursday, crFirstFourDays)
...results in 52, because by using Thursday as the beginning day of the week, there are no longer four days in the first week of 2003 to meet the criteria. The first week, from Thursday, December 26, 2002, to January 1, 2003, contains only 1 day in January 2003, so January 2, 2003 becomes the first day of the first week.
The only "funny" result arises for the first week that doesn't "count" as the first week:
datepart("ww",Date(2003,01,01),crThursday, crFirstFourDays)
...would result in 52, which doesn't make sense as its weeknumber for 2003. A more accurate result would be 0--since it is being excluded from the current year. Theoretically, if you are using a weekcount, you might be excluding records outside of the acceptable range so this would not become a factor."
Let me also respond to your statements:
"In Germany, our calendar (2003) looks as follows:
2003/12/22 - 2003/12/28: week 52
2003/12/29 - 2004/01/04: week 1
(week starts on Monday).
So what I'd expect the formula to return if using it on any date between 2003/12/29 and 2004/01/04 would be 1.
In fact it shows 53 for all dates in this range except 2004/01/04. From this date on, it seems to work as I expect."
Datepart("ww",Date(2003,12,22), crMonday, crFirstFourDays) correctly evaluates to week 52, as it does if you substitute 12/28/2003.
Datepart("ww",Date(2003,12,29), crMonday, crFirstFourDays) correctly evaluates to 53, since it is evaluating the weeknumber in relation to the year of the date (2003), i.e., 12/29/2003 is week 53 of year 2003, which starts on Monday, 12/30/2002.
If you then use Datepart("ww",Date(2004,01,01), crMonday, crFirstFourDays), it will correctly evaluate to week 1, since it evaluating based on the year 2004.
I think the issue is that you want the function to operate differently than it does. This does not mean it is operating incorrectly. I don't think we know enough about your issue to develop a solution, so maybe you should provide more information. One possibility might be to always evaluate the weeknumber based on the weekending date, rather than for each day of the week. You could adjust each date to reflect the weekending date by using a formula like the following {@sunwkending}, which assumes Sunday is the end of the week:
{table.date} - dayofweek({table.date},crMonday) + 7
Then you could use this in your week number formula instead of {table.date}:
datepart("ww",{@sunwkending},crMonday, crFirstFourDays)
I think this would give you the weeknumbers you might be looking for.
-LB