US and Worldwide: +1 (866) 660-7555
Results 1 to 4 of 4

Thread: Incrementing variable values

  1. #1
    Join Date
    Jun 2015
    Posts
    12

    Default Incrementing variable values

    Hi All,

    Im pretty new to Pentaho, and I am struck with a small problem. The following is my data:


    ParentValue
    ChildVal
    ChildVal
    ChildVal
    ParentValue2
    ChildVal
    ChildVal


    Now, i need to identify the child rows number, i.e the expected output is as follows:

    ParentValue
    ChildVal1
    ChildVal2
    ChildVal3
    ParentValue2
    ChildVal1
    ChildVal2



    How do i achieve this result? I have tried setting a variable at the job level, and in the transformation, i tried to increment this variable. But everytime the variable's value is being reset for every row. how do i persist the value?

    Kindly guide me on this.

    Thanks in advance!!

  2. #2
    Join Date
    Sep 2014
    Posts
    122

    Default

    Hi,

    I think the step "Add value fields changing sequence" can do this logic.

  3. #3
    Join Date
    Jun 2015
    Posts
    7

    Default

    This can be achieved in JavaScript transformation. Here is the code

    Code:
    var child_count = getVariable('child_counter',0); 
    var parent_count = getVariable('parent_counter',0);  
    
    if( input_column == 'ParentValue' ) {
      setVariable('parent_counter', parent_count+1,'r');
      setVariable('child_counter', 0,'r');
    }
    else {
      setVariable('child_counter', child_count+1,'r');
    }
    
    child_count = getVariable('child_counter',0); 
    parent_count = getVariable('parent_counter',0);
    Check the exact syntax for getVariable and setVariable functions.

  4. #4
    Join Date
    Feb 2013
    Posts
    530

    Default

    Hi,

    Below is my approach to solve the problem.

    Data Grid -> Add value fields changing sequence -> Java Script.

    var
    res = field.concat(seq);

    var indexOfParentValue=res.indexOf("ParentValue");
    if(indexOfParentValue ==0){
    var finalField=field;
    }
    else{
    var finalField= res;
    }
    Sample output:
    field;seq;res;indexOfParentValue;finalField
    ParentValue;1;ParentValue1;0;ParentValue
    ChildVal;1;ChildVal1;-1;ChildVal1
    ChildVal;2;ChildVal2;-1;ChildVal2
    ChildVal;3;ChildVal3;-1;ChildVal3
    ParentValue2;1;ParentValue21;0;ParentValue2
    ChildVal;1;ChildVal1;-1;ChildVal1
    ChildVal;2;ChildVal2;-1;ChildVal2
    - Sadakar Pochampalli

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •