Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
pbidevcr
Frequent Visitor

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 NameCountry Code20002001200220032004200520062007200820092010
ArubaABW4.0440213122.8836043033.3157749913.6573765812.5299380593.3956252463.6087114595.391202648.957732188-2.1363715732.07773902
AfganistánAFG     12.686268727.2548955618.48288926830.55494061-8.2830783950.892536936
AngolaAGO324.9968716152.5610225108.897436198.223717743.5419472522.9637444413.3032533612.2486755212.4737134113.7314511414.4705412
AlbaniaALB0.0500181363.107588277.7705258340.4840026122.2800191692.3665819572.3707283192.9326824823.3592424182.2805022223.552267388

 

My question is:  how can I get all of those columns into a single “Year” column so that I can analyze the data more efficiently in Power BI? I need to spread out the values across rows.

 

Appreciate your help!

 

Best!

 

 

 

2 ACCEPTED SOLUTIONS
ChandeepChhabra
Impactful Individual
Impactful Individual

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

 

Country.PNG

 

You can download the solution Excel file here

 

thanks

View solution in original post

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

View solution in original post

4 REPLIES 4
ChandeepChhabra
Impactful Individual
Impactful Individual

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

 

Country.PNG

 

You can download the solution Excel file here

 

thanks

Hi @ChandeepChhabra

 

I tested this code in my own excel file and it worked without any issues.  Is there a way to run this in Power BI? Do you recommend to  make this in the source file instead?

 

Thank you! 

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

Hi @ChandeepChhabra,

 

There is another way to solve this challenge within Power BI. See the following video: 

 

Unpivot Column in Power BI

https://www.youtube.com/watch?v=W6UeZCte9YM

 

Thanks a lot for your support!

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.