Back to Influxdb

pivot() function

content/flux/v0/stdlib/universe/pivot.md

latest5.7 KB
Original Source
<!------------------------------------------------------------------------------ IMPORTANT: This page was generated from comments in the Flux source code. Any edits made directly to this page will be overwritten the next time the documentation is generated. To make updates to this documentation, update the function comments above the function definition in the Flux source code: https://github.com/influxdata/flux/blob/master/stdlib/universe/universe.flux#L2047-L2055 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

pivot() collects unique values stored vertically (column-wise) and aligns them horizontally (row-wise) into logical sets.

Output data

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

Every input row should have a 1:1 mapping to a particular row and column combination in the output table. Row and column combinations are determined by the rowKey and columnKey parameters. In cases where more than one value is identified for the same row and 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 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 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.
Function type signature
js
(<-tables: stream[A], columnKey: [string], rowKey: [string], valueColumn: string) => stream[B] where A: Record, B: Record

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

rowKey

({{< req >}}) Columns to use to uniquely identify an output row.

columnKey

({{< req >}}) Columns to use to identify new output columns.

valueColumn

({{< req >}}) Column to use to populate the value of pivoted columnKey columns.

tables

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

Examples

Align fields into rows based on time

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

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time*_measurement*_field_value
1970-01-01T00:00:01Zm1f11
1970-01-01T00:00:01Zm1f22
1970-01-01T00:00:01Zm1f3
1970-01-01T00:00:02Zm1f14
1970-01-01T00:00:02Zm1f25
1970-01-01T00:00:02Zm1f36
1970-01-01T00:00:03Zm1f1
1970-01-01T00:00:03Zm1f27
1970-01-01T00:00:04Zm1f38

Output data

_time*_measurementf1f2f3
1970-01-01T00:00:01Zm112
1970-01-01T00:00:02Zm1456
1970-01-01T00:00:03Zm17
1970-01-01T00:00:04Zm18

{{% /expand %}} {{< /expand-wrapper >}}

Associate values to tags by time

js
import "sampledata"

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

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Z10t1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Z17t1
2021-01-01T00:00:40Z15t1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Z19t2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Z13t2
2021-01-01T00:00:50Z1t2

Output data

_timet1t2
2021-01-01T00:00:00Z-219
2021-01-01T00:00:10Z104
2021-01-01T00:00:20Z7-3
2021-01-01T00:00:30Z1719
2021-01-01T00:00:40Z1513
2021-01-01T00:00:50Z41

{{% /expand %}} {{< /expand-wrapper >}}