Writing a variable-depth hierarchy creation process

Ivan Kulman -

Welcome to the third article in the MODLR Development series.

In this article we’re going to expand on the process knowledge and develop a basic variable-depth hierarchy building process.

 

Firstly, let’s define a variable-depth hierarchy: A variable-depth hierarchy has varying numbers of levels between the top parent and the N-level (lowest-level) elements. In addition to the variable hierarchy levels, there might be a mix of consolidations and N-elements under another consolidation (shown in red):

 

Usually, this type of hierarchies is stored as a parent-child relationship in a separate table.

Child Parent
Eurasia World
Europe Eurasia
Asia Eurasia

The basic setup is the same as for the fixed-depth hierarchy in the previous article and we're going to use the attached file as our source this time. Also this time we won't be creating aliases.

 

Because there's no way to know how many levels there are in a provided hierarchy, we need to use the blueprint functions.

Creating a dimension blue print involves three steps:

  1. Create a blueprint
  2. Generate elements within a blueprint
  3. Pushing the blueprint into a hierarchy

 

As per usual, we're defining the global variables outside of existing functions:

var dimName = 'Variable Depth Dimension';
var allElements = 'All Geography';
var hierarchyName = 'Process Generated Hierarchy';
var blueprintName = 'Blueprint';

 

Then, in the begin section we are going to define everything that should be done once:

  1. Create dimension
    if( dimension.exists(dimName) != true ) {
        dimension.create(dimName, "standard");
        script.log("Dimension " + dimName + " has been successfully created");
    }
  2. Create hierarchy
    if( hierarchy.exists(dimName, hierarchyName) != true && dimension.exists(dimName) == true ) {
        hierarchy.create(dimName, hierarchyName);
        script.log("Hierarchy " + hierarchyName + "in the " + dimName + " dimension has been successfully created");
    }
    
  3. Create blueprint
    blueprint.create(blueprintName);
    

 

Now we can proceed with the actual dimension building, this would be done in the data section because it is specific to each record.

The way we get items from the data source is by using record[‘field_name’].

So to make the development more streamlined and easier, it is recommended to get the variables separately from the actual code. In this case, the code simply gets the values from those columns and them trims them to make the dimension cleaner:

var child= record['child'].trim();
var parent = record['parent'].trim();

 

In our case this is the blueprint creation code:

blueprint.addChild(blueprintName, parent, child); 

 

Then once the all relationships are set-up in the data function, in the end function we can push the blueprint to the new hierarchy we defined earlier:

blueprint.publish(dimName, hierarchyName, blueprintName); 

 

The whole process once again:

var dimName = 'Variable Depth Dimension';
var allElements = 'All Geography';
var hierarchyName = 'Process Generated Hierarchy';
var blueprintName = 'blueprint';

function pre() {
	//this function is called once before the processes is executed.
	//Use this to setup prompts.
	script.log('process pre-execution parameters parsed.');
}

function begin() {
  if( dimension.exists(dimName) != true ) {
      dimension.create(dimName, "standard");
      script.log("Dimension " + dimName + " has been successfully created");
  }
  
  if( hierarchy.exists(dimName, hierarchyName) != true && dimension.exists(dimName) == true ) {
      hierarchy.create(dimName, hierarchyName);
      script.log("Hierarchy " + hierarchyName + "in the " + dimName + " dimension has been successfully created");
  }
  blueprint.create(blueprintName);
}

function data(record) {
    var child= record['child'].trim();
    var parent = record['parent'].trim();
    blueprint.addChild(dimName, parent, child);	
}

function end() {
    //this function is called once at the end of the process
  
    script.log(blueprint.publish(dimName, hierarchyName, blueprintName));
    script.log('process execution finished.');
}

 This is the end result of the process execution:

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk