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
oliviaholmes
New Member

Merge/Consolidate Rows with Cascading Data

I need some help figuring out what to do for this data. I currently have 

 

Project IDProject NameFirst YearDiscountLong Term PlanProductionExampleExampleExample Example
1Project 12023nullnullnullnullnullnullnull
1Project 1 null10%nullnullnullnullnullnull
1Project 1nullnull.02nullnullnullnullnull

 

This data is multiple Projects and has about 75 Columns where data follows this pattern 

 

And I would Like to have 

 

Project IDProject NameFirst YearDiscountLong Term PlanProductionExampleExampleExample Example
1Project 1202310%.021005.2511
2Project 220185%.021500022.011
          

 

What can I do in Power Query to flatten this data? 

1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community Champion

Hi @oliviaholmes,

You can do an unpivot operation (this will remove any null values) and then re-pivot the data.
Have a look at the following code:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQooys9KTS5RALFBGIRidTDlQNKGeCTB8oZgWSMkWRDbCMlYdDmYNA45kLSRkVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project ID" = _t, #"Project Name" = _t, Number1 = _t, Number2 = _t, Number3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project ID", type text}, {"Project Name", type text}, {"Number1", Int64.Type}, {"Number2", Int64.Type}, {"Number3", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project ID", "Project Name"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"

 


Before:

SpartaBI_0-1656428425676.png

After:

SpartaBI_1-1656428441606.png

 

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

View solution in original post

3 REPLIES 3
SpartaBI
Community Champion
Community Champion

Hi @oliviaholmes,

1. Start from your initial table.

SpartaBI_0-1656432648031.png

2. Select the Project ID & Project Name columns and then right-click and choose "unpivot other columns". 

SpartaBI_1-1656432762375.png
You should get:

SpartaBI_2-1656433032929.png

3. Select the "Attribute" column and then choose "Pivot Column".

SpartaBI_3-1656433163620.png

4. Fill the following:

SpartaBI_4-1656433203560.png
You should get:

SpartaBI_5-1656433250738.png

 

 

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

SpartaBI
Community Champion
Community Champion

Hi @oliviaholmes,

You can do an unpivot operation (this will remove any null values) and then re-pivot the data.
Have a look at the following code:

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQooys9KTS5RALFBGIRidTDlQNKGeCTB8oZgWSMkWRDbCMlYdDmYNA45kLSRkVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project ID" = _t, #"Project Name" = _t, Number1 = _t, Number2 = _t, Number3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project ID", type text}, {"Project Name", type text}, {"Number1", Int64.Type}, {"Number2", Int64.Type}, {"Number3", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project ID", "Project Name"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"

 


Before:

SpartaBI_0-1656428425676.png

After:

SpartaBI_1-1656428441606.png

 

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

This is not working for me. When i use the code, it shows an error for all columns. Can you do a step walkthrough so I can mirror your actions @SpartaBI

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