Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Chimichanga2024
Regular Visitor

how to pivot several columns like this? TIA!

this is my current table:

ShiftDateValueIndexOrder
6:00 AM-2:00 PM11/12/2024Meghan11
6:00 AM-2:00 PM11/12/20242nd shift22
6:00 AM-2:00 PM11/13/2024Nancy31
6:00 AM-2:00 PM11/13/20241st shift42
6:00 AM-2:00 PM11/14/2024Troy51
6:00 AM-2:00 PM11/14/20242nd shift62
6:00 AM-2:00 PM11/15/2024Tracie71
6:00 AM-2:00 PM11/15/20241st shift82
7:00 AM-3:00 PM11/11/2024Kelly91
7:00 AM-3:00 PM11/11/20243rd Shift102

 

And I just cannot transform into this:

ShiftDateStaffShift
6:00 AM-2:00 PM11/12/2024Meghan2nd shift
6:00 AM-2:00 PM11/13/2024Nancy1st shift
6:00 AM-2:00 PM11/14/2024Troy2nd shift
6:00 AM-2:00 PM11/15/2024Tracie1st shift
7:00 AM-3:00 PM11/11/2024Kelly3rd Shift

 

I was trying to use pivot column function, but when some of the staff and their shifts were showed as error, I simply jusr want to group by column of order. so that shift date

 

what should I do, any advice will be appreciated!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Chimichanga2024 ,
Thanks for Omid_Motamedise and ronrsnfld reply.
Here is another way to achieve this

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrMyMFBw9NU1AtEBvko6SoaG+oZG+kYGRiZAjm9qekZiHkgUjGN1COowyktRKM7ITCsBscEYtyZjmCa/xLzkSiBtTMAWuAbD4hK4LSYEbDGBaQopygdZYkrAEhNsXjEjYIkpwpLE5MxUIMOcgDWm2PxiAbfGHKrJGEWTIUyTd2pODsgzlnBbCGgwLkpRCIbaYmgAsSYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Shift = _t, Date = _t, Value = _t, Index = _t, Order = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Shift", type text}, {"Date", type date}, {"Value", type text}, {"Index", Int64.Type}, {"Order", Int64.Type}}),
    Order1 = Table.SelectRows(#"Changed Type", each [Order] = 1),
    Order2 = Table.SelectRows(#"Changed Type", each [Order] = 2),
    Order2WithPrevIndex = Table.AddColumn(Order2, "PrevIndex", each [Index] - 1),
    Merged = Table.NestedJoin(Order1, {"Index"}, Order2WithPrevIndex, {"PrevIndex"}, "Order2", JoinKind.LeftOuter),
    Expanded = Table.ExpandTableColumn(Merged, "Order2", {"Value"}, {"Shift Order"}),
    #"Removed Columns" = Table.RemoveColumns(Expanded,{"Index", "Order"})
in
    #"Removed Columns"

Final output

vheqmsft_0-1730683922454.png

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

View solution in original post

8 REPLIES 8
mussaenda
Super User
Super User

Hi @Chimichanga2024 ,

 

If you'll remove the index then Pivot the Value according to the Order field without aggregation,  then there will be no error.

 

Hope this helps

Hi, Mussaenda, 

thank you for your reply, the potential issue will be: 

if two person with same date time and shift time, if I use pivot column, then it will be showing error

1.png

Anonymous
Not applicable

Hi @Chimichanga2024 ,
Thanks for Omid_Motamedise and ronrsnfld reply.
Here is another way to achieve this

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrMyMFBw9NU1AtEBvko6SoaG+oZG+kYGRiZAjm9qekZiHkgUjGN1COowyktRKM7ITCsBscEYtyZjmCa/xLzkSiBtTMAWuAbD4hK4LSYEbDGBaQopygdZYkrAEhNsXjEjYIkpwpLE5MxUIMOcgDWm2PxiAbfGHKrJGEWTIUyTd2pODsgzlnBbCGgwLkpRCIbaYmgAsSYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Shift = _t, Date = _t, Value = _t, Index = _t, Order = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Shift", type text}, {"Date", type date}, {"Value", type text}, {"Index", Int64.Type}, {"Order", Int64.Type}}),
    Order1 = Table.SelectRows(#"Changed Type", each [Order] = 1),
    Order2 = Table.SelectRows(#"Changed Type", each [Order] = 2),
    Order2WithPrevIndex = Table.AddColumn(Order2, "PrevIndex", each [Index] - 1),
    Merged = Table.NestedJoin(Order1, {"Index"}, Order2WithPrevIndex, {"PrevIndex"}, "Order2", JoinKind.LeftOuter),
    Expanded = Table.ExpandTableColumn(Merged, "Order2", {"Value"}, {"Shift Order"}),
    #"Removed Columns" = Table.RemoveColumns(Expanded,{"Index", "Order"})
in
    #"Removed Columns"

Final output

vheqmsft_0-1730683922454.png

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

Thank you Albert, your method is easier!

I end up revise like this:

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Shift", type text}, {"Date", type date}, {"Value", type text}, {"Index", Int64.Type}, {"Order", Int64.Type}}),
Order1 = Table.SelectRows(#"Changed Type", each [Order] = 1),
Order2 = Table.SelectRows(#"Changed Type", each [Order] = 2),
Order2WithPrevIndex = Table.AddColumn(Order2, "PrevIndex", each [Index] - 1),
Merged = Table.NestedJoin(Order1, {"Index"}, Order2WithPrevIndex, {"PrevIndex"}, "Order2", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Merged, "Order2", {"Value"}, {"Shift Order"}),
#"Removed Columns" = Table.RemoveColumns(Expanded,{"Index", "Order"})
in
#"Removed Columns"

Omid_Motamedise
Super User
Super User

Table.Split is a wounderful functino which can use you, so just copy the below code and past it into advance editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrMyMFBw9NU1AtEBvko6SoaG+oZG+kYGRiZAjm9qekZiHkhUKVaHoGqjvBSF4ozMtBIQG48GY5gGv8S85EoCpsMVGxaXEGW6CUxDSFE+IcNNSHW6KcLwxOTMVALGm+J2uzlUgzGKBkOYBu/UnByE4wkoNi5KUQhGmB4LAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Shift = _t, Date = _t, Value = _t, Order = _t]),
    Custom1 = Table.Split(Source,2),
    Custom2 = List.Transform(Custom1, each  Table.Pivot(_, List.Distinct(_[Order]), "Order", "Value")),
    Custom3 = Table.Combine(Custom2)
in
    Custom3
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

Hi Sir, this is the codes I tried to update, but showing error: 

let
// Load data from the Excel table
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

// Change data types of the relevant columns
#"Changed Type" = Table.TransformColumnTypes(Source, {
{"Shift", type text}, {"Date", type date}, {"Value", type text},
{"Index", Int64.Type}, {"Order", Int64.Type}}),

// Add a Custom column with split tables
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.Split(Source, 2)),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom2", each List.Transform([Custom],each Table.Pivot(_, List.Distinct(_[Order]), "Order", "Value"))),
#"Added Custom3" = Table.AddColumn(#"Added Custom1", "Custom2", each Table.Combine([Custom2]))
in

ronrsnfld
Super User
Super User

I would use a different approach

  • Create a duplicate of the Value column but shift it one up
  • Remove every other row from the table

No need for your Index and Order columns in your data:

ronrsnfld_0-1730506604688.png

 

Use this code (Paste into the Advanced Editor):

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrMyMFBw9NU1AtEBvko6SoaG+oZG+kYGRiZAjm9qekZinlKsDkGVRnkpCsUZmWkleBQbwxT7JeYlVxKj0LC4hKCpJjDFIUX5+Aw1IcWppghDE5MzU4lRiepWc6hiYxTFhjDF3qk5OZXEKDQuSlEIhpgaCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Shift = _t, Date = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Shift", type text}, {"Date", type date}, {"Value", type text}}),
    
    #"Add Shifted Value" = Table.FromColumns(
        Table.ToColumns(#"Changed Type")
        & {List.Skip(#"Changed Type"[Value]) & {null}},
        type table[Shift=text, Date=date, Name=text, _Shift=text]),
    
    #"Removed Alternate Rows" = Table.AlternateRows(#"Add Shifted Value",1,1,1)
in
    
    #"Removed Alternate Rows"

 

Results:

ronrsnfld_1-1730506694226.png

 

 

Chimichanga2024
Regular Visitor

pivot columns were showing error, I don't know why, so any advices will be appreciated!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.