LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Percentage Calculation

    Employee
    Posted 04-18-2012 18:24

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Anamitra

    Hi

    I have data in two cols. Col A having countries, Col B having usage. I want the Usage % in a third Col C

    I have:
    Col A Col B
    Country Usage
    Australia 20
    Singapore 10
    India 20
    USA 50
    100

    I want to see:

    Col A Col B Col C
    Country Usage %
    Australia 20 20%
    Singapore 10 10%
    India 20 20%
    USA 50 50%
    100 100%

    Thanks
    Anamitra


  • 2.  RE: Percentage Calculation

    Employee
    Posted 04-23-2012 12:22

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rboccuzzi

    Welcome to Lavastorm Community!

    So to create a new column, you want to use a Filter node, and the emit statement. If you connect a filter to your data, it will default to emit *. You will want to create the string you want to emit, putting it in a temporary variable, and then emit it. You could put this following code there:

    PercentageFormatted = str('Col B') + "%"
    emit *, PercentageFormatted as "Col C"

    This converts the number in the field "Col B" to a string, and then appends the "%" to the end of it.

    Cheers
    Rich


  • 3.  RE: Percentage Calculation

    Employee
    Posted 02-28-2018 04:11

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: sto

    new question based on your latest reply :

    how to calculate the percentage before emitting it in the 3rd column ?
    In you answer you are converting the col B number. I would like to calculate for each row the percentage and then emit it.

    Thx


  • 4.  RE: Percentage Calculation

    Employee
    Posted 03-01-2018 03:58

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: awilliams1024

    Hi sto,

    In rbocuzzi's reply the assumption was that 'Col B' already contained the percentage value. If instead you need to calculate the percentage value for each record out of the total value of the field then you will need to aggregate the field values first. You can then merge the total value back into each of the records of the data set so that you can then calculate the percentage for each record. See the example below (copy the code and paste it onto the canvas) :

    Regards,
    Adrian

    node:Usage_Data
    bretype:core::Static Data
    editor:Label=Usage Data
    editor:sortkey=5a97d92530351f8d
    output:@40fe6c55598828e5/=
    prop:StaticData=<<EOX
    Col A, Col B:int
    Australia, 20
    Singapore, 10
    India, 20
    USA, 50
    EOX
    editor:XY=340,110
    end:Usage_Data
    
    node:Merge_the_Data
    bretype:core::Lookup
    editor:Label=Merge the Data
    editor:sortkey=5a97d9ed69022a09
    input:@40fd2c746abc6dc7/=Usage_Data.40fe6c55598828e5
    input:@40fd2c74486e4494/=Calculate_Total.40fd2c744c862db0
    output:@40fd2c7445835585/=
    prop:InputKey=<<EOX
    1
    EOX
    prop:LookupKey=<<EOX
    1
    EOX
    editor:XY=560,110
    end:Merge_the_Data
    
    node:Calculate_Percentage_as_numeric_and_string_values
    bretype:core::Filter
    editor:Label=Calculate Percentage as numeric and string values
    editor:sortkey=5a97da184a071bba
    input:@40fd2c74167f1ca2/=Merge_the_Data.40fd2c7445835585
    output:@40fd2c7420761db6/=
    prop:Script=<<EOX
    ## Calculate the percentage value
    _Percent = 'Col B' * 100.0 / _Total
    
    ## Convert to string representation
    _percentStr = str(_Percent) + "%"
    
    emit *, _Percent as "Col C", _percentStr as "Col D"
    
    EOX
    editor:XY=670,110
    end:Calculate_Percentage_as_numeric_and_string_values
    
    node:Calculate_Total
    bretype:core::Agg
    editor:Label=Calculate Total
    editor:sortkey=5a97d98b0f6e204e
    input:@40fd2c7427456e5b/=Usage_Data.40fe6c55598828e5
    output:@40fd2c744c862db0/=
    prop:GroupBy=<<EOX
    1
    EOX
    prop:Script=<<EOX
    _Total = groupSum('Col B')
    
    emit _Total
    where lastInGroup
    
    EOX
    editor:XY=460,190
    end:Calculate_Total