Grafana & InfluxQL: Show relative percentages as bar labels

Dear Grafana community

I’ve been struggling the last few hours to find the best way to achieve my goal. There’s different opinions on whether to do all the work in InfluxQL vs. using Grafana’s Transformations to do the grunt work, for example. I hope to find some guidance here.

What Grafana version and what operating system are you using?

Grafana v10.4.2 (22809dea50), running on Docker

What are you trying to achieve?

I am trying to visualize player data for a game. Whenever a player loses the game, I log a data point on our InfluxDB, with two relevant fields:

  • “game_time_days”: integer
  • “session_id”: string

I do not want to change them to tags, as this would result in unbounded cardinality (thousands of possible values).

I want to display a bar chart. I want there to be one bar for each distinct value of “game_time_days”, and the bar should be labelled with the relative percentage of players who survived that long.

I have the following test data, simplified for this example:

"Time","game_time_days", "session_id"
2024-05-13 17:00:15,10,1
2024-05-13 17:13:15,6,2
2024-05-13 17:29:34,0,3
2024-05-13 17:30:10,0,4

I’ve managed to get the basic bar chart working in Grafana:

using this query:

and these transformations:

Now, I want to see the relative fraction of players for each distinct bar, rather than the absolute number of players (the 0, 0, 1, 1, 1, 1, 2, 2, 2 chart on the left). I don’t care too much about the exact visualization, anything in this direction would be perfectly fine for example:


But I’m a bit at a loss on how to get there. I tried several approaches, for example:

  • Calculate percentages with Flux | InfluxDB OSS v2 Documentation → this looked promising at first, but I first had trouble translating Flux into InfluxQL, and then realized that their example data set already included the total values
  • leveraging basic SQL knowledge to aggregate data (to inject the total row count into each row and then calculate the fraction), but I kept running into Influx’s “mixing aggregate and non-aggregate queries is not supported” limitation
  • checking existing topics here. There’s a few that seemed similiar, but turned out to be just different enough to not be applicable

After exhausting the resources available to me, I was hoping to get some good pointers from the community. Thanks for taking the time, any input is appreciated!

Maybe you could look at Business Chart pluing and do this as follows