Subset Element Exists

Post Reply
MattyG
Posts: 6
Joined: Sat May 10, 2008 7:18 pm

Subset Element Exists

Post by MattyG »

Hi all,

Is there such a TI function as SUBSETELEMENTEXISTS or similar. I've checked the help files and can't find anything.

Is there a way round this in TI, without using DIMIX?

Cheers,
Matt
User avatar
Martin Ryan
Site Admin
Posts: 1988
Joined: Sat May 10, 2008 9:08 am
OLAP Product: TM1
Version: 10.1
Excel Version: 2010
Location: Wellington, New Zealand
Contact:

Re: Subset Element Exists

Post by Martin Ryan »

You could use a while loop with the functions SubsetGetSize and SubsetGetElementName.

Code: Select all

top=SubsetGetSize('Dim', 'Sub');
count=0;
SubElemExists=0;
while(count<top);
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
count=count+1;
end;
Martin
Last edited by Martin Ryan on Fri Sep 19, 2008 9:21 am, edited 1 time in total.
Reason: Forgot to add the "count+1";
Please do not send technical questions via private message or email. Post them in the forum where you'll probably get a faster reply, and everyone can benefit from the answers.
Jodi Ryan Family Lawyer
User avatar
bihints.com
Posts: 52
Joined: Tue May 20, 2008 8:56 am
OLAP Product: TM1
Version: 9.0.3
Excel Version: 2003
Contact:

Re: Subset Element Exists

Post by bihints.com »

use MDX

SubsetCreateByMDX('Subset 2','{EXCEPT( {[dim].[Subset 1]}, {[dim].[element1]} )}');

then compare the sizes of 'Subset 1' and 'Subset 2' with SubsetGetSize
if both subsets are the same size then it means element1 is not in 'Subset 1'.
MattyG
Posts: 6
Joined: Sat May 10, 2008 7:18 pm

Re: Subset Element Exists

Post by MattyG »

Thanks Martin,

Whereabouts in the code should I put the SUBSETELEMENTINSERT function?

Cheers,
Matt
User avatar
Martin Ryan
Site Admin
Posts: 1988
Joined: Sat May 10, 2008 9:08 am
OLAP Product: TM1
Version: 10.1
Excel Version: 2010
Location: Wellington, New Zealand
Contact:

Re: Subset Element Exists

Post by Martin Ryan »

At the end, like so...

Code: Select all

top=SubsetGetSize('Dim', 'Sub');
count=0;
SubElemExists=0;
while(count<top);
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
count=count+1;
end;
# If element wasn't found in the subset already, then add it in.
if(SubElemExists)=0;
SubsetElementInsert('Dim', 'sub', soughtElement, 1);
endif;
Please do not send technical questions via private message or email. Post them in the forum where you'll probably get a faster reply, and everyone can benefit from the answers.
Jodi Ryan Family Lawyer
MattyG
Posts: 6
Joined: Sat May 10, 2008 7:18 pm

Re: Subset Element Exists

Post by MattyG »

Nearly there . . . .

Count = 1, instead of Count = 0, but the last element in my list is entered into the subset twice, any ideas why?

TIA
Matt
MattyG
Posts: 6
Joined: Sat May 10, 2008 7:18 pm

Re: Subset Element Exists

Post by MattyG »

Moving up count = count + 1 :

top=SubsetGetSize('Dim', 'Sub');
count=1;
SubElemExists=0;
while(count<top);
count=count+1;
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
end;
# If element wasn't found in the subset already, then add it in&#46;
if(SubElemExists)=0;
SubsetElementInsert('Dim', 'sub', soughtElement, 1);
endif;

This means that ONLY the last element of the list is added to the subset. Still not quite right.
User avatar
Alan Kirk
Site Admin
Posts: 6606
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: Subset Element Exists

Post by Alan Kirk »

MattyG wrote:Moving up count = count + 1 :

top=SubsetGetSize('Dim', 'Sub');
count=1;
SubElemExists=0;
while(count<top);
count=count+1;
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
end;
# If element wasn't found in the subset already, then add it in&#46;
if(SubElemExists)=0;
SubsetElementInsert('Dim', 'sub', soughtElement, 1);
endif;

This means that ONLY the last element of the list is added to the subset. Still not quite right.
I don't think that you should have moved
count=count+1;
up there; it means that you're ignoring the 1st element in the subset.

(Also 1 minor tweak that I'd suggest;

Code: Select all

if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
count = top;
endif;
There's no point looping through the other elements if you find the one you're after.

But as to your actual problem... you ARE doing this on the Metadata tab and not one of the others, yes?
"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.
MattyG
Posts: 6
Joined: Sat May 10, 2008 7:18 pm

Re: Subset Element Exists

Post by MattyG »

Hi Alan,

This is the code as it stands, which does what I want it to do. Moving the Count = Count + 1 up just puts the last element in the list into the subset.

I have put + 1 on the end of Top = SUBSETGETSIZE ( ' Dim ', ' Sub ' ), because without it the last element in the list is added twice into the subset.

I was doing all this on the Data tab, but I've just tried moving it to the Metadata tab, and the results are the same.

IF ( SUBSETEXISTS ( ' Dim ', ' Sub ' ) = 0 ) ;
SUBSETCREATE ( ' Dim ', ' Sub ' ) ;
ENDIF ;

Top = SUBSETGETSIZE ( ' Dim ', ' Sub ' ) + 1 ;
Count = 1 ;
SubsetElementExists = 0 ;
WHILE ( Count < Top ) ;
IF ( SUBSETGETELEMENTNAME ( ' Dim ', ' Sub ', Count ) @= SoughtElement) ;
SubsetElementExists = 1 ;
Count = Top ;
ENDIF ;
Count = Count + 1 ;
END ;
IF ( SubsetElementExists = 0 ) ;
SUBSETELEMENTINSERT ( ' Dim ', ' Sub ', SoughtElement, 1 ) ;
ENDIF ;

However, I would still like to incorporate the functionality of rebuilding the subset each time the process is run. Any ideas?

Cheers,
Matt
Post Reply