Skip to content

Instantly share code, notes, and snippets.

@Thell
Last active February 7, 2021 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thell/00818b99bbee5267c3c066c4bef30273 to your computer and use it in GitHub Desktop.
Save Thell/00818b99bbee5267c3c066c4bef30273 to your computer and use it in GitHub Desktop.
An example of unpivoting multiple columns.
let
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText(
"bcxBCoAgFATQq8hf28L5p+gM4iLJoE1FtLDbl4INhLth/rzvvYzbnLJYOc60rNlc95Gc8cu+u9Br0W3116L7AW0brBf33iZTVsMXwag1Rg5iawsGMYhBDGIQo2IlVmIlVmIlVgnhAQ==",
BinaryEncoding.Base64
),
Compression.Deflate
)
),
let
_t = ((type nullable text) meta [Serialized.Text = true])
in
type table [
Column1 = _t,
Column2 = _t,
Column3 = _t,
Column4 = _t,
Column5 = _t,
Column6 = _t
]
),
#"Changed Type" = Table.TransformColumnTypes(
Source,
{
{"Column1", type text},
{"Column2", type text},
{"Column3", type text},
{"Column4", type text},
{"Column5", type text},
{"Column6", type text}
}
),
#"Base" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars = true]),
// Type 1 and 2 attribute columns are processed independently but identically, and the common portion of each is extracted for merging.
#"UnpivotMultipleOtherColumns" = Table.Sort(
Table.ExpandTableColumn(
Table.NestedJoin(
Table.TransformColumns(
Table.UnpivotOtherColumns(
Table.SelectColumns(
#"Base",
List.Select(
Table.ColumnNames(#"Base"),
each Text.Contains(_, "type1") or Text.Contains(_, "Index")
)
),
{"Index"},
"Attribute",
"Type1 Value"
),
{{"Attribute", each Text.BetweenDelimiters(_, "[", "]")}}
),
{"Index", "Attribute"},
Table.TransformColumns(
Table.UnpivotOtherColumns(
Table.SelectColumns(
#"Base",
List.Select(
Table.ColumnNames(#"Base"),
each Text.Contains(_, "type2") or Text.Contains(_, "Index")
)
),
{"Index"},
"Attribute",
"Type2 Value"
),
{{"Attribute", each Text.BetweenDelimiters(_, "[", "]")}}
),
{"Index", "Attribute"},
"Custom",
JoinKind.LeftOuter
),
"Custom",
{"Type2 Value"},
{"Type2 Value"}
),
{{"Index", Order.Ascending}, {"Attribute", Order.Ascending}}
)
in
#"UnpivotMultipleOtherColumns"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment