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
3CompanyOriginDestiny
4AABarcelonaValencia
5AAValenciaBarcelona
6AAMadridRoma
7AARomaMadrid
8AABarcelonaMadrid
9AAMadridBarcelona
10ABBarcelonaValencia
11ABValenciaBarcelona
12ACMadridRoma
13ACRomaMadrid
14ACBarcelonaRoma
15ACRomaBarcelona

<tbody>
</tbody>

Into this:

*EFGHI
3CitiesNo. Of DestiniesDestiny 1Destiny 2Destiny 3
4Barcelona3ValenciaMadridRoma
5Valencia1Barcelona
6Madrid2RomaBarcelona
7Roma2MadridBarcelona

<tbody>
</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 does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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,216,106
Messages
6,128,863
Members
449,473
Latest member
soumyahalder4

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