Nov 27 2019 02:33 PM
I have a dataset that looks like this:
record | Code | Value |
1 | v1 | S |
1 | v2 | 23 |
1 | v3 | Power |
2 | v1 | P |
2 | v2 | 25 |
2 | v3 | Power |
2 | v4 | 23/12/2018 |
3 | v1 | S |
3 | v3 | Gas |
3 | v4 | 16/04/2018 |
This is three records (as per column 1 - record), 4 columns (v1 to 4 in column 2 - Code) and the value for that column/record (column 3 Value)
The output should look like this:
record | v1 | v2 | v3 | v4 |
1 | S | 23 | Power | null |
2 | P | 25 | Power | 23/12/2018 |
3 | S | null | Gas | 16/04/2018 |
I need to make this transformation in Power Query as the record set is large and frequently changes. I know how to load it into query but can't workout how to make a distinct list from [Code] as column headers and then apply the [value] as the rows under those columns. Note, that not every column appears in each record so can't just assume the first value in the record is v1 and the second is v2.
Thanks in advance for your assistance.
Nov 27 2019 03:05 PM
With pivoting that will be done by default, generated script is
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(
Source,
{
{"record", Int64.Type},
{"Code", type text},
{"Value", type any}
}
),
#"Pivoted Column" = Table.Pivot(
#"Changed Type",
List.Distinct(#"Changed Type"[Code]),
"Code",
"Value")
in
#"Pivoted Column"
for