Counter in while loop

Post Reply
Michael Barker
Posts: 20
Joined: Fri May 11, 2012 9:57 am
OLAP Product: TM1
Version: 10.1.1
Excel Version: 2010

Counter in while loop

Post by Michael Barker »

Hi all,

I have a large number of elements I want to add to a dimension in TI,

i.e.
DimensionElementInsert('Department','',Department_1,'S');
DimensionElementInsert('Department','',Department_2,'S');
DimensionElementInsert('Department','',Department_3,'S');
DimensionElementInsert('Department','',Department_4,'S');
DimensionElementInsert('Department','',Department_5,'S');
(etc.)

Is it possible to create a counter (i) that I can substitute into the department variables (e.g. Department_i) with a WHILE loop?

Thanks in advance.
Catherine
Posts: 110
Joined: Wed May 20, 2009 7:30 am
OLAP Product: TM1
Version: 10.2.2 - PA
Excel Version: 2010
Location: Rennes, France

Re: Counter in while loop

Post by Catherine »

Hi,

Yes, it is. That would be something like that (not tested...):

i=1;
While (i<=200);
DimensionElementInsert('Department','','Department_'|i,'S');
i=i+1;
end;

Your element names would then be:
Department_1
Department_2
...
Department_200

Is that what you're looking for?

HTH
Michael Barker
Posts: 20
Joined: Fri May 11, 2012 9:57 am
OLAP Product: TM1
Version: 10.1.1
Excel Version: 2010

Re: Counter in while loop

Post by Michael Barker »

Hi Catherine,

I'm referencing Department_1, Department_2, etc. directly as variables.

Enclosing them in inverted commas populates the dimension with the text within. I'm after the variables values. So I cannot use the inverted commas.

Thanks.
declanr
MVP
Posts: 1827
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: Counter in while loop

Post by declanr »

Catherine wrote:Hi,

Yes, it is. That would be something like that (not tested...):

i=1;
While (i<=200);
DimensionElementInsert('Department','','Department_'|i,'S');
i=i+1;
end;

Your element names would then be:
Department_1
Department_2
...
Department_200

Is that what you're looking for?

HTH
Catherine has answered the question but I think the code needs just 1 small tweak, in the case above i is a numeric term so when stating 'Department_' | i the code may not work due to i not being string as such it could simply be replaced with 'Department_' | NumberToString ( i )

e.g.

i=1;
While (i<=200);
DimensionElementInsert('Department','','Department_'|NumberToString ( i ) ,'S');
i=i+1;
end;
Declan Rodger
declanr
MVP
Posts: 1827
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: Counter in while loop

Post by declanr »

Michael Barker wrote:Hi Catherine,

I'm referencing Department_1, Department_2, etc. directly as variables.

Enclosing them in inverted commas populates the dimension with the text within. I'm after the variables values. So I cannot use the inverted commas.

Thanks.

If Department_1 and Department_2 are variables then why don't you just populate in the metadata tab with a simple

DimensionElementInsert( 'Dim', '', v1, 'S');

Or do you mean that they are the names of the variables as opposed to the variable values?
Declan Rodger
Michael Barker
Posts: 20
Joined: Fri May 11, 2012 9:57 am
OLAP Product: TM1
Version: 10.1.1
Excel Version: 2010

Re: Counter in while loop

Post by Michael Barker »

They are variable names. There's s lot of them. I can copy/paste the code for all of them in the metadata tab, but that just seems unnecessary.
declanr
MVP
Posts: 1827
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: Counter in while loop

Post by declanr »

I'm not sure I entirely understand the problem.

Could you provide an example csv of the datasource and explain what you want to be created?
Declan Rodger
Grandryjn
Posts: 3
Joined: Thu Jul 19, 2012 12:32 pm
OLAP Product: TM1
Version: 9.4.1
Excel Version: 2003

Re: Counter in while loop

Post by Grandryjn »

I think that you want to retrieve the variable name from the concatenation of 'Departement_' and a number i in your while.

I think that you should try the "Expand" function.

Something like that "expand('%Departement_'|numbertostring(i)|'%')" should give you the variable value.

Is that correct?
Michael Barker
Posts: 20
Joined: Fri May 11, 2012 9:57 am
OLAP Product: TM1
Version: 10.1.1
Excel Version: 2010

Re: Counter in while loop

Post by Michael Barker »

Expand() is what I was looking for. Let me try it out.

Thanks!
Grandryjn
Posts: 3
Joined: Thu Jul 19, 2012 12:32 pm
OLAP Product: TM1
Version: 9.4.1
Excel Version: 2003

Re: Counter in while loop

Post by Grandryjn »

:D

Tell me if it worked ;)
mykill
Posts: 11
Joined: Thu Jun 26, 2008 12:07 pm
OLAP Product: IBM Cognos TM1
Version: 10.2
Excel Version: 2010
Location: Germany

Re: Counter in while loop

Post by mykill »

It should be a double Expand. The inner one to get the name and the outer one to get the value.
User avatar
Steve Rowe
Site Admin
Posts: 2455
Joined: Wed May 14, 2008 4:25 pm
OLAP Product: TM1
Version: TM1 v6,v7,v8,v9,v10,v11+PAW
Excel Version: Nearly all of them

Re: Counter in while loop

Post by Steve Rowe »

http://www.tm1forum.com/viewtopic.php?f=21&t=4921

A few examples here on the use of expand to manipulate variable names.

I think you only need a single expand.

Double expand is when the value you need is held in a variable that is itself the value of antother variable....
Technical Director
www.infocat.co.uk
Michael Barker
Posts: 20
Joined: Fri May 11, 2012 9:57 am
OLAP Product: TM1
Version: 10.1.1
Excel Version: 2010

Re: Counter in while loop

Post by Michael Barker »

Thanks all, works fantastically.

Code: Select all

WHILE()

j = NUMBERTOSTRING(i);

DIMENSIONELEMENTINSERT('Department','',EXPAND('%Department_'|EXPAND('%j%')|'%'),'S');

i = i +1

END
User avatar
Steve Rowe
Site Admin
Posts: 2455
Joined: Wed May 14, 2008 4:25 pm
OLAP Product: TM1
Version: TM1 v6,v7,v8,v9,v10,v11+PAW
Excel Version: Nearly all of them

Re: Counter in while loop

Post by Steve Rowe »

Except you only need on expand

DIMENSIONELEMENTINSERT('Department','',EXPAND('%Department_'| j|'%'),'S');

will work too
Technical Director
www.infocat.co.uk
Post Reply