Check if Element of Dimension exists

Post Reply
maps
Posts: 43
Joined: Wed Aug 10, 2011 9:21 am
OLAP Product: TM1
Version: 9.4.1 and 9.5.1
Excel Version: 2003

Check if Element of Dimension exists

Post by maps »

Hello,

I like to import Data to a Cube. There could be the case that the data of the source file doesn't have all the same elements in the dimensions like the target cube. Thats why I like to check first if the element of the dimension exists in the target dimension.

If a Dimension exists I can easily check using "DimensionExists(DimName)", but is there a way to check if the element of the dimension exists before putting the data in the cube. I don't wanna get an error in the logfile so I like to catch the error in the code.

I wrote the following lines... But I think it is a little too complicated. Isn't ther something out of the box in TM1?

Explanation: Cube with two dimensions (abc and Country). For the Dimension abc I check if the current element exists in the target dimension.

Code: Select all

run = 1;

bFound = 'FALSE';
WHILE(DIMSIZ('abc') >= run);
  IF(vAbc @= DIMNM('abc', run));
    CellPutN( vValue, 'cubeVerhalten', vCountry, vAbc);
    bFound = 'TRUE';
    BREAK;
  ENDIF;

  run = run + 1;
END;

IF(bFound @<> 'TRUE');

  AsciiOutput ('D:\TM1\TM1Data\TestDominik\Beispielanwendungen\import file\fehler.csv', vAbc);

ENDIF;
David Usherwood
Site Admin
Posts: 1458
Joined: Wed May 28, 2008 9:09 am

Re: Check if Element of Dimension exists

Post by David Usherwood »

There is.
You will find DIMIX(dimname, elname) in the Reference Guide. It returns zero if the element is not a member of the dimension.
lotsaram
MVP
Posts: 3702
Joined: Fri Mar 13, 2009 11:14 am
OLAP Product: TableManager1
Version: PA 2.0.x
Excel Version: Office 365
Location: Switzerland

Re: Check if Element of Dimension exists

Post by lotsaram »

In other words ...

Code: Select all

IF( DIMIX('abc', vABC) > 0 );
  CellPutN( vValue, 'cubeVerhalten', vCountry, vAbc);
EndIF;
A point to note, obviously you don't need to cycle through the dimension in this case but if you did then it would be more efficient to declare a variable for the dimension size outside of the control loop then there is no need to evaluate the dimension size on each iteration of the loop. For Example ...

Code: Select all

# loop through dimension
sDimName = 'ABC';
nCounter = 1;
nMaximum = DimSiz(sDimName);
WHILE(nCounter <= nMaximum);
  sElName = DimNm(sDimName, nCounter);
  nCounter = nCounter + 1;
END;
BUT!
The thing that really grabbed be about your question was the use of BREAK within the while loop.
1/ I haven't ever seen this used in TI before
2/ Break isn't mentioned in any TI documentation that I am aware of
3/ Break isn't mentioned in any list of reserved words (again that I am aware of)

... Yet, it works! I tested it out in 9.5.1 and 9.5.2 with the following code

Code: Select all

n=1;
While( n <= 1000000 );
  n = n + 1;
  IF( n > 500000 );
    Break;
  EndIF;
End;

ASCIIOutput('File.txt', NumberToString(n));
If Break is commented the file output returns 1000001 as expected, if not commented it returns 500001.

You are a TM1 novice (not knowing about DIMIX is a dead giveaway, no offence intended) so maybe you accidentally picked this up from another programming language assuming it must work (justifiably it seems!) But if not where did you pick this up from?

They say you learn something new every day so I'm happy for today!
Did anyone else know about this??
David Usherwood
Site Admin
Posts: 1458
Joined: Wed May 28, 2008 9:09 am

Re: Check if Element of Dimension exists

Post by David Usherwood »

Vairy Interesssting....

I've often wondered about leftover corners of the TM1 codebase. My utter favourite was a debug version of 7.1.4 which dumped out a file showing the number of cells affected _per individual feeder statement_. What I'd give to have that back again :!:

But of course, IBM will say that it's not documented and thus not supported.

You are in a maze of twisty tunnels, all alike.
>>Go North
You are in a maze of twisty tunnels, all alike.
<etc>
maps
Posts: 43
Joined: Wed Aug 10, 2011 9:21 am
OLAP Product: TM1
Version: 9.4.1 and 9.5.1
Excel Version: 2003

Re: Check if Element of Dimension exists

Post by maps »

lotsaram wrote:In other words ...
You are a TM1 novice (not knowing about DIMIX is a dead giveaway, no offence intended) so maybe you accidentally picked this up from another programming language assuming it must work (justifiably it seems!) But if not where did you pick this up from?

They say you learn something new every day so I'm happy for today!
Did anyone else know about this??
Yes I'm new to TM1. I was expecting a function like ElementExists() for checking the elements but i haven't found something like this in the documentation. Of course I do know DIMIX, but I wasn't aware of the fact that it returns 0 in case the element doesn't exist (sry I'm a newbie).

I was programming java before and I just assumed that this also works for TM1. That means I just tried it and was happy that I got no error when saving my rule. So this wasn't a big thing for me... Seems like it was an accident ;)
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: Check if Element of Dimension exists

Post by Alan Kirk »

maps wrote:
lotsaram wrote:In other words ...
You are a TM1 novice (not knowing about DIMIX is a dead giveaway, no offence intended) so maybe you accidentally picked this up from another programming language assuming it must work (justifiably it seems!) But if not where did you pick this up from?

They say you learn something new every day so I'm happy for today!
Did anyone else know about this??
Not me. When I've needed to break from a loop I've always just incremented the counter to Max+1. Yay documentation.
maps wrote:Yes I'm new to TM1. I was expecting a function like ElementExists() for checking the elements but i haven't found something like this in the documentation. Of course I do know DIMIX, but I wasn't aware of the fact that it returns 0 in case the element doesn't exist (sry I'm a newbie).

I was programming java before and I just assumed that this also works for TM1. That means I just tried it and was happy that I got no error when saving my rule. So this wasn't a big thing for me... Seems like it was an accident ;)
It was a fortunate one. Over time you'll get used to things that you expect to be there not being there, or alternatively being there under a different name. As there are CubeExists and DimensionExists functions it wasn't unreasonable to assume that there would be an ElementExists function... but of course there isn't, DimIx fills that role. Similarly if you want to know the size of a subset you may assume that you'd find a SubSiz function since hey, one exists for worksheets, right? But no, because nobody at Applix could bother being consistent in naming conventions the one that you use in TI is called SubsetGetSize. That kind of thing.
"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.
Post Reply