Transform Data with Power Query

johnmpl

Board Regular
Joined
Jun 14, 2013
Messages
237
Office Version
  1. 365
Platform
  1. Windows
Hi to all!

This time i want to transform data, using Power Query, from this:

*ABC
BarcelonaValencia
ValenciaBarcelona
MadridRoma
RomaMadrid
BarcelonaMadrid
MadridBarcelona
BarcelonaValencia
ValenciaBarcelona
MadridRoma
RomaMadrid
BarcelonaRoma
RomaBarcelona

<tbody>
[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="align: center"]Company[/TD]
[TD="align: center"]Origin[/TD]
[TD="align: center"]Destiny[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="align: center"]AA[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]
[TD="align: center"]AA[/TD]

[TD="bgcolor: #cacaca, align: center"]6[/TD]
[TD="align: center"]AA[/TD]

[TD="bgcolor: #cacaca, align: center"]7[/TD]
[TD="align: center"]AA[/TD]

[TD="bgcolor: #cacaca, align: center"]8[/TD]
[TD="align: center"]AA[/TD]

[TD="bgcolor: #cacaca, align: center"]9[/TD]
[TD="align: center"]AA[/TD]

[TD="bgcolor: #cacaca, align: center"]10[/TD]
[TD="align: center"]AB[/TD]

[TD="bgcolor: #cacaca, align: center"]11[/TD]
[TD="align: center"]AB[/TD]

[TD="bgcolor: #cacaca, align: center"]12[/TD]
[TD="align: center"]AC[/TD]

[TD="bgcolor: #cacaca, align: center"]13[/TD]
[TD="align: center"]AC[/TD]

[TD="bgcolor: #cacaca, align: center"]14[/TD]
[TD="align: center"]AC[/TD]

[TD="bgcolor: #cacaca, align: center"]15[/TD]
[TD="align: center"]AC[/TD]

</tbody>

Into this:

*EFGHI

<tbody>
[TD="bgcolor: #cacaca, align: center"]3[/TD]
[TD="bgcolor: #c0c0c0, align: center"]Cities[/TD]
[TD="bgcolor: #c0c0c0, align: center"]No. Of Destinies[/TD]
[TD="bgcolor: #c0c0c0, align: center"]Destiny 1[/TD]
[TD="bgcolor: #c0c0c0, align: center"]Destiny 2[/TD]
[TD="bgcolor: #c0c0c0, align: center"]Destiny 3[/TD]

[TD="bgcolor: #cacaca, align: center"]4[/TD]
[TD="align: center"]Barcelona[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]Valencia[/TD]
[TD="align: center"]Madrid[/TD]
[TD="align: center"]Roma[/TD]

[TD="bgcolor: #cacaca, align: center"]5[/TD]
[TD="align: center"]Valencia[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]Barcelona[/TD]

[TD="align: center"][/TD]

[TD="bgcolor: #cacaca, align: center"]6[/TD]
[TD="align: center"]Madrid[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]Roma[/TD]
[TD="align: center"]Barcelona[/TD]

[TD="bgcolor: #cacaca, align: center"]7[/TD]
[TD="align: center"]Roma[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]Madrid[/TD]
[TD="align: center"]Barcelona[/TD]

</tbody>


Excel tables to the web >> Excel Jeanie HTML 4


I already know how to do it with formulas and vba. But I wanna know how can I get this result with Power Query. Thx in advanced for your answers. Blessings!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi johnmpl,
Try this code below
Code:
let
    Source =  Table.FromRows({{"AA","Barcelona","Valencia"},{"AA","Valencia","Barcelona"},{"AA","Madrid","Roma"},{"AA","Roma","Madrid"},{"AA","Barcelona","Madrid"},{"AA","Madrid","Barcelona"},{"AB","Barcelona","Valencia"},{"AB","Valencia","Barcelona"},{"AC","Madrid","Roma"},{"AC","Roma","Madrid"},{"AC","Barcelona","Roma"},{"AC","Roma","Barcelona"}},  {"Company", "Origin", "Destiny"}),
    #"Changed Type" =  Table.TransformColumnTypes(Source,{{"Company", type text}, {"Origin",  type text}, {"Destiny", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Company"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns"),
     #"Grouped Rows" = Table.Group(#"Removed Duplicates", {"Origin"}, {{"How  many", each Table.RowCount(_), type number}, {"records", each  Record.FromList(_[Destiny], List.Transform({1..Table.RowCount(_)}, each  "Destiny " & Text.From(_))), type record}}),
    #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"How many", Order.Descending}}),
    LstOfFieldNames = Record.FieldNames( #"Sorted Rows"{0}[records]),
    #"Expand Record Column" = Table.ExpandRecordColumn(#"Sorted Rows", "records", LstOfFieldNames, LstOfFieldNames)
in
    #"Expand Record Column"

Regards
 
Upvote 0
Nice Billszysz! Excellent Solution. I have troubles with this line:

#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"How many", Order.Descending}}),
But I erase that, and worked like a charm. Thx and Blessings!
 
Upvote 0
Hello,

Your question is more popular now. I've made video trick how to pass this.
If you've interested with another solution with an example then visit here.



Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Removed Duplicates" = Table.Distinct(Source),
    #"Grouped Rows" = Table.Group(#"Removed Duplicates", {"Origin"}, {{"GrBy", each Table.Transpose(Table.FromList([Destiny]))}}),
    MainTable = Table.AddColumn(#"Grouped Rows", "No. Of Destinies", each Table.ColumnCount([GrBy])),
    ListDistinct = List.Distinct(List.Combine(Table.AddColumn(MainTable, "ColNames", each Table.ColumnNames([GrBy]))[ColNames])),
    Finish = Table.ExpandTableColumn(MainTable,"GrBy",ListDistinct),
    #"Converted to Table" = Table.FromList(ListDistinct, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    DestinColNam = Table.AddColumn(#"Converted to Table", "Custom", each Text.Replace([Column1],"Column","Destiny")),
    ColNamList = Table.ToRows(DestinColNam),
    Custom2 = Table.RenameColumns(Finish,ColNamList),
    #"Reordered Columns" = Table.ReorderColumns(Custom2,List.InsertRange(DestinColNam[Custom],0,{"Origin", "No. Of Destinies"}))
in
    #"Reordered Columns"
 
Upvote 0

Forum statistics

Threads
1,225,478
Messages
6,185,228
Members
453,283
Latest member
Shortm88

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