Nested IF statements in TI

Post Reply
dfrench77
Posts: 52
Joined: Thu Feb 17, 2011 2:52 pm
OLAP Product: TM1
Version: 9.5
Excel Version: 2007

Nested IF statements in TI

Post by dfrench77 »

Can someone please let me know why the following code is not compiling in TI on the MetaData tab?

vDateUsed=IF(LONG(vDate)=10);
SUBST(vDate,1,5);
ELSEIF(LONG(vDate)=9);
SUBST(vDate,1,4);
ELSEIF(LONG(vDate)=8);
SUBST(vDate, 1, 3);
ENDIF;

However the following code is compiling:

vDateUsed=If(LONG(vDate)=10, SUBST(vDate, 1, 5), SUBST(vDate, 1, 4));

Per TM1 documentation:
The TurboIntegrator If statement differs from the Rules IF function in that the TurboIntegrator statement can accept multiple ElseIf statements to evaluate multiple expressions, while the Rules IF function can evaluate only one expression.

Thank you.
Alan Kirk
Site Admin
Posts: 6647
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: Nested IF statements in TI

Post by Alan Kirk »

dfrench77 wrote:Can someone please let me know why the following code is not compiling in TI on the MetaData tab?

vDateUsed=IF(LONG(vDate)=10);
SUBST(vDate,1,5);
ELSEIF(LONG(vDate)=9);
SUBST(vDate,1,4);
ELSEIF(LONG(vDate)=8);
SUBST(vDate, 1, 3);
ENDIF;

However the following code is compiling:

vDateUsed=If(LONG(vDate)=10, SUBST(vDate, 1, 5), SUBST(vDate, 1, 4));

Per TM1 documentation:
The TurboIntegrator If statement differs from the Rules IF function in that the TurboIntegrator statement can accept multiple ElseIf statements to evaluate multiple expressions, while the Rules IF function can evaluate only one expression.

You're misunderstanding the syntax. The "block" If test is a flow control statement. It doesn't return a value, it merely evaluates to True or False. If it's True, the next block of line(s) will be executed. If False, they won't be. The "in line" If() function, as you've found, DOES return a value. They serve different purposes. The Block one would work like this:

Code: Select all

                    IF(LONG(vDate)=10);
                           vDateUsed=SUBST(vDate,1,5);
                    ELSEIF(LONG(vDate)=9); 
                           vDateUsed=SUBST(vDate,1,4); 
                    ELSEIF(LONG(vDate)=8); 
                            vDateUsed=SUBST(vDate, 1, 3);
                    ENDIF;
The "in line" rules If works in TI (as most Rules functions do) but obviously the advantage of the "block" If syntax is that you can execute multiple lines of code for each block, not just do a single value assignment.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
dfrench77
Posts: 52
Joined: Thu Feb 17, 2011 2:52 pm
OLAP Product: TM1
Version: 9.5
Excel Version: 2007

Re: Nested IF statements in TI

Post by dfrench77 »

Alan - Thanks for that explanation. That makes sense to me now. Your explanation should be included in the TM1 documentation.

Thanks.
jrizk
Posts: 48
Joined: Thu Nov 19, 2009 10:38 pm
OLAP Product: Tm1
Version: 10.2.2
Excel Version: 2010

Re: Nested IF statements in TI

Post by jrizk »

To simply your script you could try:

vDateUsedLen = LONG(vDate) - 5;
vDateUsed=SUBST(vDate,1,vDateUsedLen);
J.Rizk
Tm1 for everyone
anathaw
Posts: 2
Joined: Wed Jun 25, 2014 2:35 pm
OLAP Product: cognos TM1
Version: 9.5.2
Excel Version: tm1 perspective

Re: Nested IF statements in TI

Post by anathaw »

Hi , we do have a maximum limit to the no. of nested ifelse we can use in a TI. Are you aware of the maximum limit?
pandinus
Posts: 78
Joined: Tue Mar 18, 2014 8:02 am
OLAP Product: TM1, Cognos Express
Version: 10.2.2
Excel Version: 2013

Re: Nested IF statements in TI

Post by pandinus »

anathaw wrote:Hi , we do have a maximum limit to the no. of nested ifelse we can use in a TI. Are you aware of the maximum limit?
Yes, as far as I can remember this is about 15 or 16.
jacktuckerman
Posts: 16
Joined: Tue Mar 29, 2011 12:47 pm
OLAP Product: TM1/Cognos Express
Version: 9.5.1 - 10.2
Excel Version: 2003-2013

Re: Nested IF statements in TI

Post by jacktuckerman »

I think it may support up to 20, at least it used to.

Also - depending on what version of TM1 you are using, a TI script will actually save without error if you exceed the nested limit, I think any version prior to 9.4-ish.
declanr
MVP
Posts: 1828
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: Nested IF statements in TI

Post by declanr »

This is actually one thing where the documentation is quite clear and correct at the same time.

The limit is 20 and for the last few versions at least you get the following error if you hit 21 nested ifs or more:

"Maximum IF stack depth exceeded..."

The ellipses relates to the part where it tries to include the start of your 20+ nested if statement in the error message... because that's going to fit in so nicely...
Declan Rodger
jacktuckerman
Posts: 16
Joined: Tue Mar 29, 2011 12:47 pm
OLAP Product: TM1/Cognos Express
Version: 9.5.1 - 10.2
Excel Version: 2003-2013

Re: Nested IF statements in TI

Post by jacktuckerman »

Just to add one last thing to this, I think versions 9.4 - 10 (as declanr states after that you get a meaningful error message), the error message is the classically cryptic "possible missing y after x", roughly translated as TI telling you "there's an error somewhere, go find it chump".
declanr
MVP
Posts: 1828
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: Nested IF statements in TI

Post by declanr »

jacktuckerman wrote:Just to add one last thing to this, I think versions 9.4 - 10 (as declanr states after that you get a meaningful error message), the error message is the classically cryptic "possible missing y after x", roughly translated as TI telling you "there's an error somewhere, go find it chump".
9.5.2 definitely gives the "Maximum IF stack depth exceeded" message but I'm not in a position to check any earlier than that, certainly wouldn't surprise me that it wouldn't tell you. I doubt it's one that most people encounter anyway but it has been documented since at least 9.4 that the limit is 20.
Declan Rodger
anathaw
Posts: 2
Joined: Wed Jun 25, 2014 2:35 pm
OLAP Product: cognos TM1
Version: 9.5.2
Excel Version: tm1 perspective

Re: Nested IF statements in TI

Post by anathaw »

thanks for the information :)
Post Reply