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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Steef
New Member

Converting columns to rows where one column duplicates for every row

I have the following problem. I want to transform the data from this format:

Steef_0-1599843442407.png

To this format:

Steef_2-1599843620794.png

Does anybody know how to complete this transformation in power query or DAX?

 

1 REPLY 1
STIBBS_807
Resolver I
Resolver I

This is how I would do it.  I am not a expert just a beginner.

Make your data into an Excel Table.  You can name your table whatever you like.

STIBBS_807_0-1636583340440.png

Then using the Data Tab, select From Table/Range

STIBBS_807_1-1636583437292.png

Now you have one copy of the Table.  Duplicate this in Power Query to have 4 tables.  Name they however you want.

STIBBS_807_2-1636583525767.png

Format them to have only the one set of data per table.  Here is the code:

RawData1

let
Source = Excel.CurrentWorkbook(){[Name="RawData"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date1", type datetime}, {"Date2", type datetime}, {"Date3", type datetime}, {"Date4", type datetime}, {"D1", Int64.Type}, {"D2", Int64.Type}, {"D3", Int64.Type}, {"D4", Int64.Type}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"ID", "Date1", "D1"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{"Date1", "Dates"}, {"D1", "D"}})
in
#"Renamed Columns"

RawData2

let
Source = Excel.CurrentWorkbook(){[Name="RawData"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Date1", type datetime}, {"Date2", type datetime}, {"Date3", type datetime}, {"Date4", type datetime}, {"D1", Int64.Type}, {"D2", Int64.Type}, {"D3", Int64.Type}, {"D4", Int64.Type}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"ID", "Date2", "D2"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{"Date2", "Dates"}, {"D2", "D"}})
in
#"Renamed Columns"

ETC....

Then Append to New to get your data:

let
Source = Table.Combine({RawData1, RawData2, RawData3, RawData4}),
#"Sorted Rows" = Table.Sort(Source,{{"ID", Order.Ascending}})
in
#"Sorted Rows"

Close and Load the Power Query Editor.

 

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.

Top Solution Authors