Unpivot and Arrange the data in Sheet1

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Actuallly it is not really unpivot, rather take a set of columns, take another set of columns and glue the 2 parts together.

More detail:

I renamed the columns in the first part (remove "First ") using function Table.TransformColumnNames, which is - I guess - not a well known function as it is never used in code that is generated from the Query Editor options.

I renamed the columns of the second part by simply replacing the table type (i.e. the column names, their types and any table keys) with the table typ from part 1.
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1418"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Bus Driver ID", Int64.Type}, {"Bus Driver Name", type text}, {"Mobile No. 1", Int64.Type}, {"Mobile No. 2", Int64.Type}, {"Bus No.", type text}, {"Bus Plate No.", Int64.Type}, {"Bus Type", type text}, {"Seats", Int64.Type}, {"First Trip ID", type text}, {"First School Name", type text}, {"Students Gender", type text}, {"First Bus Assistant ID", Int64.Type}, {"First Bus Assistant Name", type text}, {"First Bus Assistant Mobile", Int64.Type}, {"Second Trip ID", type text}, {"Second School Name", type text}, {"Students Gender2", type text}, {"Second Bus Assistant ID", Int64.Type}, {"Second Bus Assistant Name", type text}, {"Second Bus Assistant Mobile", Int64.Type}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"Bus Driver ID", "Bus Driver Name", "Mobile No. 1", "Mobile No. 2", "Bus No.", "Bus Plate No.", "Bus Type", "Seats", "First Trip ID", "First School Name", "Students Gender", "First Bus Assistant ID", "First Bus Assistant Name", "First Bus Assistant Mobile"}),
    Part1 = Table.TransformColumnNames(#"Removed Other Columns", each Text.Replace(_, "First ","")),

    // Notice that the next step proceeds again from step #"Changed Type:
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"First Trip ID", "First School Name", "Students Gender", "First Bus Assistant ID", "First Bus Assistant Name", "First Bus Assistant Mobile"}),
    Part2 = Value.ReplaceType(#"Removed Columns",Value.Type(Part1)),
    #"Parts Combined" = Part1 & Part2,
    #"Filtered Rows" = Table.SelectRows(#"Parts Combined", each ([Trip ID] <> null))
in
    #"Filtered Rows"
 
Last edited:
Upvote 0
I think it can be done easily with excel formula but I want it to be done with Power Query..
 
Upvote 0
It can only be done if you know which rows are for the first trip and which rows are for the second trip. so you need an additional column.
And if you want to do it in the opposite way, then you can't use Trip ID as key.
Below the query for the opposite way, where a Part column ("First", "Second") was added and Bus Driver ID was used as key column.
Code:
let
    Source = ResultExtra,
    CommonColumns = Table.SelectColumns(Source,{"Bus Driver ID", "Bus Driver Name", "Mobile No. 1", "Mobile No. 2", "Bus No.", "Bus Plate No.", "Bus Type", "Seats"}),
    CommonPart = Table.Distinct(CommonColumns),
    SpecificColumns = Table.RemoveColumns(Source,{"Bus Driver Name", "Mobile No. 1", "Mobile No. 2", "Bus No.", "Bus Plate No.", "Bus Type", "Seats"}),
    
    Filtered1 = Table.SelectRows(SpecificColumns, each ([Part] = "First")),
    RemovedColumns1 = Table.RemoveColumns(Filtered1,{"Part"}),
    RenameList1 = List.Transform(List.Skip(Table.ColumnNames(RemovedColumns1)), each {_, "First "&_}),
    SpecificPart1 = Table.RenameColumns(RemovedColumns1,RenameList1),
    Filtered2 = Table.SelectRows(SpecificColumns, each ([Part] = "Second")),
    RemovedColumns2 = Table.RemoveColumns(Filtered2,{"Part"}),
    RenameList2 = List.Transform(List.Skip(Table.ColumnNames(RemovedColumns2)), each {_, "Second "&_}),
    SpecificPart2 = Table.RenameColumns(RemovedColumns2,RenameList2),
    Merged1 = Table.NestedJoin(CommonPart,{"Bus Driver ID"},SpecificPart1,{"Bus Driver ID"},"SpecificPart1",JoinKind.LeftOuter),
    Merged2 = Table.NestedJoin(Merged1,{"Bus Driver ID"},SpecificPart2,{"Bus Driver ID"},"SpecificPart2",JoinKind.LeftOuter),
    
    ExpandList1 = List.Transform(RenameList1, each _{1}),
    ExpandList2 = List.Transform(RenameList2, each _{1}),
    ExpandedPart1 = Table.ExpandTableColumn(Merged2, "SpecificPart1", ExpandList1),
    ExpandedPart2 = Table.ExpandTableColumn(ExpandedPart1, "SpecificPart2", ExpandList2)
in
    ExpandedPart2
 
Last edited:
Upvote 0
That's not what I meant..
I think I need to open a new post where I will clear things to understand...I am so thankful to you MarcelBeug . You's a legend..
 
Upvote 0
The question seemed quite obvious to me, so I really don't understand your one liner "That's not what I meant", without any further specification what would be wrong.
 
Upvote 0
marcelBeug,
I posted a new thread and its very clear I think.. I am so thankful to you for quick replies..
 
Upvote 0
How is this working? I get it is replacing the nulls in the second part with data from Part1, but why?

Part2 = Value.ReplaceType(#"Removed Columns",Value.Type(Part1)),
 
Upvote 0
No, you got it wrong.

This code replaces the table type of the table in #"Removed Columns" by the table type of table Part1.
A table type is the combination of the table properties: column names, column types and any table keys.

In this case, it is used to synchronize the column names and column types of the second part with those of the first part.
It doesn't affect any table content.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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