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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Scott4850
Regular Visitor

Pivot with Multiple Columns?

Obviously I'm not very knowledgeable about this, but I did think I could do this fairly easily with a Pivot, UnPivot, or Transpose...or some combination therof.  But I am stumped.

 

I can pivot on Task, but with only Start or Finish, not both.  I tried duplicating Task and Pivoting again, but results in errors.  Actually, I think I've tried about 50 combinations.  Thanks for your attention.

 

Given the top table, I need the bottom:

 

Capture.JPG

 

 

 

 

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

    fx = (tbl) => #table(
        {"Project"} & List.Combine(List.Transform(tbl[Task], (x) => {x & " (Start)", x & " (Finish)"})),
        {{tbl{0}[Project]} & List.Combine(Table.ToRows(tbl[[Start], [Finish]]))}
    ),
    group = Table.Group(
        your_table,
        "Project", 
        {"x", fx}
    ),
    combine = Table.Combine(group[x])

View solution in original post

13 REPLIES 13
dufoq3
Super User
Super User

Hi @Scott4850, different approach here:

 

Result

dufoq3_0-1714637973347.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCjBU0lEKgRDBJYlFJWCWW2ZeZnGGUqwOTIERmIArMEJWYETIBCN8JsQCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Project = _t, Task = _t, Start = _t, Finish = _t]),
    GroupedRows = Table.Group(Source, {"Project"}, {{"All", each 
        [ colNames = List.Combine(List.Transform([Task], each { _ & " (Start)", _ & " (Finish)" })),
          transform = Table.FromRows( { {[Project]{0}} & List.Combine(Table.ToRows(Table.SelectColumns(_, {"Start", "Finish"}))) }, {"Project"} & colNames ),
          changedType = Table.TransformColumnTypes(transform, List.Transform({"Project"} & colNames, (x)=> {x, type text} ))
        ][changedType]
, type table}}),
    All = Table.Combine(GroupedRows[All])
in
    All

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thanks for the tips on posting!  Couldn't get the table control to work even with tips, though 🙂  Updated original post with a pic instead.

 

I'm unable to try the solution at this time, but will post back later.  Note the link to your explanation is not working, at least at the moment.  Thanks!

The link is working.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Ok, corporate block or something.  I've got it on a personal device.

AlienSx
Super User
Super User

    fx = (tbl) => #table(
        {"Project"} & List.Combine(List.Transform(tbl[Task], (x) => {x & " (Start)", x & " (Finish)"})),
        {{tbl{0}[Project]} & List.Combine(Table.ToRows(tbl[[Start], [Finish]]))}
    ),
    group = Table.Group(
        your_table,
        "Project", 
        {"x", fx}
    ),
    combine = Table.Combine(group[x])

Thanks, I think I can kinda understand this one, but I keep getting 'expected EOF' at the comma at the end of fx = (tbl) => #table(...),

 

Also, just to make sure I understand, your_table is the name of my table from the previous step, which in my case is "Renamed Columns", so I would have #"Renamed Columns" there, correct?

expected EOF - syntax error, show your full code. Kind of "let" is missing or something. 

your_table: yes, that's correct. You should replace this by the name of your (stand alone) query or last step in your code. 

Capture.JPG

 

@Scott4850 i did not change anything, just added your table definition. Watch this

let
    // your_tabel definition
    your_table = #table(
        {"Project", "Task", "Start", "Finish"},
        {{"P1", "T1", "T1Start", "T1Finish"},
        {"P1", "T2", "T2Start", "T2Finish"},
        {"P2", "T1", "T1Start", "T1Finish"},
        {"P2", "T2", "T2Start", "T2Finish"}}),

    fx = (tbl) => #table(
        {"Project"} & List.Combine(List.Transform(tbl[Task], (x) => {x & " (Start)", x & " (Finish)"})),
        {{tbl{0}[Project]} & List.Combine(Table.ToRows(tbl[[Start], [Finish]]))}
    ),
    group = Table.Group(
        your_table,
        "Project", 
        {"x", fx}
    ),
    combine = Table.Combine(group[x])
in
    combine

 

Thanks!  Your solution is succinct and intuitive (even if I don't fully understand it yet).  And, it works!

 

And thanks so much for your patience.  I'd never done a custom query, btw, so that is the reason it took the extra effort.

 

Thanks, I'll post back when I have a chance to try it.

lbendlin
Super User
Super User

There is no need to pivot this. You can achieve the desired outcome with a standard matrix visual.

But this is not going in a visual.  I do need to learn more about the matrix visual, though, so thanks!

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors