Creating the view and subset in TM1

Hello All,
Below code is simplified version of IBM Planning Analytics code for creating the source and target TM1 Dimension Subset and TM1 Cube Views.

Mainly highlight of this code is
1. MDX
2. Temp Object
3. Static Subset

# Code for creating the TM1 Subset and View
# ===============================================
# Set Variables
# ===============================================

# Define the variables
nTempObject = 1;
StringGlobalVariable('global_view');
PROCESS_ME = GetProcessName();
sTS = TIMST(NOW(), '\y\m\d\h\i\s');
sRUID = NumberToString(Round(RAND()*100000000));
global_view = sTS | PROCESS_ME | sRUID;

# Give an appropriate Cube name below
sCube = 'abc';
sSourceMeasure = 'abc_m';
sTargetCube = 'xyz';
sTargetMeasure = 'xyz_m';

# ===============================================
# Target View
# ===============================================
sView = global_view;
sSub = sView;
sCube = sTargetCube;

# Define Elements
sE1 = ''; sE2 = ''; sE3 = ''; sE4 = ''; sE5 = ''; sE6= ''; sE7 = ''; sE8 = ''; sE9 = ''; sE10 = ''; sE11 = ''; sE12= ''; sE13 = ''; sE14 = ''; sE15 = ''; sE16 = '';

# Define the element that need to consider for a particular MDX
sE16 = sTargetMeasure;

# Create View 
IF ( ViewExists ( sCube, sView ) = 1);
  ViewDestroy ( sCube, sView );
ENDIF;

ViewCreate(sCube, sView, nTempObject);

iSkipCalcs = 1; iSkipCons = 1; iSkipZeroes = 1;

ViewExtractSkipCalcsSet(sCube, sView, iSkipCons);
ViewExtractSkipZeroesSet(sCube, sView, iSkipZeroes);
ViewExtractSkipRuleValuesSet(sCube, sView, iSkipCalcs);

# Add Subsets
i = 1;
iMax = CubeDimensionCountGet(sCube);

WHILE(i <=iMax);
  sDim = TABDIM(sCube, i);
  sEle = EXPAND('%E' | NumberToString(i) | '%');
  IF(sEle @= '');
    sMDX = '{TM1FILTERBYLEVEL({TM1SUBSETALL([' |sDim| '])},0)}';
  ELSE;
    IF(DIMIX(sDim,sEle) = 0 & SubsetExists(sDim, sEle) = 0, ItemReject('Invalid Element in '| sDim |': '| sEle | ''),0);
      sMDX = '{[' | sDim | '].[' | sEle | ']}';
      IF(ELLEV(sDim, sEle) <> 0);
        sMDX =  '{TM1FILTERBYLEVEL({TM1DRILLDOWNMEMBER('|sMDX|',ALL,RECURSIVE)},0)}';
      ENDIF;
    ENDIF;
  # Build Subsets
  IF(SubsetExists(sDim, sSub) = 1);
    SubsetDestroy(sDim, sSub);
  ENDIF;
  SubsetCreateByMDX(sSub, sMDX, nTempObject);
  # Make Static
  sTmpEle = DIMNM(sDim, 1);
  SubsetElementInsert(sDim, sSub, sTmpEle, 0);
  SubsetElementInsert(sDim, sSub, SubsetGetSize(sDim, sSub));
  ViewSubsetAssign(sCube, sView, sDim, sSub);
  i = i + 1;
END;

# Zero View
ViewZeroOut(sCube, sView);
    
# ===============================================
# Source View
# ===============================================
sRUID = NumberToString(Round(RAND()*100000000));
global_view = sTS | PROCESS_ME | sRUID;

sView = global_view;
sSub = sView;


# Define Elements
sE1 = ''; sE2 = ''; sE3 = ''; sE4 = ''; sE5 = ''; sE6= ''; sE7 = ''; sE8 = ''; sE9 = ''; sE10 = ''; sE11 = ''; sE12= ''; sE13 = ''; sE14 = ''; sE15 = ''; sE16 = '';

# Define the element that need to consider for a particular MDX
sE16 = sSourceMeasure;

# Create View 
IF ( ViewExists ( sCube, sView ) = 1);
  ViewDestroy ( sCube, sView );
ENDIF;

ViewCreate(sCube, sView, nTempObject);

iSkipCalcs = 0; iSkipCons = 0; iSkipZeroes = 1;

ViewExtractSkipCalcsSet(sCube, sView, iSkipCons);
ViewExtractSkipZeroesSet(sCube, sView, iSkipZeroes);
ViewExtractSkipRuleValuesSet(sCube, sView, iSkipCalcs);

# Add Subsets
i = 1;
iMax = CubeDimensionCountGet(sCube);

WHILE(i <=iMax);
  sDim = TABDIM(sCube, i);
  sEle = EXPAND('%E' | NumberToString(i) | '%');
  IF(sEle @= '');
    sMDX = '{TM1FILTERBYLEVEL({TM1SUBSETALL([' |sDim| '])},0)}';
  ELSE;
    IF(DIMIX(sDim,sEle) = 0 & SubsetExists(sDim, sEle) = 0, ItemReject('Invalid Element in '| sDim |': '| sEle | ''),0);
      sMDX = '{[' | sDim | '].[' | sEle | ']}';
      IF(ELLEV(sDim, sEle) <> 0);
        sMDX =  '{TM1FILTERBYLEVEL({TM1DRILLDOWNMEMBER('|sMDX|',ALL,RECURSIVE)},0)}';
      ENDIF;
    ENDIF;
  # Build Subsets
  IF(SubsetExists(sDim, sSub) = 1);
    SubsetDestroy(sDim, sSub);
  ENDIF;
  SubsetCreateByMDX(sSub, sMDX, nTempObject);
  # Make Static
  sTmpEle = DIMNM(sDim, 1);
  SubsetElementInsert(sDim, sSub, sTmpEle, 0);
  SubsetElementInsert(sDim, sSub, SubsetGetSize(sDim, sSub));
  ViewSubsetAssign(sCube, sView, sDim, sSub);
  i = i + 1;
END;

# ===============================================
# Data Source 
# ===============================================
DataSourceType = 'VIEW';
DataSourceNameForServer = sCube;
DataSourceCubeView = sView;
Scroll to Top