Hello,
I thought I would test this, and it works fine

I made a couple of smaller edits. Here is the code for 1 date, output in the TM1 server log:
Code: Select all
pDate = '20241231';
pDateFormat = 'yyyyMMdd';
ndf = NewDateFormatter( 'nl_BE', 'Etc/UTC', 'serial', 'date' );
# parse the date parameter
nDate = ParseDate( pDate, pDateFormat, ndf );
sYear = FormatDate( nDate, 'y' );
sYear_Next = NumberToString( StringToNumber( sYear ) + 1 );
# ISO week 1 is the first Thursday on/after Jan 1
# last ISO week is the week prior to next years week 1
# find the weekday for Jan 1 for the current and next year, where Friday is 0 and Thursday is 6
nJan1 = Dayno( Expand( '%sYear%-01-01' ));
nJan1_Day = Mod( nJan1, 7 );
nJan1_Next = Dayno( Expand( '%sYear_Next%-01-01' ));
nJan1_Day_Next = Mod( nJan1_Next, 7 );
# calculate number of days until next Thursday (weekday 6 in TM1) and set first day of year as that date
nTargetDay = 6;
nDaysTillThursday = Mod( ( nTargetDay + 7 ) - nJan1_Day, 7 );
nDaysTillThursday_Next = Mod( ( nTargetDay + 7 ) - nJan1_Day_Next, 7 );
nFirstThursday = nJan1 + nDaysTillThursday;
nFirstThursday_Next = nJan1_Next + nDaysTillThursday_Next;
# find Monday's date of the first week
nFirstMonday = nFirstThursday - 3;
nFirstMonday_Next = nFirstThursday_Next - 3;
# if parameter date date >= nFirstMonday_Next, ISO week will be in next year's week count
If( nDate >= nFirstMonday_Next );
nFirstMonday = nFirstMonday_Next;
sYear = sYear_Next;
EndIf;
# calculate the ISO week of specified date by dividing difference in days between date parameter and first monday date by 7, rounding up
nISOweek = -Int( -( nDate - nFirstMonday + 1 ) / 7 );
# write to log for testing
sISOweek = NumberToStringEx( nISOweek, '00', '', '' );
LogOutput( 'INFO', Expand( 'ISO Week for %pDate%: %sYear% W%sISOweek%' ));
Here is the code for a window of over 11 years of dates, output in a text file - less edited (I prefer my formatting):
Code: Select all
DatasourceASCIIQuoteCharacter = '';
ndf = NewDateFormatter( 'nl_BE', 'Etc/UTC', 'serial', 'date' );
# loop over more than 10 years, day by day
dLoop = Dayno( '2019-12-15');
While( dLoop <= Dayno( '2031-01-15' ));
# yyyy-mm-dd
pDate = Date( dLoop, 1);
pDateFormat = 'yyyy-MM-dd';
## parse the date parameter
nDate = ParseDate(pDate, pDateFormat,ndf);
nYear = YEAR(FormatDate(nDate, 'yyyy-MM-dd'));
sYear = NumberToString(nYear);
sYear_Next = NumberToString(nYear + 1);
sYear_prev = NumberToString(nYear - 1);
### ISO week 1 is first thursday on/after Jan 1
### last ISO week is week prior to next years week 1
## find weekday of 1 Jan for current & next year with Friday being 0 and Thursday 6
sJan1 = EXPAND('%sYear%-01-01');
nJan1 = DAYNO(sJan1);
nJan1_Day = MOD(nJan1, 7);
sJan1_Next = EXPAND('%sYear_Next%-01-01');
nJan1_Next = DAYNO(sJan1_Next);
nJan1_Day_Next = MOD(nJan1_Next, 7);
sJan1_prev = EXPAND('%sYear_prev%-01-01');
nJan1_prev = DAYNO(sJan1_prev);
nJan1_Day_prev = MOD(nJan1_prev, 7);
## calculate number of days until next Thursday (weekday 6 in TM1) and set first day of year as that date
nTargetDay = 6;
nDaysTillThursday = MOD((nTargetDay + 7) - nJan1_Day, 7);
nDaysTillThursday_Next = MOD((nTargetDay + 7) - nJan1_Day_Next, 7);
nDaysTillThursday_prev = MOD((nTargetDay + 7) - nJan1_Day_prev, 7);
nFirstThursday = nJan1 + nDaysTillThursday;
nFirstThursday_Next = nJan1_Next + nDaysTillThursday_Next;
nFirstThursday_prev = nJan1_prev + nDaysTillThursday_prev;
## find monday date of first week
nFirstMonday = nFirstThursday - 3;
nFirstMonday_Next = nFirstThursday_Next - 3;
nFirstMonday_prev = nFirstThursday_prev - 3;
## if parameter date date >= nFirstMonday_Next, ISO week will be in next year's week count
## if parameter date < nFirstMonday, ISO week will be in previous year's week count
IF(nDate >= nFirstMonday_Next);
nFirstMonday = nFirstMonday_Next;
sYear = sYear_Next;
ELSEIF(nDate < nFirstMonday);
nFirstMonday = nFirstMonday_prev;
sYear = sYear_prev;
ENDIF;
## calc ISO week of specified date by dividing difference in days between date parameter and first monday date by 7, rounding up
nISOweek = -INT(-(nDate - nFirstMonday + 1) \ 7);
## write to log for testing
sISOweek = NumberToStringEx( nISOweek, '00', '', '' );
AsciiOutput( 'INFO_' | GetProcessName | '.txt', Expand( 'ISO Week for %pDate%: %sYear% W%sISOweek%' ));
dLoop = dLoop + 1;
End;
The full 11-year loop coincides with the Excel results of the function ISOWEEKNUM. Good job both.