## Introduction

Ever wanted an easy way to duplicate a dimension? I mean, have another dimension with exactly the same elements and hierarchical structures as a chosen dimension. Here is a bunch of TI code to do it for you.

## TI is our friend again

To download all the below code in a process, right-click the file above and Save it.

Create a new TI process with data source None. Then create a parameter in the Advanced > Parameters tab:

 Parameter Type Default Value Prompt Question pDimensionOld String leave this empty Which dimension do you want to duplicate ? pDimensionNew String leave this empty How do you want to name the new dimension (empty means generic name) ?

Here is the code in the Prolog tab:

```# Wim Gielis
# https://www.wimgielis.com
#####
# TI code to duplicate a dimension, attributes are copied too
# 07/11/09
#####
IF( LONG( pDimensionNew) = 0 );
pDimensionNew = pDimensionOld | '_Copy';
ENDIF;

IF( DIMENSIONEXISTS( pDimensionOld ) = 0 % DIMENSIONEXISTS( pDimensionNew ) = 1);
PROCESSQUIT;
ENDIF;

DIMENSIONCREATE( pDimensionNew );

# loop through the elements of the original dimension
# to copy to the new dimension
i = 1;

WHILE( i <= DIMSIZ( pDimensionOld ));

# CHILD
vChildElement = DIMNM( pDimensionOld, i);
vChildType = DTYPE( pDimensionOld, vChildElement);
DIMENSIONELEMENTINSERT( pDimensionNew, '', vChildElement, vChildType);

# PARENT (loop through them)
j = 1;

WHILE(j <= ELPARN( pDimensionOld, vChildElement ));
vParentElement = ELPAR( pDimensionOld, vChildElement, j );
vChildWeight = ELWEIGHT( pDimensionOld, vParentElement, vChildElement );
DIMENSIONELEMENTINSERT( pDimensionNew, '', vParentElement, 'C' );
DIMENSIONELEMENTCOMPONENTADD( pDimensionNew, vParentElement, vChildElement, vChildWeight );
j = j + 1;
END;

i = i + 1;

END;
```

Here is the code in the Epilog tab:

```# loop through the elements of the new dimension
# and fill in the attributes
i=1;

WHILE(i <= DIMSIZ( pDimensionNew ));

vElement = DIMNM( pDimensionNew, i );

# attributes, if any
IF( DIMENSIONEXISTS( '}ElementAttributes_' | pDimensionOld ) = 1 );

# there are attributes, so loop through them
j = 1;

WHILE(j <= DIMSIZ( '}ElementAttributes_' | pDimensionOld ));

vAttributeName = DIMNM( '}ElementAttributes_' | pDimensionOld, j );
vAttributeType = SUBST( DTYPE( pDimensionOld,
'}ElementAttributes_' | vAttributeName ), 2, 1 );

# insert attribute itself
IF( i = 1 );
ATTRINSERT( pDimensionNew, '', vAttributeName, vAttributeType );
ENDIF;

# update attribute values for the element
IF( vAttributeType @= 'N' );
ATTRPUTN( ATTRN( pDimensionOld, vElement, vAttributeName),
pDimensionNew, vElement, vAttributeName );
ELSE;
ATTRPUTS( ATTRS( pDimensionOld, vElement, vAttributeName ),
pDimensionNew, vElement, vAttributeName);
ENDIF;

j = j + 1;

END;

ENDIF;

i = i + 1;

END;
```

The Prolog makes a loop through all elements in the Old dimension, inserting them (in the correct structure) in the new dimension. The Epilog tab mainly loops through all the attributes for that dimension and sets the attributes, if any.

As far as I know, in TI there is no easy way to loop through subsets, except for copying folders in the TM1 database directory on the server. With the TM1 API, it will be possible too.

#### Section contents

Wim Gielis is a Business Intelligence consultant and Excel expert