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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
sperry1625
New Member

Transpose/Pivot Table

I am trying to figure out how to transpose/Pivot a table I have.  Here is what I have:

SR IdSR NameNameTypeStatusStateComment
1Mapping (9116)[1] - 9116AccountCompleteApprovedComment 1
1Mapping (9116)[1] - 9116FunctionCompleteApprovedComment2
1Mapping (9116)[1] - 9116ICPCompleteApprovedComment3
1Mapping (9116)[1] - 9116EntityCompleteApprovedComment4

Here is what I am trying to achieve:

SR IdSR NameNameAccount StatusAccount StateAccount CommentFunction StatusFunction StateFunction CommentICP StatusICP StateICP CommentEntity StatusEntity StateEntity Comment
1Mapping (9116)[1] - 9116CompleteApprovedComment 1CompleteApprovedComment 2CompleteApprovedComment 3CompleteApprovedComment 4

 

I have tried several options; nothing gives me the result I am looking for. Any help would be appreciated.

 

Thank you.

 

Steven

2 ACCEPTED SOLUTIONS
lance_6
Helper II
Helper II

This is an interesting way to do it, but...

 

let
    Source = Source,
    #"Grouped Rows" = Table.Group(Source, {"id", "name", "type"}, {{"Rows", each _, type table [id=nullable number, name=nullable text, type=nullable text, status=nullable text, state=nullable text, comment=nullable text]}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"type"]), "type", "Rows"),
    #"Expanded Account" = Table.ExpandTableColumn(#"Pivoted Column", "Account", {"status", "state", "comment"}, {"Account.status", "Account.state", "Account.comment"}),
    #"Expanded Function" = Table.ExpandTableColumn(#"Expanded Account", "Function", {"status", "state", "comment"}, {"Function.status", "Function.state", "Function.comment"}),
    #"Expanded ICP" = Table.ExpandTableColumn(#"Expanded Function", "ICP", {"status", "state", "comment"}, {"ICP.status", "ICP.state", "ICP.comment"}),
    #"Expanded Entity" = Table.ExpandTableColumn(#"Expanded ICP", "Entity", {"status", "state", "comment"}, {"Entity.status", "Entity.state", "Entity.comment"})
in
    #"Expanded Entity"

 

So it's essentially a group by, then pivot, then expand.

View solution in original post

v-shex-msft
Community Support
Community Support

HI @sperry1625,

You can take a look at the following sample formulas if these suitable for your requirement:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJNLCjIzEtX0LA0NDTTBApEG8Yq6CqAeECOY3JyfmleCZDlnJ9bkJNakgoSLCgoyi9LTYGI5qbmlSgYKsXqEGOeW2lecklmfh4hA42INM/TOYCQUcZEGuWaV5JZUknINBOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"SR Id" = _t, #"SR Name" = _t, Name = _t, Type = _t, Status = _t, State = _t, Comment = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SR Id", Int64.Type}, {"SR Name", type text}, {"Name", type text}, {"Type", type text}, {"Status", type text}, {"State", type text}, {"Comment", type text}}),
    #"Merged Columns" = Table.CombineColumns(#"Changed Type",{"Status", "State", "Comment"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    #"Grouped Rows" = Table.Group(#"Merged Columns", {"SR Id", "SR Name", "Name"}, {{"Count", each Table.PromoteHeaders(Table.Transpose(Table.SelectColumns(_,{"Type","Merged"}))), type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Account", "Function", "ICP", "Entity"}, {"Account", "Function", "ICP", "Entity"})
in
    #"Expanded Count"

1.png

Comment:

I merge three fields and use the group function to summary row table records. After these steps, I nested some functions to deal with group result fields to remove not-used fields and transpose them.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

HI @sperry1625,

You can take a look at the following sample formulas if these suitable for your requirement:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfJNLCjIzEtX0LA0NDTTBApEG8Yq6CqAeECOY3JyfmleCZDlnJ9bkJNakgoSLCgoyi9LTYGI5qbmlSgYKsXqEGOeW2lecklmfh4hA42INM/TOYCQUcZEGuWaV5JZUknINBOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"SR Id" = _t, #"SR Name" = _t, Name = _t, Type = _t, Status = _t, State = _t, Comment = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SR Id", Int64.Type}, {"SR Name", type text}, {"Name", type text}, {"Type", type text}, {"Status", type text}, {"State", type text}, {"Comment", type text}}),
    #"Merged Columns" = Table.CombineColumns(#"Changed Type",{"Status", "State", "Comment"},Combiner.CombineTextByDelimiter(",", QuoteStyle.None),"Merged"),
    #"Grouped Rows" = Table.Group(#"Merged Columns", {"SR Id", "SR Name", "Name"}, {{"Count", each Table.PromoteHeaders(Table.Transpose(Table.SelectColumns(_,{"Type","Merged"}))), type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Account", "Function", "ICP", "Entity"}, {"Account", "Function", "ICP", "Entity"})
in
    #"Expanded Count"

1.png

Comment:

I merge three fields and use the group function to summary row table records. After these steps, I nested some functions to deal with group result fields to remove not-used fields and transpose them.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
lance_6
Helper II
Helper II

This is an interesting way to do it, but...

 

let
    Source = Source,
    #"Grouped Rows" = Table.Group(Source, {"id", "name", "type"}, {{"Rows", each _, type table [id=nullable number, name=nullable text, type=nullable text, status=nullable text, state=nullable text, comment=nullable text]}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"type"]), "type", "Rows"),
    #"Expanded Account" = Table.ExpandTableColumn(#"Pivoted Column", "Account", {"status", "state", "comment"}, {"Account.status", "Account.state", "Account.comment"}),
    #"Expanded Function" = Table.ExpandTableColumn(#"Expanded Account", "Function", {"status", "state", "comment"}, {"Function.status", "Function.state", "Function.comment"}),
    #"Expanded ICP" = Table.ExpandTableColumn(#"Expanded Function", "ICP", {"status", "state", "comment"}, {"ICP.status", "ICP.state", "ICP.comment"}),
    #"Expanded Entity" = Table.ExpandTableColumn(#"Expanded ICP", "Entity", {"status", "state", "comment"}, {"Entity.status", "Entity.state", "Entity.comment"})
in
    #"Expanded Entity"

 

So it's essentially a group by, then pivot, then expand.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.