Remove duplicate elements from the subset

Post Reply
pandeytm777
Posts: 4
Joined: Mon Nov 23, 2020 5:49 am
OLAP Product: Planning analytics
Version: 2.0
Excel Version: 2016

Remove duplicate elements from the subset

Post by pandeytm777 »

Hi,
I have a requiement where dimension (pProduct) and subset name (pPorductSubset) are being passed as parameters to a TI process. The subset contains duplicate elements as shown below. How do i delete all the duplicate elements (not distinct) ? In the below example, the requirement is to remove the elements for Banana and Orange which are appearing twice in the subset. Another ask is to code it only using the prolog. Is it possible to do this ?

Input pProductSubset
---------------------h
Mango
Banana
Orange
Apple
Banana
Guava
Orange

Expected Output for the pProductSubset:
--------------------------------------------------------
Mango
Apple
Guava

Thank you!
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: Remove duplicate elements from the subset

Post by Wim Gielis »

What code do you come up with ? Then helpers can have a look at it and suggest improvements.

I would think that the functions SubsetGetSize, SubsetGetElementName, SubsetElementDelete in a double loop will be what you need.
Or, go the MDX route and use TM1SubsetToSet and Distinct and 1 loop.
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: Remove duplicate elements from the subset

Post by Wim Gielis »

Untested code.

Code: Select all

sDim = pProduct; sSub = pProductSubset;
n1 = SubsetGetSize( sDim, sSub );
While( n1 >= 1 );

   sEl1 = SubsetGetElementName( sDim, sSub, n1 );

   n2 = n1 - 1;
   While( n2 >= 1 );
   
      sEl2 = SubsetGetElementName( sDim, sSub, n2 );
      If( Dimix( sDim, sEl1 ) = Dimix( sDim, sEl2 ));
         SubsetElementDelete( sDim, sSub, n1 );
         Break;
      EndIf;
   
      n2 = n2 - 1;
   End;

   n1 = n1 - 1;
End;
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
David Usherwood
Site Admin
Posts: 1453
Joined: Wed May 28, 2008 9:09 am

Re: Remove duplicate elements from the subset

Post by David Usherwood »

I recall that Hierarchy Sort has the useful side effect of removing duplicates - and it is supported by the MDX Record Expression feature:

Code: Select all

{ HIERARCHIZE( {TM1SubsetBasis()} ) }
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: Remove duplicate elements from the subset

Post by Wim Gielis »

Indeed, good catch !

I forgot, even when I do this all the time manually in the Subset Editor !
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
pandeytm777
Posts: 4
Joined: Mon Nov 23, 2020 5:49 am
OLAP Product: Planning analytics
Version: 2.0
Excel Version: 2016

Re: Remove duplicate elements from the subset

Post by pandeytm777 »

Hi Wim Gielis,
Thank you for your reply. I used the similar logic. Your code and my code produces distinct list of elements. But I want to delete all the elements that are appearing more than once. If you look at the example I provided in my original post, elements "Banana" and "Orange" appear twice and I want to delete both the occurrences of that element. To summarize, "banana" and "Orange" should not be part of the subset at all after the TI process is run as shown in my expected output. I can use DimensionElementDelete. But it deletes the element from the dimension itself. I want to delete it only form the subset.
User avatar
PavoGa
MVP
Posts: 617
Joined: Thu Apr 18, 2013 6:59 pm
OLAP Product: TM1
Version: 10.2.2 FP7, PA2.0.9.1
Excel Version: 2013 PAW
Location: Charleston, Tennessee

Re: Remove duplicate elements from the subset

Post by PavoGa »

TM1TupleSize maybe?

This code will return the unique leaf elements of the subset:

Code: Select all

TM1FilterByLevel(FILTER( [pProduct].[pProductSubset],
		TM1TupleSize(INTERSECT([pProduct].[pProductSubset], {[pProduct].currentmember}, ALL).item(1)) = 0),
		0)
To find those that exist at least twice:

Code: Select all

TM1FilterByLevel(FILTER( [pProduct].[pProductSubset],
		TM1TupleSize(INTERSECT([pProduct].[pProductSubset], {[pProduct].currentmember}, ALL).item(1)) = 1),
		0)
Ty
Cleveland, TN
Wim Gielis
MVP
Posts: 3113
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: Remove duplicate elements from the subset

Post by Wim Gielis »

pandeytm777 wrote: Tue Nov 24, 2020 1:42 pm Hi Wim Gielis,
Thank you for your reply. I used the similar logic. Your code and my code produces distinct list of elements. But I want to delete all the elements that are appearing more than once. If you look at the example I provided in my original post, elements "Banana" and "Orange" appear twice and I want to delete both the occurrences of that element. To summarize, "banana" and "Orange" should not be part of the subset at all after the TI process is run as shown in my expected output. I can use DimensionElementDelete. But it deletes the element from the dimension itself. I want to delete it only form the subset.
So you still have 2 options. The MDX way as shown above, or looping subset elements. If you came up with similar code as I did then I’m sure you could tweak it to remove all instances of the duplicate elements.
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
Post Reply