Flatten Table with Power Query

ilya2004

Board Regular
Joined
Mar 17, 2011
Messages
135
Hello,

I have a table whose data looks like this:

Salesman NameTablesLaddersChairs
John
5
John10
John9

<tbody>
</tbody>

I would like to flatten it so that the table reads like the below and remove the repetition of the key column.

Salesman NameTablesLaddersChairs
John5109

<tbody>
</tbody>


Thank you!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I should add that there are multiple columns to the left of the data which is being flattened that are redundant, but I can flatten by the uniqueID in the first column.
 
Upvote 0
That doesn't work because the blanks were created as the result of a pivot, when I unpivot it, the blanks go away on its own, but when you pivot, they come back.
 
Upvote 0
Huh, I thought that would work.

Here is what I did:

Code:
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUTIFYhCK1YELAZGhAaYYEFkqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Sales Person" = _t, Tables = _t, Ladders = _t, Chairs = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales Person", type text}, {"Tables", Int64.Type}, {"Ladders", Int64.Type}, {"Chairs", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Sales Person"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"

Returns the result I expected. Didn't even need to remove blanks.
 
Upvote 0
For me, it just produces a flat column that looks like this:

John Tables 5
John Ladders 10
John Chairs 9

Which is how the data started out to begin with.

My goal is to have Tables, Ladders and chairs as columns and that each unique ID should not get more than one row on the table.
 
Upvote 0
Really strange. You copied my code directly into a new query? Do we have different versions of Power BI? How can we get a different result from the same query?
 
Upvote 0
For me, it just produces a flat column that looks like this:

John Tables 5
John Ladders 10
John Chairs 9

Actually, if you can get this far, why can't you just pivot the data by the middle column, using the third column as the values and select don't aggregate under advanced?
 
Upvote 0
My solution:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Salesman Name"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"
 
Upvote 0
Alternatively you can Group By Salesman, sum the values and take care of flattening the columns to te left you mentioned in post number 2.

Code:
let
    Source = Table1,
    #"Grouped Rows" = Table.Group(Source, {"Salesman Name"}, {{"Tables", each List.Sum([Tables]), type number}, {"Ladders", each List.Sum([Ladders]), type number}, {"Chairs", each List.Sum([Chairs]), type number}})
in
    #"Grouped Rows"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,061
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top