How can I calculate and add a column of percentage?

I have data something like

Screenshot 2022-11-02 at 6.32.25 PM

And I want to add another column which calculate percentage for “Number” column

Basically

 PercentageX = (NumberX) / (NumberX + NumberY + NumberZ)

This seems so genuine and easy but I can’t find an option to do so

Hey @yosiasz how it is calculating percentage ? You have just added the unit right ?

as I mentioned in the post it has to calculate this formula

The numbers in the example that I have given adds upto 100 just to simply the things, but it will not always be the case

1 Like

You give some more clarification if there are 2 rows with numbers 80, 120 then the percentage should be 40% and 60%

ah gotcha! sorry totally missed the formula. so the data is vertically stacked? What if there are 500 rows? Also what is your data source? Might be better to do it in the data source itself than grafana

No worries, I shouldn’t have added the an example that sums up to 100 haha.

Datasource is actually infinity - json and it’s an external api so it’s not possible for me to change how I get the data unless I code a wrapped on top of it to calculate the sum which is a lot work.

Numbers of rows should ideally be not that huge

please post the structure of the data coming from api minus confidential stuff, obfuscate it. I think one could use UQL for this.

that way we can emulate your stack otherwise it would be guess work, back and forth questions

The json object that I am getting is formatted like this @yosiasz

{
  "data": [
    {
      "String": "A",
      "Number": 80
    },
    {
      "String": "B",
      "number": 120
    }
  ]
}
1 Like

Just checking in if you were able to find something to solve this @yosiasz

UQL + JSONATA is what you needed I think. For example,

{ "data": [ 
  { "String": "A", "Number": 80 },    
  { "String": "B", "Number": 120 }
] }
parse-json
| jsonata "$map(data,function($v){{ 'S': $v.String, 'N': $v.Number, 'P': $v.Number/$sum(data[*].Number)}})"

1 Like

@adityavermamanit learn jsonata using this cool online tool

https://try.jsonata.org/zxK4bIfnD

Appreciate the reply @yesoreyeram ! This is what I wanted !