Page 1 of 1

Adding attributes only to newly created elements

Posted: Thu Dec 03, 2020 4:38 pm
by michalb
Hello,

Please bear in mind I am a new TM1 user, so following question might look silly for you.

There is a process in my model that runs daily and imports products (vProduct) from .csv file to a product dimension (V_Dimension). Then it adds attribute 'Product Status' value as 'Active' to every product from source Let's simplify that there are no consolidations in this dimension.

Metadata:
DimensionElementInsert(V_Dimension, '', vProduct, 'N');
Data:
AttrPutS('Active',V_Dimension,vProduct,'Product Status');
That .csv file contains a lot of products, some of them are indeed 'Active' some of them not. To manage this I created also dictionary cube that allows users to modify attributes and process that update atrributes based on that dictionary cube. But the process described at the beggining obviously overrite all the newly created attributes.

What I want to do is to add 'Active' attribute only to new elements from source, that weren't present in V_Dimension. What is the best practise to do this? Should I create somewhere a loop through V_Dimension and somehow flag existing products just not to insert attributes for them?

regards

Re: Adding attributes only to newly created

Posted: Thu Dec 03, 2020 5:16 pm
by gtonkin
Anything new i.e. created in Metadata would have a blank/empty 'Product Status'.
You could check in Data if the attribute is blank then update it otherwise don't.

If you have blanks existing in 'Product Status' this would obviously not work.

Plan B may be to use the Direct functions and insert the element and update the attribute in Data.

Re: Adding attributes only to newly created

Posted: Thu Dec 03, 2020 7:39 pm
by declanr
Or create a temporary static subset in the prolog of all Products that exist when the process starts.
Then in the data tab use SubsetElementGetIndex and if it returns a zero then it is a newly added Product.

Re: Adding attributes only to newly created

Posted: Fri Dec 04, 2020 11:03 am
by lotsaram
Many ways to skin a cat.

Or loop through all products in the Prolog tab and set the status to empty or "Inactive" with AttrPutS function. Then in the Data tab set status "Active" with AttrPutS. This way you can be sure that only products present in the file will have status "Active".

(Note: this isn't quite what you asked for "Only flag newly created products as active". But I think it is the end result that you want).

Re: Adding attributes only to newly created

Posted: Fri Dec 04, 2020 11:20 am
by tomok
lotsaram wrote: Fri Dec 04, 2020 11:03 am Or loop through all products in the Prolog tab and set the status to empty or "Inactive" with AttrPutS function. Then in the Data tab set status "Active" with AttrPutS.
Or create a view on the }ElementAttributes_Products dimension with just that one attribute and do a ViewZeroOut. This will likely be the fastest option unless the dimension has just a few elements. The overhead of creating and destroying the view might exceed the time needed to loop and do a CellPutS on every element if the dimension is really small.

Re: Adding attributes only to newly created

Posted: Fri Dec 04, 2020 11:37 am
by lotsaram
tomok wrote: Fri Dec 04, 2020 11:20 am The overhead of creating and destroying the view might exceed the time needed to loop and do a CellPutS on every element if the dimension is really small.
My you're up early.
Yep. That is indeed my experience. Quite often looping (or even nested looping) without the benefit of zero supression is actually much faster than using a view due to avoiding the overhead of creating, registering and destroying objects.

Re: Adding attributes only to newly created

Posted: Fri Dec 04, 2020 3:35 pm
by michalb
lotsaram wrote: Fri Dec 04, 2020 11:03 am
Or loop through all products in the Prolog tab and set the status to empty or "Inactive" with AttrPutS function. Then in the Data tab set status "Active" with AttrPutS. This way you can be sure that only products present in the file will have status "Active".

(Note: this isn't quite what you asked for "Only flag newly created products as active". But I think it is the end result that you want).
Indeed, this is not quite what I wanted to achive, because in the .csv file there are also 'Inactive' products, which are going to be flag as 'Inactive' in mentioned Dictionary Cube.
AttrPutS in Data tab will flag them again as 'Active' I guess.
Please note that both in V_Dimension and .csv file we have all the products existing (active and inactive). The thing is in .csv file we have new products added every day which are needed to be added to V_Dimension and have autmatically added attribute 'Active' (which can be later changed in Ditcionary cube).

I think I'll try with @lotsaram solution, however, I am not quite sure what 'static subset' means yet :)
@gtonkin ideas are nice too, but indeed I have some blanks existing in 'Product Status' and I am not familiar with 'direct functions'

Update:

Prolog:
V_Dimension='Test Product';

V_MDX='{TM1SUBSETALL( ['|V_Dimension|'] )}';
V_SubsetName='Products from dimension';

SubsetCreatebyMDX(V_SubsetName, V_MDX, 1);
SubsetMDXSet( V_Dimension, V_SubsetName, '' );
Metadata:
DimensionElementInsert(V_Dimension, '', vProduct, 'N');
Data:
V_Index=SubsetElementGetIndex(V_Dimension, V_SubsetName, vProduct, 1);

If(V_Index=0);
AttrPutS('Active',V_Dimension,vProduct,'Attribute test');
Endif;
works just fine for me. thanks for your responses