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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Works for more Vehicles

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.Accumulate(List.Range(Table.ColumnNames(_),3),{},(s,l)=> s&{List.Sum(Table.ToColumns(Table.SelectColumns(_,l)){0})}  ) },Table.ColumnNames(_) )  }}),
    Subtotal = Table.Combine(GroupedYear[A]),
    TotalRow = Table.FromRows(Table.ToRows(Subtotal)&{{"Total",null,null}& List.Accumulate(List.Range(Table.ColumnNames(Pivoted),3),{},(s,l)=> s&{List.Sum(Table.ToColumns(Table.SelectColumns(Pivoted,l)){0})}  )},Table.ColumnNames(Pivoted) ),
    TotalColumn = Table.AddColumn(TotalRow, "Total", each List.Sum(List.Range(Record.ToList(_),3)) )
in
    TotalColumn



Book1
ABCDEFGHIJKLM
1IDNameYearPurchasesVehiclesIDNameYearCarsMotorcyclesBikeTotal
2123A202013065Cars123A2020130653599749062
3456B202054220Cars246D20207322473224
4789C202025227Cars357E202073505105201178706
5246D202073224Cars456B20205422054220
6357E202073505Cars468F202011014824807134955
7468F2020110148Cars789C2020252276260287829
8123A202035997Motorcycles888H20206979269792
9999G202016750Motorcycles999G20201675016750
10789C202062602MotorcyclesSubtotal2020349389315149664538
11888H202069792Motorcycles123A20215215052150
12357E2020105201Motorcycles999G20212350023500
13468F202024807MotorcyclesSubtotal2021521502350075650
14123A202152150Cars888Z2022234234
15999G202123500MotorcyclesSubtotal2022234234
16888Z2022234BikeTotal401539338649234740422
Sheet1
 
Upvote 0
I'd add this line and will be fine
Power Query:
Table.SelectRows(Source, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null})))
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
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