As of December 1, 2020, Focal Point is retired and repurposed as a reference repository. We value the wealth of knowledge that's been shared here over the years. You'll continue to have access to this treasure trove of knowledge, for search purposes only.
Join the TIBCO Community TIBCO Community is a collaborative space for users to share knowledge and support one another in making the best use of TIBCO products and services. There are several TIBCO WebFOCUS resources in the community.
From the Home page, select Predict: WebFOCUS to view articles, questions, and trending articles.
Select Products from the top navigation bar, scroll, and then select the TIBCO WebFOCUS product page to view product overview, articles, and discussions.
Request access to the private WebFOCUS User Group (login required) to network with fellow members.
Former myibi community members should have received an email on 8/3/22 to activate their user accounts to join the community. Check your Spam folder for the email. Please get in touch with us at community@tibco.com for further assistance. Reference the community FAQ to learn more about the community.
Does anyone know how to calculate the week of the month? I can get the week of the year but need to know the week of each month. The values would be between 1-5.
ThanksThis message has been edited. Last edited by: Trudy,
Not sure I understand how your post works. If the 1st day of the month falls on a Friday then the 4th would be on the Monday of the following week so the 1st would be week 1 and the 4th should be week 2 but with your calculation wouldn't they both still be week 1? Maybe I am not understanding what day_of_month is in your example.
Precisely why I stated "If your week in month number always starts on the first of the month".
From your reply, it is now clear that you want your weeks to run from Monday to Sunday - you didn't state that in your question, so I had to guess!
Perhaps obtain the week number within the year from the 1st of the month and take this value away from the week number of the other days within the month?
Use DATEMOV(date in YYMD, 'BOM') to get the begging of the month for the date in your data. Use DTPART(date in YYMD, WEEK) to get a 2 digit week number for both the beginning of the month and the date in your data. Finally work out which week number a date falls within (second date minus week of first of month).
Remember that dates may belong to a week from the previous year.
Check out using ISO dates depending upon your requirement also the setting of WEEKFIRST.
Good luck
TThis message has been edited. Last edited by: Tony A,
In FOCUS since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2
WebFOCUS App Studio 8.2.06 standalone on Windows 10
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004
As an example and tester, use the following code to create a temporary table for every date in the current year -
FILEDEF DATEMAS DISK DATERNG.MAS
-RUN
-WRITE DATEMAS FILE=DATERNG,SUFFIX=XFOC
-WRITE DATEMAS SEGNAME=SEG1
-WRITE DATEMAS FIELD=DATE_KEY, ,DMYY ,DMYY , $
-RUN
-* Now create it
CREATE FILE DATERNG
-* and add all possible dates within a month
MODIFY FILE DATERNG
FREEFORM DATE_KEY.A8.
LOG FORMAT MSG OFF
LOG TRANS MSG OFF
LOG INVALID MSG OFF
-* Any invalid dates will be rejected.
-DEFAULT &Year = 2020
-SET &Year = EDIT(&YYMD,'9999$');
DATA
-* This repeat loop will ensure at least a whole year is input,
-SET &Month = 1;
-SET &Day = 1;
-REPEAT :Loop0 12 TIMES;
-SET &Month = FPRINT(&Month, 'I2L', 'A2');
-REPEAT :Loop1 31 TIMES;
-SET &Date = FPRINT(&Day, 'I2L', 'A2');
&Date&Month&Year
-SET &Day = &Day + 1;
-:Loop1
-SET &Day = 1;
-SET &Month = &Month + 1;
-:Loop0
END
-RUN
Then use the following code to test the week in month calculation -
-* Set the first day of week to Mondays
SET WEEKFIRST = 2
TABLE FILE DATERNG
SUM COMPUTE FSTOFMNTH/YYMD = DATEMOV(DATE_KEY, 'BOM'); NOPRINT
COMPUTE FSTWEEK/I2 = DTPART(FSTOFMNTH, WEEK); NOPRINT
COMPUTE CURWEEK/I2 = DTPART(DATE_KEY, WEEK); NOPRINT
COMPUTE DAYOFWK/A3 = DOWK(DATECVT(DATE_KEY, 'YYMD', 'I8YYMD'), 'A3');
COMPUTE WEEKINMNTH/I2 = IF FSTWEEK GE 52 THEN CURWEEK - ((CURWEEK GE 52) * CURWEEK) + 1 ELSE CURWEEK - FSTWEEK + 1;
BY DATE
END
-RUN
T
In FOCUS since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2
WebFOCUS App Studio 8.2.06 standalone on Windows 10
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004
Here's some code that I created and used for my Financial Calendar back in 2016... It's just the basis and should still work today.
SET WEEKFIRST = 1
SET WEEKFIRST = ISO2
...
DEFINE FILE InputFile
RPTDATE/A8MDYY = TRANSDATE ;
...
-* Define MONTH...
-* The day of week (DOW) gives us the column number but this is Sunday thru Saturday
-* and we require Monday thru Sunday
DOWX/W = TRANSDATE;
DOWNMBR/W = IF DOWX = 7 THEN 1 ELSE DOWX + 1 ;
DAYNAME/A9 = DECODE DOW (1 Sunday 2 Monday 3 Tuesday 4 Wednesday 5 Thursday 6 Friday 7 Saturday) ;
...
END
TABLE FILE InputFile
SUM
FIELD1 FIELD2 ...
BY MONTH
ACROSS DOWNMBR NOPORINT
ACROSS DOWNAME AS 'Whatever'
END
...
In FOCUS Since 1983 ~ from FOCUS to WebFOCUS. Current: WebFOCUS Administrator at FIS Worldpay | 8204, 8206
Posts: 3132 | Location: Tennessee, Nashville area | Registered: February 23, 2005
Thanks to everyone. I used the following code to get the two weeks like Tony suggested and then subtracted them to get the WEEKOFMONTH. Works beautifully.
Sorry folks realized I posted extra field calculations that I need for my report that aren't necessary for the week of month calculation so below is the cleaner code.
DB, No, you're not missing anything. There is no allowance for the first week of the year belonging to the last week of the previous year. I mentioned that this needs to be allowed for in my initial post.
Using the following in Trudy's code will sort that out -
COMPUTE WEEKOFMNTH/I2 = IF WEEK1 GE 52 THEN WEEK2 - ((WEEK2 GE 52) * WEEK2) + 1 ELSE WEEK2 - WEEK1 + 1;
T
In FOCUS since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2
WebFOCUS App Studio 8.2.06 standalone on Windows 10
Posts: 5694 | Location: United Kingdom | Registered: April 08, 2004
Perhaps you want to have the weeks run from Saturday to Friday to avoid having a weekday occur in a week number 6. I think the following code should work. It allows you to specify which day of the week is the start of a week. The last three COMPUTE statements accomplish the week of month calculation. The first sections of the code generate a file with dates for testing.
/* Can set the day that represents the first day of the week. */
/* Week Start Day 1=Mon 2=Tue 3=Wed 4=Thu 5=Fri 6=Sat 7=Sun */
-SET &Weekstart = 6;
-SET &StartDate = &DATEYY | '0101';
-SET &EndDate = &DATEYY | '1231';
-SET &Date = &StartDate;
-SET &i = 1;
SQL CREATE TABLE calendar
( KEY INTEGER,
DATEYYMD DATE );
END
MODIFY FILE calendar
MATCH KEY
ON MATCH REJECT
ON NOMATCH INCLUDE
DATA
-REPEAT ENDREPEAT WHILE &EndDate GE &Date ;
&i,'&Date',$
-SET &i = &i + 1;
-SET &Date = AYMD(&Date, 1, 'I8YYMD');
-ENDREPEAT
END
DEFINE FILE calendar
DATEMYY/MYY = DATEYYMD;
END
TABLE FILE calendar
PRINT COMPUTE DATEW/W = DATEYYMD;
COMPUTE DATEWtr/Wtr = DATEYYMD;
COMPUTE DATED/D = DATEYYMD; NOPRINT
COMPUTE DATEBOMW/W = DATEMOV(DATEYYMD, 'BOM'); NOPRINT
COMPUTE Week/I2 = ( DATED - 1 + MOD(DATEBOMW - &Weekstart + 7, 7) ) / 7 + 1 ;
BY DATEMYY NOPRINT
BY DATEYYMD
ON DATEMYY PAGE-BREAK
END
WebFOCUS 8.2.06
Posts: 210 | Location: Sterling Heights, Michigan | Registered: October 19, 2010