Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Unpivoting help please!

Hi all,

 

I'm pretty new to Power BI so still trying to figure out how to transform data properly. I have my data in the following format:

 

NameReleaseCategoryPhase 1 StartPhase 1 EndPhase 2 StartPhase 2 EndPhase 3 StartPhase 3 End
Task 1April-22A1/3/2130/3/211/4/2126/4/211/5/2120/5/21
Task 2May-22B1/6/2124/6/211/7/2119/7/211/8/2125/8/21

 

I'm need to transform this data so that I can use a Gantt chart so I know my target is:

 

NameReleaseStageStart DateDuration (or end date)
Task 1April-22Phase 11/3/21x
Task 1April-22Phase 21/4/21x
Task 1April-22Phase 31/5/21x
Task 2May-22Phase 11/6/21x
Task 2May-22Phase 21/7/21x
Task 2May-22Phase 31/8/21x

 

I can pivot the start dates, but then become unstuck. I've tried creating two queries where I unpivot the start dates in one, and unpivot the end dates in the other, but that doesn't work. Can anyone please suggest the steps I need to take to get to my target state?

 

Much appreciated!

 

  • Anonymous 

     

    Select only the start date fields (3 of them) and then right click --> unpivot columns

    Now Select only the end date fields (3 of them) and then right click --> unpivot columns

     

    Add a field where it flags rows where the Start and End Date is on the same phase.

     

    Select these rows

     

    Attached is the PowerBI workspace

     

    let
    Source = Excel.Workbook(File.Contents("C:\Dropbox\Share\Sample to Delete\UnPivot.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Name", type text}, {"Release", type date}, {"Category", type text}, {"Phase 1 Start", type date}, {"Phase 1 End", type date}, {"Phase 2 Start", type date}, {"Phase 2 End", type date}, {"Phase 3 Start", type date}, {"Phase 3 End", type date}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "Release", "Category", "Phase 1 End", "Phase 2 End", "Phase 3 End"}, "Attribute", "Value"),
    #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Unpivoted Columns", {"Name", "Release", "Category", "Attribute", "Value"}, "Attribute.1", "Value.1"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns1", "Flag rows", each if Text.Start([Attribute],7) = Text.Start([Attribute.1],7) then 1 else 0),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Flag rows] = 1))
    in
    #"Filtered Rows"

5 Replies

  • rsbin's avatar
    rsbin
    Community Champion

    Anonymous,

    In PQ first, make sure all your date fields are type Date.

    Then select all 6 of your Phase fields.

    Then Unpivot Selected Columns only.

    Hope this works for you.

    Regards,

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for the reply rsbin 

       

      That gives me the following

       

      Phase 1 Startx
      Phase 1 Endx
      Phase 2 Startx
      Phase 2 Endx
      Phase 3 Startx
      Phase 3 Endx

       

      Which isn't exactly what I'm looking for. Any tips?

      • themistoklis's avatar
        themistoklis
        Community Champion

        Anonymous 

         

        Select only the start date fields (3 of them) and then right click --> unpivot columns

        Now Select only the end date fields (3 of them) and then right click --> unpivot columns

         

        Add a field where it flags rows where the Start and End Date is on the same phase.

         

        Select these rows

         

        Attached is the PowerBI workspace

         

        let
        Source = Excel.Workbook(File.Contents("C:\Dropbox\Share\Sample to Delete\UnPivot.xlsx"), null, true),
        Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Name", type text}, {"Release", type date}, {"Category", type text}, {"Phase 1 Start", type date}, {"Phase 1 End", type date}, {"Phase 2 Start", type date}, {"Phase 2 End", type date}, {"Phase 3 Start", type date}, {"Phase 3 End", type date}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "Release", "Category", "Phase 1 End", "Phase 2 End", "Phase 3 End"}, "Attribute", "Value"),
        #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Unpivoted Columns", {"Name", "Release", "Category", "Attribute", "Value"}, "Attribute.1", "Value.1"),
        #"Added Custom" = Table.AddColumn(#"Unpivoted Columns1", "Flag rows", each if Text.Start([Attribute],7) = Text.Start([Attribute.1],7) then 1 else 0),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Flag rows] = 1))
        in
        #"Filtered Rows"

  • CNENFRNL's avatar
    CNENFRNL
    Community Champion

    Simple game of montage

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkkszlYwVNJRciwoyszRNTICMYHYUN9Y3wgkbmwAYxnqm0AYRmYwlqG+KVTIAMKK1YEaCTLHN7ESYqATWKkZVKkJjGWobw5lWMJZ+hZQVaYQVmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Release = _t, Category = _t, #"Phase 1 Start" = _t, #"Phase 1 End" = _t, #"Phase 2 Start" = _t, #"Phase 2 End" = _t, #"Phase 3 Start" = _t, #"Phase 3 End" = _t]),
    
        Cols = Table.ToColumns(Source),
        Montage =
            let
                common = List.FirstN(Cols,3),
                hdr = List.FirstN(Table.ColumnNames(Source),3) & {"Start", "End"},
                phases = List.Split(List.Skip(Cols,3),2)
            in
                Table.Combine(List.Transform({0..2}, each Table.AddColumn(Table.FromColumns(common & phases{_}, hdr), "Phase", (r)=>_+1)))
    in
        Montage