keyValues() function

The keyValues() function returns a table with the input table’s group key plus two columns, _key and _value, that correspond to unique column and value pairs for specific columns in each input table.

keyValues(keyColumns: ["usage_idle", "usage_user"])



A list of columns from which values are extracted. All columns indicated must be of the same type. Each input table must have all of the columns listed by the keyColumns parameter.


Input data. Default is piped-forward data (<-).


The following examples use to simulate data queried from InfluxDB and illustrate how keys() transforms data.

Get key values from explicitly defined columns

import "influxdata/influxdb/sample"

data = "airSensor")
    |> filter(fn: (r) => r.sensor_id == "TLM0100")

    |> keyValues(keyColumns: ["sensor_id", "_field"])

View example input and output

Get key values from all group key columns

  1. Use keys() and findColumn() to extract an array of group key columns and store it as a variable.
  2. Use keyValues() and provide the group key column array variable to the keyColumns parameter.
import "influxdata/influxdb/sample"

data = "airSensor")
    |> filter(fn: (r) => r.sensor_id == "TLM0100")

keyColumns = data
    |> keys()
    |> findColumn(fn: (key) => true, column: "_value")
// Returns [_field, _measurement, sensor_id]

    |> keyValues(keyColumns: keyColumns)

View example input and output

Was this page helpful?

Thank you for your feedback!

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.