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
doeked
Frequent Visitor

Unpivot multiple columns

I am currently struggling with a Gantt chart that I need to make. I need for that the starting dates (I have) and the duration of an activity. The Gantt will have 5 different phases, of which I only have the starting dates. The duration I have calculated from the difference between phase 1 and 2 starting dates. For that I have created a table that looks like this:

 

IndexPhase 1: Project kick-offPhase 2: Technical alignmentPhase 3: ABSPhase 1 durationPhase 2 duration
103-Mar-1921-Jul-1917-Nov-19140119
201-Mar-2010-May-2012-Jul-207063
3null24-Sep-1703-Dec-17null70

 

For me, with unpivot funtionality I have created an table that has all the phases nicely listed. However, I need to have a column added that containt the duration of that phase for that specific index no.

IndexAttributeValue (starting date)Duration (how to?)
1Phase 1: Project kick-off03-Mar-19 
1Phase 2: Technical alignment01-Mar-20 

 

If I unpivot all columns other columns (than the index), it will look like this. How do I take the duration to another column?

 

IndexAttributeValue

1

Phase 1: Project kick-off03-Mar-19
1Phase 2: Technical alignment01-Mar-20
1Phase 1 duration140
1Phase 2 duration119

 

Thanks in advance!!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @doeked,

I'd like to suggest you only use 'unpivot column' on the date fields, then you can add a custom column with if statement to check the attribute field value and return different 'phase' duration value and remove the original columns.

ResultResult

Full code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1vVNLNI1tASyjQx1vUpzIGxDc12//DIo28QARALZsTrRSkYgXYZgXUZgcQMguxLKNgKbAGabgwgzY7AeYyAzrzQnB2SJiW5waoGuoTnEcpfUZAgbKg3UFRsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, #"Phase 1: Project kick-off" = _t, #"Phase 2: Technical alignment" = _t, #"Phase 3: ABS" = _t, #"Phase 1 duration" = _t, #"Phase 2 duration" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Phase 1: Project kick-off", type date}, {"Phase 2: Technical alignment", type date}, {"Phase 3: ABS", type date}, {"Phase 1 duration", Int64.Type}, {"Phase 2 duration", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Index", "Phase 1 duration", "Phase 2 duration"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Duration", each if Text.Split([Attribute],":"){0} ="Phase 1" then [Phase 1 duration] else if Text.Split([Attribute],":"){0} ="Phase 2" then  [Phase 2 duration]  else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Phase 1 duration", "Phase 2 duration"})
in
    #"Removed Columns"

Regards,

Xiaoxin Sheng

View solution in original post

1 REPLY 1
Anonymous
Not applicable

HI @doeked,

I'd like to suggest you only use 'unpivot column' on the date fields, then you can add a custom column with if statement to check the attribute field value and return different 'phase' duration value and remove the original columns.

ResultResult

Full code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1vVNLNI1tASyjQx1vUpzIGxDc12//DIo28QARALZsTrRSkYgXYZgXUZgcQMguxLKNgKbAGabgwgzY7AeYyAzrzQnB2SJiW5waoGuoTnEcpfUZAgbKg3UFRsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, #"Phase 1: Project kick-off" = _t, #"Phase 2: Technical alignment" = _t, #"Phase 3: ABS" = _t, #"Phase 1 duration" = _t, #"Phase 2 duration" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Phase 1: Project kick-off", type date}, {"Phase 2: Technical alignment", type date}, {"Phase 3: ABS", type date}, {"Phase 1 duration", Int64.Type}, {"Phase 2 duration", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Index", "Phase 1 duration", "Phase 2 duration"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Duration", each if Text.Split([Attribute],":"){0} ="Phase 1" then [Phase 1 duration] else if Text.Split([Attribute],":"){0} ="Phase 2" then  [Phase 2 duration]  else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Phase 1 duration", "Phase 2 duration"})
in
    #"Removed Columns"

Regards,

Xiaoxin Sheng

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.