Forum Discussion
Unpivot paired columns
- 5 years ago
Hello DDON
you can add a new column with this formula, delete all your S and F-fields and then expand the list to rows and then expand the record
let CreateNumber = List.Numbers(0,3), TransformToValuesFS = List.Transform ( CreateNumber, (item)=> [F= try Record.Field(_, "F[" & Text.From(item) & "]") otherwise null , S= try Record.Field(_, "S[" & Text.From(item) & "]") otherwise null] ), CleanNulls = List.Select(TransformToValuesFS, each _[F] <> null and _[S]<>null) in CleanNullsThis is quite complex you have to pay attention on the following
Adapt this number to your maximum numbers of pairs --> CreateNumber = List.Numbers(0,3)
Adapt this text according your standard naming of your F and S-columns
(item)=> [F= try Record.Field(_, "F[" & Text.From(item) & "]") otherwise null , S= try Record.Field(_, "S[" & Text.From(item) & "]") otherwise null]
)Here the complete code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjLWMzDSMzJU0lFKTEoGkiCWERAbA7GJUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateTime = _t, IMEI = _t, #"F[0]" = _t, #"F[1]" = _t, #"S[0]" = _t, #"S[1]" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateTime", type date}, {"IMEI", type text}, {"F[0]", Int64.Type}, {"F[1]", Int64.Type}, {"S[1]", Int64.Type}, {"S[0]", Int64.Type}}, "de-DE"), AddPairs = Table.AddColumn(#"Changed Type", "Pairs", each let CreateNumber = List.Numbers(0,3), TransformToValuesFS = List.Transform ( CreateNumber, (item)=> [F= try Record.Field(_, "F[" & Text.From(item) & "]") otherwise null , S= try Record.Field(_, "S[" & Text.From(item) & "]") otherwise null] ), CleanNulls = List.Select(TransformToValuesFS, each _[F] <> null and _[S]<>null) in CleanNulls), #"Removed Other Columns" = Table.SelectColumns(AddPairs,{"DateTime", "IMEI", "Pairs"}), #"Expanded Pairs" = Table.ExpandListColumn(#"Removed Other Columns", "Pairs"), #"Expanded Pairs1" = Table.ExpandRecordColumn(#"Expanded Pairs", "Pairs", {"F", "S"}, {"F", "S"}) in #"Expanded Pairs1"this transforms this
into this
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hello DDON
you can add a new column with this formula, delete all your S and F-fields and then expand the list to rows and then expand the record
let
CreateNumber = List.Numbers(0,3),
TransformToValuesFS = List.Transform
(
CreateNumber,
(item)=> [F= try Record.Field(_, "F[" & Text.From(item) & "]") otherwise null , S= try Record.Field(_, "S[" & Text.From(item) & "]") otherwise null]
),
CleanNulls = List.Select(TransformToValuesFS, each _[F] <> null and _[S]<>null)
in
CleanNulls
This is quite complex you have to pay attention on the following
Adapt this number to your maximum numbers of pairs --> CreateNumber = List.Numbers(0,3)
Adapt this text according your standard naming of your F and S-columns
(item)=> [F= try Record.Field(_, "F[" & Text.From(item) & "]") otherwise null , S= try Record.Field(_, "S[" & Text.From(item) & "]") otherwise null]
)
Here the complete code
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjLWMzDSMzJU0lFKTEoGkiCWERAbA7GJUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateTime = _t, IMEI = _t, #"F[0]" = _t, #"F[1]" = _t, #"S[0]" = _t, #"S[1]" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"DateTime", type date}, {"IMEI", type text}, {"F[0]", Int64.Type}, {"F[1]", Int64.Type}, {"S[1]", Int64.Type}, {"S[0]", Int64.Type}}, "de-DE"),
AddPairs = Table.AddColumn(#"Changed Type", "Pairs", each let
CreateNumber = List.Numbers(0,3),
TransformToValuesFS = List.Transform
(
CreateNumber,
(item)=> [F= try Record.Field(_, "F[" & Text.From(item) & "]") otherwise null , S= try Record.Field(_, "S[" & Text.From(item) & "]") otherwise null]
),
CleanNulls = List.Select(TransformToValuesFS, each _[F] <> null and _[S]<>null)
in
CleanNulls),
#"Removed Other Columns" = Table.SelectColumns(AddPairs,{"DateTime", "IMEI", "Pairs"}),
#"Expanded Pairs" = Table.ExpandListColumn(#"Removed Other Columns", "Pairs"),
#"Expanded Pairs1" = Table.ExpandRecordColumn(#"Expanded Pairs", "Pairs", {"F", "S"}, {"F", "S"})
in
#"Expanded Pairs1"
this transforms this
into this
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Thank for sharing. I have just used your solution to help on another forum