Documentation

pivot() function

Flux 0.7.0+

The pivot() function collects values stored vertically (column-wise) in a table and aligns them horizontally (row-wise) into logical sets.

pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")

The group key of the resulting table is the same as the input tables, excluding columns found in the columnKey and valueColumn parameters. This is because these columns are not part of the resulting output table.

Every input row should have a 1:1 mapping to a particular row + column in the output table, determined by its values for the rowKey and columnKey parameters. In cases where more than one value is identified for the same row + column pair, the last value encountered in the set of table rows is used as the result.

The output is constructed as follows:

  • The set of columns for the new table is the rowKey unioned with the group key, but excluding the columns indicated by the columnKey and the valueColumn.
  • A new column is added to the set of columns for each unique value identified in the input by the columnKey parameter.
  • The label of a new column is the concatenation of the values of columnKey using _ as a separator. If the value is null, "null" is used.
  • A new row is created for each unique value identified in the input by the rowKey parameter.
  • For each new row, values for group key columns stay the same, while values for new columns are determined from the input tables by the value in valueColumn at the row identified by the rowKey values and the new column’s label. If no value is found, the value is set to null.
  • Any column that is not part of the group key or not specified in the rowKey, columnKey and valueColumn parameters is dropped.

Parameters

rowKey

List of columns used to uniquely identify a row for the output.

columnKey

List of columns used to pivot values onto each row identified by the rowKey.

valueColumn

Column that contains the value to be moved around the pivot.

tables

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

Examples

Align fields within each measurement that have the same timestamp

data
  |> pivot(
    rowKey:["_time"],
    columnKey: ["_field"],
    valueColumn: "_value"
  )

View example input and output

Align fields and measurements that have the same timestamp

Note the effects of:
  • Having null values in some columnKey values.
  • Having more values for the same rowKey and columnKey value (the 11th row overrides the 10th, and so does the 15th with the 14th).
data
  |> pivot(
    rowKey:["_time"],
    columnKey: ["_measurement", "_field"],
    valueColumn: "_value"
  )

View example input and output

Align values for each tag

The following example uses data provided by the sampledata package to show how pivot() transforms data.

import "sampledata"

sampledata.int()
  |> pivot(
    rowKey: ["_time"],
    columnKey: ["tag"],
    valueColumn: "_value"
  )

View input and output


Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.