Forum Discussion

jeffrey759's avatar
jeffrey759
Regular Visitor
8 years ago
Solved

Combine multiple columns

Hello,   I have multiple Vicinity's with a rating (number), but every Vicinity and rating exists as a new column. For this, I would like to have: 1 Column with the Vicinity and 1 column with the c...
  • ChandeepChhabra's avatar
    8 years ago

    Hi jeffrey759

     

    Try the following code. Note that I have converted your data into a table (named vicinity) in Excel and wrote PQ code there

     

     

    let
    Source = Excel.CurrentWorkbook(){[Name="Vicinity"]}[Content],
    #"Transposed Table" = Table.Transpose(Source),
    #"Added Index" = Table.AddIndexColumn(#"Transposed Table", "Index", 1, 1),
    #"Calculated Modulo" = Table.TransformColumns(#"Added Index", {{"Index", each Number.Mod(_, 2), type number}}),
    #"Added Conditional Column" = Table.AddColumn(#"Calculated Modulo", "Custom", each if [Index] = 0 then [Column1] else null ),
    #"Filled Up" = Table.FillUp(#"Added Conditional Column",{"Custom"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each ([Index] = 1)),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows",{{"Column1", "Vicinity"}, {"Custom", "Rating"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Index"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Rating", type text}, {"Vicinity", type text}})
    in
    #"Changed Type"

     

    Here is the snapshot of ther result


     

     

     

     

     

     

     

     

     

     

    You can download my excel (powerquery) file here

     

    thanks

     

  • v-xjiin-msft's avatar
    v-xjiin-msft
    8 years ago

    jeffrey759

     

    Please refer to my sample data screenshot. The column name format in my smaple data is like: Vicinity.0, Rating.0 and Vicinity.1, Rating.1 and so on. So that I can know which rating belonds to which vicinity. Also, I have a group column called Item. With this if there exists multiple groups, we can know which vicinities or ratings are in the same row.

     

    Thereby, in your scenario, your column names are like Vicinity, Vicinity_1, Rating_2 ... It is hard to know which rating belongs to which vicinity. Please format your sample data first by renaming the column names. And consider if it is necessary to add a group column.

     

    Thanks,
    Xi Jin.