Page 1 of 1

Create date element with date attributes

Posted: Fri Oct 28, 2016 12:02 pm
by dccarm
Greetings all.

I'm building a weekly cash forecast model and I've created a TI process that will roll the week forward every week, creating a new element in the Weekly Forecast Dimension for the following week.

After a lot of trial and error (mainly due to the fact that I first used elements in the format dd/mm/yyyy and now in format yyyy-mm-dd) I've got something that works but I can't help feeling it could be a lot simpler. I'm having to strip the current week's date down to Year, Month and Date, then add 7 to get next week, and then strip the new date back into Year, Month and Date (adding 2000 to the year to get it into 4 digits) to build the date string back up again.

Prolog:

Code: Select all

# 
# Create a new element for Next Week's forecasts
#
pOldWeek = CellGetS('Accounting_Period','Current Week','Week');
pNewWeek = CellGetS('Accounting_Period','Next Week','Week');
DimensionElementInsert ( 'FcastWk', '',pNewWeek,'N');


#
# Calculate new value of the following week's date for Next Week attribute on new element
#


pYear = NUMBR(SUBST(pNewWeek,1,4));
pMonth = NUMBR(SUBST(pNewWeek,6,2));
pDate = NUMBR(SUBST(pNewWeek,9,2));

pNewDate = DayNo(DateS(pYear,pMonth,pDate))+7;
pNewDate2 = DATE(pNewDate);


nYear = 2000+NUMBR(STR(YEAR(pNewDate2),4,0));
nMonth = MONTH(pNewDate2);
nDate = DAY(pNewDate2);

sNewDate = DATES(nYear,nMonth,nDate);
Epilog:

Code: Select all

AttrPutS(pOldWeek, 'FcastWk',pNewWeek, 'PriorWk');
AttrPutS(sNewDate, 'FcastWk',pNewWeek, 'NextWk');

CellPutS(pNewWeek,Accounting_Period','Current Week','Week');
CellPutS('READ','}ElementSecurity_FcastWk',pOldWeek,'Manager');
CellPutS('WRITE','}ElementSecurity_FcastWk',pNewWeek,'Manager');
Have I missed a really easy way to do this? (Or should I just shut up and be happy that I've got something that works?!)

Re: Create date element with date attributes

Posted: Fri Oct 28, 2016 2:35 pm
by declanr
Since you have format in YYYY-MM-DD you can use dayno() to convert it to a tm1 date serial, add 7 to that and then get it back to a string using TimSt.

Re: Create date element with date attributes

Posted: Fri Oct 28, 2016 3:12 pm
by dccarm
Thanks Declan - I wasn't aware of TIMST.

Managed to reduce that section from 7 lines to two as a result;

Code: Select all

pNewDate = DayNo(pNewWeek)+7;
sNewDate = TIMST (pNewDate, '\Y-\m-\d');