Forum Discussion

pbidevcr's avatar
pbidevcr
Frequent Visitor
8 years ago
Solved

Combine Multiple Columns of Data into a Single Column Spread Out Across Rows

Hi all,    I have an excel file with multiple columns (years) and values by country. The format is like this:   Country Name Country Code 2000 2001 2002 2003 2004 2005 2006 2007 2008...
  • ChandeepChhabra's avatar
    8 years ago

    Hi pbidevcr

     

    Use the following power query code. Note that I have converted your data into a table (named country)

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Country"]}[Content],
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Country", "CODE"}, "Attribute", "Value"),
        #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Year"}})
    in
        #"Renamed Columns"

     

    The result looks like this

     

     

    You can download the solution Excel file here

     

    thanks

  • ChandeepChhabra's avatar
    ChandeepChhabra
    8 years ago

    Hi pbidevcr

     

    The code will remain the same for Power BI as well. The only change being that your source file will be an excel workbook

    That way you can keep the source file and output (PowerBi) separate

     

    Hope it helps