Avoid copying code

Let’s say you have a bunch of code that you want to apply on a number of arguments. As an example, let&rsq;s say you have 10 hard-coded names, and you need to create a dimension for each of them. Besides that, you also want to set the sort order of elements in that dimension. You also need to create a subset in each of the dimensions and add an alias.

This is the code for a dimension called 'a' (some short name, only for illustration purposes):

MyDim = 'a';

DimensionDestroy( MyDim );

DimensionSortOrder( MyDim, 'ByName', 'Ascending', 'ByHierarchy', 'Ascending');

SubsetCreate(MyDim, 'MySubset');

AttrInsert(MyDim, '', 'MyAttribute', 'A');

Effectively, this leads to 6 lines of code. For 10 dimensions, you have 60 lines of code, who only differ for the parameter. This does not seem to be a viable strategy. What alternatives do we have?

A better strategy

  • Storing the data temporarily into a dimension/subset, or ASCIIOUTPUT it to a text file. After that, you use this source as the data source. This seems to be too much hassle.
  • Transferring the generic code (the 6 lines) to a separate process with a parameter. Execute the second process 10 times with an EXECUTEPROCESS statement.
  • Use a chore and global variables.
  • Loop through a string containing the 10 dimension names.

Sample code

This last option seems to be the most promising one. Here is example code.

# Wim Gielis # http://www.wimgielis.com
##### # A general approach to avoid copying TI code # 11/07/2009 #####
MyDims = 'a§bc§d§efgh§i§j§k§lm§nopq§r§'; While( Long( MyDims ) > 0); MyDim = Subst( MyDims, 1, Scan('§', MyDims) - 1); DimensionDestroy( MyDim ); DimensionSortOrder( MyDim, 'ByName', 'Ascending', 'ByHierarchy', 'Ascending'); SubsetCreate(MyDim, 'MySubset'); AttrInsert(MyDim, '', 'MyAttribute', 'A'); MyDims = Delet( MyDims, 1, Scan('§', MyDims )); End;

TM1 rules text functions

The functions LONG, SUBST, DELET and SCAN are key here. The variable MyDims is filled with the full text, but in each iteration in the WHILE structure, part of it is deleted so as to end with an empty string.

Wrap up

By using this general method, we avoid copying numerous lines of code. This turns our code into a much more maintainable code.




Homepage

Section contents

About Wim

Wim Gielis is a Business Intelligence consultant and Excel expert

Other links