Forum Discussion

zsidane's avatar
zsidane
Regular Visitor
4 years ago
Solved

Help with transpose/unpivot

 Hello everyone,

 

I need help in order to organise data from column to rows  using POWER QUERY (Data is in Excel).

 

EDIT: I aattached an excel file as my mess isn't the best to look at 🙂  See excel file

 

My data looks like this:

 

DRIVER #SITEVEHICLE-1VEHICLE-1 % ALLOCVEHICLE-2VEHICLE-2 % ALLOCVEHICLE-3VEHICLE-3 % ALLOCVEHICLE-4VEHICLE-4 % ALLOCVEHICLE-5VEHICLE-5 % ALLOC
101Construction Site 1Truck 11        
102Construction Site 1Truck 10.5Truck 20.4Car 10.1    
103Construction Site 2Truck 10.4Excavator 10.5Excavator 20.1    
104HQCar 10.1Car 20.1Car 30.1Pickup 10.4Pickup 20.3
105Construction Site 2Excavator 20.8Pickup 20.2      

Columns have equipment and their cost allocation and I want to have my equipments to be in a row with each the driver and the allocation:

 

 

VehiculeDriverAllocation
Truck 11011
Truck 11020.5
Truck 11030.4
Truck 21020.4
Excavator 11030.5
Excavator 21030.1
Excavator 21050.8
Car 11020.1
Car 11040.1
Car 21040.1
Car 31040.01
Pickup 11040.4
Pickup 21040.3
Pickup 21050.2

 

Thanks in advance for your help

 

 

  • This should work for you

    let
        Source = Excel.CurrentWorkbook(),
        InputTable = Source{[Name="Table1"]}[Content],
        #"Removed Columns" = Table.RemoveColumns(InputTable,{"Site"}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Driver #"}, "Attribute", "Value"),
        #"Inserted Text Before Delimiter" = Table.AddColumn(#"Unpivoted Other Columns", "VehiculeId", each Text.BeforeDelimiter([Attribute], " "), type text),
        #"Grouped Rows" = Table.Group(#"Inserted Text Before Delimiter", {"Driver #", "VehiculeId"}, {{"Vehicule", each List.Max([Value]), type any}, {"Allocation", each List.Min([Value]), type any}}),
        #"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"VehiculeId"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Driver #", "Driver"}})
    in
        #"Renamed Columns"

5 Replies

  • artemus's avatar
    artemus
    Microsoft Employee

    This should work for you

    let
        Source = Excel.CurrentWorkbook(),
        InputTable = Source{[Name="Table1"]}[Content],
        #"Removed Columns" = Table.RemoveColumns(InputTable,{"Site"}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Driver #"}, "Attribute", "Value"),
        #"Inserted Text Before Delimiter" = Table.AddColumn(#"Unpivoted Other Columns", "VehiculeId", each Text.BeforeDelimiter([Attribute], " "), type text),
        #"Grouped Rows" = Table.Group(#"Inserted Text Before Delimiter", {"Driver #", "VehiculeId"}, {{"Vehicule", each List.Max([Value]), type any}, {"Allocation", each List.Min([Value]), type any}}),
        #"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"VehiculeId"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Driver #", "Driver"}})
    in
        #"Renamed Columns"
    • zsidane's avatar
      zsidane
      Regular Visitor

      Thank you! It worked. Now I have a different problem...

       

      My set of data has around 4100 "drivers" and +50 types of "vehicule". Once I close and load data, the query takes a lot of time (I see the count of rows at +20 million rows...) then it ends up failing.

       

      Also, another question please: What's the best way to put the code in the formula bar in power query? The only way I managed to get it running is by pasting it in the source (1st step). Ideally, I'dd like to do a couple steps before (like removing columns I don't need).

       

      Again, thanks for your help.

      • artemus's avatar
        artemus
        Microsoft Employee

        I would have to see what the error is to understand why it is failing.

         

        For the formula, you can use the advanced editor and paste it in there.

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    =#table({"Vehicle","Driver","Allocation"},List.TransformMany(Table.ToRows(PreviousStepName),each List.Split(List.Skip(List.RemoveLastN(_,each _=null),2),2),(x,y)=>{x{0}}&y))

    • zsidane's avatar
      zsidane
      Regular Visitor

      Thank you. I'll try and get back to you!