Calculate percentages in a query

Use Flux or InfluxQL to calculate percentages in a query.

Flux InfluxQL

Flux lets you perform simple math equations, for example, calculating a percentage.

Calculate a percentage

Learn how to calculate a percentage using the following examples:

Basic calculations within a query

When performing any math operation in a Flux query, you must complete the following steps:

  1. Specify the bucket to query from and the time range to query.
  2. Filter your data by measurements, fields, and other applicable criteria.
  3. Align values in one row (required to perform math in Flux) by using one of the following functions:

For examples using the join() function to calculate percentages and more examples of calculating percentages, see Calculate percentages with Flux.

Data variable

To shorten examples, we’ll store a basic Flux query in a data variable for reuse.

Here’s how that looks in Flux:

  1. // Query data from the past 15 minutes pivot fields into columns so each row
  2. // contains values for each field
  3. data = from(bucket:"your_db/your_retention_policy")
  4. |> range(start: -15m)
  5. |> filter(fn: (r) => r._measurement == "measurement_name" and r._field =~ /field[1-2]/)
  6. |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")

Each row now contains the values necessary to perform a math operation. For example, to add two field keys, start with the data variable created above, and then use map() to re-map values in each row.

  1. data
  2. |> map(fn: (r) => ({ r with _value: r.field1 + r.field2}))

Note: Flux supports basic math operators such as +,-,/, *, and (). For example, to subtract field2 from field1, change + to -.

Calculate a percentage from two fields

Use the data variable created above, and then use the map() function to divide one field by another, multiply by 100, and add a new percent field to store the percentage values in.

  1. data
  2. |> map(fn: (r) => ({
  3. _time: r._time,
  4. _measurement: r._measurement,
  5. _field: "percent",
  6. _value: field1 / field2 * 100.0
  7. }))

Note: In this example, field1 and field2 are float values, hence multiplied by 100.0. For integer values, multiply by 100 or use the float() function to cast integers to floats.

Calculate a percentage using aggregate functions

Use aggregateWindow() to window data by time and perform an aggregate function on each window.

  1. from(bucket:"<database>/<retention_policy>")
  2. |> range(start: -15m)
  3. |> filter(fn: (r) => r._measurement == "measurement_name" and r._field =~ /fieldkey[1-2]/)
  4. |> aggregateWindow(every: 1m, fn:sum)
  5. |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  6. |> map(fn: (r) => ({ r with _value: r.field1 / r.field2 * 100.0 }))

Calculate the percentage of total weight per apple variety

Use simulated apple stand data to track the weight of apples (by type) throughout a day.

  1. Download the sample data
  2. Import the sample data:

    1. influx -import -path=path/to/apple_stand.txt -precision=ns -database=apple_stand

Use the following query to calculate the percentage of the total weight each variety accounts for at each given point in time.

  1. from(bucket:"apple_stand/autogen")
  2. |> range(start: 2018-06-18T12:00:00Z, stop: 2018-06-19T04:35:00Z)
  3. |> filter(fn: (r) => r._measurement == "variety")
  4. |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  5. |> map(fn: (r) => ({ r with
  6. granny_smith: r.granny_smith / r.total_weight * 100.0 ,
  7. golden_delicious: r.golden_delicious / r.total_weight * 100.0 ,
  8. fuji: r.fuji / r.total_weight * 100.0 ,
  9. gala: r.gala / r.total_weight * 100.0 ,
  10. braeburn: r.braeburn / r.total_weight * 100.0 ,}))

Calculate the average percentage of total weight per variety each hour

With the apple stand data from the prior example, use the following query to calculate the average percentage of the total weight each variety accounts for per hour.

  1. from(bucket:"apple_stand/autogen")
  2. |> range(start: 2018-06-18T00:00:00.00Z, stop: 2018-06-19T16:35:00.00Z)
  3. |> filter(fn: (r) => r._measurement == "variety")
  4. |> aggregateWindow(every:1h, fn: mean)
  5. |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  6. |> map(fn: (r) => ({ r with
  7. granny_smith: r.granny_smith / r.total_weight * 100.0,
  8. golden_delicious: r.golden_delicious / r.total_weight * 100.0,
  9. fuji: r.fuji / r.total_weight * 100.0,
  10. gala: r.gala / r.total_weight * 100.0,
  11. braeburn: r.braeburn / r.total_weight * 100.0
  12. }))

InfluxQL lets you perform simple math equations which makes calculating percentages using two fields in a measurement pretty simple. However there are some caveats of which you need to be aware.

Basic calculations within a query

SELECT statements support the use of basic math operators such as +,-,/, *, (), etc.

  1. -- Add two field keys
  2. SELECT field_key1 + field_key2 AS "field_key_sum" FROM "measurement_name" WHERE time < now() - 15m
  3. -- Subtract one field from another
  4. SELECT field_key1 - field_key2 AS "field_key_difference" FROM "measurement_name" WHERE time < now() - 15m
  5. -- Grouping and chaining mathematical calculations
  6. SELECT (field_key1 + field_key2) - (field_key3 + field_key4) AS "some_calculation" FROM "measurement_name" WHERE time < now() - 15m

Calculating a percentage in a query

Using basic math functions, you can calculate a percentage by dividing one field value by another and multiplying the result by 100:

  1. SELECT (field_key1 / field_key2) * 100 AS "calculated_percentage" FROM "measurement_name" WHERE time < now() - 15m

Calculating a percentage using aggregate functions

If using aggregate functions in your percentage calculation, all data must be referenced using aggregate functions. You can’t mix aggregate and non-aggregate data.

All Aggregate functions need a GROUP BY time() clause defining the time intervals in which data points are grouped and aggregated.

  1. SELECT (sum(field_key1) / sum(field_key2)) * 100 AS "calculated_percentage" FROM "measurement_name" WHERE time < now() - 15m GROUP BY time(1m)

Examples

Sample data

The following example uses simulated Apple Stand data that tracks the weight of baskets containing different varieties of apples throughout a day of business.

  1. Download the sample data
  2. Import the sample data:

    1. influx -import -path=path/to/apple_stand.txt -precision=ns -database=apple_stand

Calculating percentage of total weight per apple variety

The following query calculates the percentage of the total weight each variety accounts for at each given point in time.

  1. SELECT
  2. ("braeburn"/total_weight)*100,
  3. ("granny_smith"/total_weight)*100,
  4. ("golden_delicious"/total_weight)*100,
  5. ("fuji"/total_weight)*100,
  6. ("gala"/total_weight)*100
  7. FROM "apple_stand"."autogen"."variety"

\*

If visualized as a stacked graph in Chronograf, it would look like:

Percentage of total per apple variety

Calculating aggregate percentage per variety

The following query calculates the average percentage of the total weight each variety accounts for per hour.

  1. SELECT
  2. (mean("braeburn")/mean(total_weight))*100,
  3. (mean("granny_smith")/mean(total_weight))*100,
  4. (mean("golden_delicious")/mean(total_weight))*100,
  5. (mean("fuji")/mean(total_weight))*100,
  6. (mean("gala")/mean(total_weight))*100
  7. FROM "apple_stand"."autogen"."variety"
  8. WHERE time >= '2018-06-18T12:00:00Z' AND time <= '2018-06-19T04:35:00Z'
  9. GROUP BY time(1h)

Note the following about this query:

  • It uses aggregate functions (mean()) for pulling all data.
  • It includes a GROUP BY time() clause which aggregates data into 1 hour blocks.
  • It includes an explicitly limited time window. Without it, aggregate functions are very resource-intensive.

If visualized as a stacked graph in Chronograf, it would look like:

Hourly average percentage of total per apple variety