Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

D toc
Excerpt

Reshapes the data by merging one or more columns into key and value columns. Keys are the names of input columns, and value columns are the cell values from the source. 

Rows of data are duplicated, once for each input column. 

 The unpivot column can be applied to multiple columns in the same transform. All columns are un-pivoted into the same key and value columns. When this transform is applied to two columns, the number of rows in the dataset is doubled.

This transform is the opposite of the  pivot  transform, which converts a set of column values into distinct columns. See  Pivot Transform.

D s
snippetBasic

Single- or multi-column example:

You can specify single columns or comma-separated sets of columns. 

D code

unpivot col: FirstName, MiddleInitial

Output: Converts the values in the columns FirstName and MiddleInitial into separate key and value columns.

Column range example: 

You can also specify ranges of columns using the tilde (~) operator:

D code

unpivot col:Column1~Column20

Output: Converts all of the values in columns between Column1 and Column20 into key and value columns.

D s
snippetParameters

D code
unpivot col: column_ref [groupEvery: int_num]
TokenRequired?Data TypeDescription
unpivotYtransformName of the transform
colYstringName of source column or columns
groupEveryNstringIf specified, this parameter defines the number of individual key-value pairs to store in each generated column. Default is 1.

D s lang notes

col

Identifies the column or columns to which to apply the transform. You can specify one or more columns.

Include Page
col_multi Parameter
col_multi Parameter

D s
snippetusage

Required?Data Type
YesString (column name)

groupEvery

Include Page
groupEvery Parameter
groupEvery Parameter

D s
snippetusage

Required?Data Type
NoInteger (positive)


D s
snippetExamples

Example - Basic Unpivot

Source:

productNameproductColorproductSize
WhizbangredM
Whizbangred, blueL
WhizbanggreenM
BangwhizredS
BangwhizblueM
BangwhizredS

Tranform:

After you have created a header, if necessary, add the following transform:

D code

unpivot col:productColor

Results:

productNameproductSizekeyvalue
WhizbangMproductColorred
WhizbangLproductColorred, blue
WhizbangMproductColorgreen
BangwhizSproductColorred
BangwhizMproductColorblue
BangwhizSproductColorred

Extended:

Note how each instance of a value results in a separate row; duplicate values are included. For a single-column unpivot, this transform results in the same number of rows as the source.

  • Since the value is treated as a string, the value red, blue is treated as one value.

Now, edit the transform you just added. Replace it with the following, which includes the productSize key as part of the transform:

D code

unpivot col:productColor,productSize

Results:

productNamekeyvalue
WhizbangproductColorred
WhizbangproductSizeM
WhizbangproductColorred, blue
WhizbangproductSizeL
WhizbangproductColorgreen
WhizbangproductSizeM
BangwhizproductColorred
BangwhizproductSizeS
BangwhizproductColorblue
BangwhizproductSizeM
BangwhizproductColorred
BangwhizproductSizeS

Row keys alternate based on the order in which the source columns are specified in the transform. Since the transform specifies two columns, the number of key-value pairs is doubled, which results in a dataset that has twice as many rows as the source.

Example - Basic Pivot with groupEvery

From the previous example, modify the unpivot transform to be the following:

D code

unpivot col:productColor,productSize groupEvery:2

Results:

productNamekey1value1key2value2
WhizbangproductColorredproductSizeM
WhizbangproductColorred, blueproductSizeL
WhizbangproductColorgreenproductSizeM
BangwhizproductColorredproductSizeS
BangwhizproductColorblueproductSizeM
BangwhizproductColorredproductSizeS


D s also
labelwrangle_transform_unpivot