Transform Table with Power Query

JGordon11

Well-known Member
Joined
Jan 18, 2021
Messages
845
Office Version
  1. 365
Platform
  1. Windows
How would one transform the green table on the left into the blue table on the right using power query?

Book1
ABCDEFGHIJKL
1IDNameYearPurchasesVehiclesIDNameYearCarsMotorcyclesTotal
2123A202013065Cars123A2020130653599749062
3456B202054220Cars456B20205422054220
4789C202025227Cars789C2020252276260287829
5246D202073224Cars246D20207322473224
6357E202073505Cars357E202073505105201178706
7468F2020110148Cars468F202011014824807134955
8123A202035997Motorcycles999G20201675016750
9999G202016750Motorcycles888H20206979269792
10789C202062602MotorcyclesSubtotal2020349389315149664538
11888H202069792Motorcycles123A20215215052150
12357E2020105201Motorcycles999G20212350023500
13468F202024807MotorcyclesSubtotal2021521502350075650
14123A202152150CarsTotal401539338649740188
15999G202123500Motorcycles
Append1
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
it's possible with Power Query but why make your life difficult if you can use Pivot Table
 
Upvote 0
I'm trying to learn Power Query and this example seems like it would cover a lot of transformation types and useful functions that I could refer to.
 
Upvote 0
Please try

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"ID", "Name", "Year", "Vehicles"}, {{"Total", each List.Sum([Purchases]), type number}}),
    Pivoted = Table.Pivot(Group, List.Distinct(Group[Vehicles]), "Vehicles", "Total", List.Sum),
    GroupedYear = Table.Group(Pivoted, {"Year"}, {{"A", each Table.FromRows(Table.ToRows(_)&{{"Subtotal",null,List.Min([Year]),List.Sum([Cars]),List.Sum([Motorcycles]) }},Table.ColumnNames(_) )  }}),
    Subtotal = Table.Combine(GroupedYear[A]),
    TotalRow = Table.InsertRows(Subtotal,Table.RowCount(Subtotal),{[ID="Total",Name=null,Year=null,Cars=List.Sum(Pivoted[Cars]),Motorcycles=List.Sum(Pivoted[Motorcycles])]}),
    TotalColumn = Table.AddColumn(TotalRow, "Total", each List.Sum({[Cars],[Motorcycles]}))
in
    TotalColumn
 
Upvote 0
Solution
Please try

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"ID", "Name", "Year", "Vehicles"}, {{"Total", each List.Sum([Purchases]), type number}}),
    Pivoted = Table.Pivot(Group, List.Distinct(Group[Vehicles]), "Vehicles", "Total", List.Sum),
    GroupedYear = Table.Group(Pivoted, {"Year"}, {{"A", each Table.FromRows(Table.ToRows(_)&{{"Subtotal",null,List.Min([Year]),List.Sum([Cars]),List.Sum([Motorcycles]) }},Table.ColumnNames(_) )  }}),
    Subtotal = Table.Combine(GroupedYear[A]),
    TotalRow = Table.InsertRows(Subtotal,Table.RowCount(Subtotal),{[ID="Total",Name=null,Year=null,Cars=List.Sum(Pivoted[Cars]),Motorcycles=List.Sum(Pivoted[Motorcycles])]}),
    TotalColumn = Table.AddColumn(TotalRow, "Total", each List.Sum({[Cars],[Motorcycles]}))
in
    TotalColumn
Replicates perfectly. Very cool that all that can be done in just seven steps. Thanks for the lesson!
 
Upvote 0
it doesn't work if source will be updated, even if it will be one row
IDNameYearPurchasesVehiclesIDNameYearCarsMotorcyclesBikeTotal
123A202013065Cars123A2020130653599749062
456B202054220Cars246D20207322473224
789C202025227Cars357E202073505105201178706
246D202073224Cars456B20205422054220
357E202073505Cars468F202011014824807134955
468F2020110148Cars789C2020252276260287829
123A202035997Motorcycles888H20206979269792
999G202016750Motorcycles999G20201675016750
789C202062602Motorcycles
888H202069792Motorcycles123A20215215052150
357E2020105201Motorcycles999G20212350023500
468F202024807Motorcycles
123A202152150Cars888Z2022234
999G202123500Motorcycles
888Z2022234Bike
 
Upvote 0
I agree that PT is the way to go if this were a real data table that would change over time. But I was just trying to see some PQ steps and dive into the function references to understand how they work on a particular example. I'll keep poking around on this forum to see if there are some other examples to learn from. Thanks for the documentation reference.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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