Power Query: Help transposing/pivoting data set to correct records

Copper Contributor

I have a dataset that looks like this:

recordCodeValue
1v1S
1v223
1v3Power
2v1P
2v225
2v3Power
2v423/12/2018
3v1S
3v3Gas
3v416/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:

recordv1v2v3v4
1S23Powernull
2P25Power23/12/2018
3SnullGas16/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.

1 Reply

@SimonFA 

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

image.png