Page 1 of 1

Add months to date?

Posted: Tue Nov 03, 2009 11:57 pm
by Tiger
Hi,

Is there an easy method/function to adding months to a date in a TI process?
I'm using TM1 9.0 sp2

Thanks in advance.

Re: Add months to date?

Posted: Wed Nov 04, 2009 12:02 am
by Alan Kirk
Tiger wrote: Is there an easy method/function to adding months to a date in a TI process?
I'm using TM1 9.0 sp2
A little more information, if you please.

Is the date a string variable, or a numeric one? If it's a string, what's the format of it? If it's a value, is it a TM1 serial date? An Excel serial date? Some other type of date value?

Re: Add months to date?

Posted: Wed Nov 04, 2009 12:06 am
by Tiger
The date is in a 'other' numeric format : 22072009 which 22 July 2009.

thanks

Re: Add months to date?

Posted: Wed Nov 04, 2009 4:34 am
by Alan Kirk
Tiger wrote:The date is in a 'other' numeric format : 22072009 which 22 July 2009.
I'm afraid that it doesn't make any sense to have that as a numeric format.

Most date systems use integer values to represent dates. In this way you can do arithmetic operations on those date values. For example, Excel represents today's date as 40121. If you want to get next week's date you simply add 7 to it, and away you go.

However if the date is stored the way you have it, adding 7 is going to propel you forward 7 years rather than 7 days.

You could add 10,000 to get to the next month... unless the month is December, in which case it'll be some number that becomes meaningless as a date. Also, if you go from the 9th of November to the 10th of November in your format you're effectively going from 9,112,009 to 10,112,009, which makes no particular sense.

If the source data is in that format you're probably better off bringing it in as a string rather than a numeric value. That will allow you to dissect it and recombine it in any way you want. If you want to perform mathematical operations on it (except for bumping it by a month, which I'll come to) you'd generally use DayNo to convert it into a serial date in TM1 format, which would allow you to manipulate it mathematically.

To add a calendar month, which has a variable number of days, you'd manipulate the month component of the date (and the year component if necessary).

Here are some examples which are probably far longer than you need, but since I'm not sure what you're doing with the date you can just cherry pick the bits that are useful to you. I don't know whether your data source has leading zeroes for the day; if it does and all of the values are 8 characters long, you don't need to bother doing a test for the "month offset" obviously. This is in the Data tab of a process which pulled in a list of DMMYYY or DDMMYYY values into variable V1:

Code: Select all

# Change this to some location on your own system.
SC_OUTPUT = 'D:\TM1\Temp\NewDates.txt';

# Prevents problems with the Str() conversion
# of 4 digit values.
# This would normally go in the Prolog.
DatasourceASCIIThousandSeparator='';

# Determine whether it's DDMMYYYY or DMMYYYY.
# That will determine the character on which the month
# starts.

If ( Long (V1) = 8 );
   l_MonthOffset = 3;
ElseIf (Long (V1) = 7 );
   l_MonthOffset = 2;
Else;
   ItemReject ( 'Invalid length of date.');
EndIf;

# Get the individual components fo the date. You could get them as 
# a number in a single step, but I'm storing both strings and 
# numerics to demonstrate various ways of manipulating them.

# Strings
s_Day = Subst ( V1, 1, l_MonthOffset - 1);
s_Month = Subst ( V1, l_MonthOffset, 2 );
# Be aware that some early versions of TM1 can only handle 2 digit year strings
# in some functions, but this code assumes a more current version.
s_Year = Subst ( V1, l_MonthOffset + 2, 4 ) ;

# Numbers
l_Day = Numbr(s_Day );
l_Month = Numbr ( s_Month);
l_Year = Numbr (s_Year);

# As an example, let's convert the components to a serial date
l_OriginalDateSerial = DayNo( s_Year | '-' | s_Month | '-' | s_Day );

# Add a calendar month. The problem is that these
# can have varying day lengths so we'll add it to the
# month component calculated above.

l_MonthNew = l_Month+1;

If ( l_MonthNew = 13 ); 
    l_Month = 1;
    l_Year = l_Year + 1;
Else;
    l_Month = l_MonthNew;
EndIf;

# Store the new month and year strings. For the month
# we'll keep a leading zero.
s_Month = If( l_Month < 10, '0', '') | Trim ( Str ( l_Month, 2, 0 ) );
s_Year = Trim ( Str ( l_Year, 4, 0 ) );

# And get the new serial date...
l_NewDateSerial = DayNo(  s_Year | '-' |  s_Month | '-' |  s_Day );

# You can then manipulate these as needed to recreate the date in the required format; 
# as either string or numeric.

# This shows us the old and new dates in string format.
AsciiOutput ( SC_OUTPUT, 'Old Date', V1, 'NewDate', 
 s_Day | s_Month | s_Year );

# This allows us to clearly see how many days were added.
AsciiOutput (  SC_OUTPUT, NumberToString ( l_OriginalDateSerial ) , 
 NumberToString ( l_NewDateSerial )  ); 

Re: Add months to date?

Posted: Wed Nov 04, 2009 5:08 am
by kpk
Hello,

What about creating some string attributes for the elements of your "date" dimensions?
This approach could help you to avoid complex rules writing in TI.

NextDay, PrevDay, NextMonth, PrevMonth, LastDayOfPrevMonth....
It is very easy to create an Excel with your dates and the above mentioned attributes.
Afterwards you can use DBSS to load the data into TM1.

During the process you can retrieve the necessary
value (attributes) with the GetAttrS TI functions.

Since the date dimension is not a dynamic you can do an initial upload for 10 years or more:).

Regards,

Peter

Re: Add months to date?

Posted: Wed Nov 04, 2009 8:11 pm
by Tiger
Allan:
I know that date makes no sense at all, I've been trying to have it changed. Thank you so much for the code, I will implement and report back.

Peter:
Excel's functions are very rich and we currently doing it that way, but it takes 4 hours. The TI process takes 15 seconds.

Thanks for your help :)

Re: Add months to date?

Posted: Wed Nov 04, 2009 9:14 pm
by kpk
Tiger wrote:Allan:
I know that date makes no sense at all, I've been trying to have it changed. Thank you so much for the code, I will implement and report back.

Peter:
Excel's functions are very rich and we currently doing it that way, but it takes 4 hours. The TI process takes 15 seconds.

Thanks for your help :)
Hello,

I have not suggested to load your business data via Excel!

I have only suggested to upload the attributes via Excel.
That is something you have to do once in a lifetime:).

As a result of this upload you will have the following or similar information available in TM1:
element of "date" dimension "NextMonthDate" string attribute of "date" dimension
20091104 20091204
20091105 20091205
etc.
From your post I had no precise clue how to create the "NextMonthDate" attribute.
E.g. what is the correspondent element of 31 of Oct? 30 of Nov or 1st of Dec.
It does not matter.
It can be defined, calculated and uploaded as required in your business.

Beside this I have suggested to use this data during the upload process.
"During the process you can retrieve the necessary
value (attributes) with the GetAttrS TI functions."

E.g.
CellPutN(value, 'cube', dim1,dim2,dim3,AttrS('date',date, 'NextMonthDate'));
The point in this approach is to eliminate the complex calculation from the TI process itself and use an attribute (or a user defined lookup cube) to define the target element of the upload process.

Regards,

Peter