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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
ssk_1984
Helper I
Helper I

Unpivot or Transpose Header in row level into Column also values

Team help me to bring the below tables values into desired output, is there any way to pivot/unpivot/traspose the values.

NameSuresh
Sales10
NameRaj
Sales20
NameSunil
Sales30

Inthe above table records are stored as one by one , actual requirement to transpose above values into 

NameSales
Suresh10
Raj20
Sunil30
3 ACCEPTED SOLUTIONS
jgeddes
Super User
Super User

One way to do this is to;

1) group by the first column choosing do not aggregate so you get all the rows

2) add an index column to the resulting nested tables

3) expand the nested tables

4) pivot the table on the first column, choosing not to aggregate the values in the second column

5) remove the index column

Here is an example code...

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouLUotzlCK1YlWCk7MSS0GChkagLlQBUGJWSiyRiiywaV5mTko8sZA+VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"All", each Table.SelectColumns(_, {"Column2"}), type table [Column1=text, Column2=text]}}),
    Custom1 = Table.TransformColumns(#"Grouped Rows", {{"All", each Table.AddIndexColumn(_, "index", 1, 1)}}),
    #"Expanded All" = Table.ExpandTableColumn(Custom1, "All", {"Column2", "index"}, {"Column2", "index"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded All", List.Distinct(#"Expanded All"[Column1]), "Column1", "Column2"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"index"})
in
    #"Removed Columns"




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

dufoq3
Super User
Super User

Hi @ssk_1984, another solution:

 

Before

dufoq3_0-1739982514553.png

 

After

dufoq3_1-1739982526985.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouLUotzlCK1YlWCk7MSS0GChkagLlQBUGJWSiyRiiywaV5mTko8sZA+VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    Transformed = Table.FromRows(List.Split(Source[Column2], 2), {"Name", "Sales"})
in
    Transformed

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

View solution in original post

slorin
Super User
Super User

Hi @ssk_1984 

 

another solution

let
Source = Your_Source,
Group = Table.Group(Source, {"Column1"}, {{"Data", each [Column2]}}),
Table = Table.FromColumns(Group[Data], Group[Column1])
in
Table

Stéphane 

View solution in original post

9 REPLIES 9
SundarRaj
Resolver III
Resolver III

Hi @ssk_1984 , here's a quick way to solve your problem. I am attaching two images, first of the M code snippet used and second of the ouput. Thanks!

SundarRaj_0-1739984534688.png

SundarRaj_1-1739984579792.png

 

Thank you for your quick revert, one more query rather than only two column...in my table has more than two column...i can add for each by col3, col4,col5 ...like that....Suggest me pls

 

Do you mean something like this?

SundarRaj_0-1740142075654.png

SundarRaj_1-1740142265414.png

 

If that's the case, then this particular idea should work. Do match it with your idea. If this is not what you were looking for, do provide a sample set of your data and the output that you'd want. Thanks

Could you be a little more specific with regards to your query? I didn't quite understand it correctly

slorin
Super User
Super User

Hi @ssk_1984 

 

another solution

let
Source = Your_Source,
Group = Table.Group(Source, {"Column1"}, {{"Data", each [Column2]}}),
Table = Table.FromColumns(Group[Data], Group[Column1])
in
Table

Stéphane 

dufoq3
Super User
Super User

Hi @ssk_1984, another solution:

 

Before

dufoq3_0-1739982514553.png

 

After

dufoq3_1-1739982526985.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouLUotzlCK1YlWCk7MSS0GChkagLlQBUGJWSiyRiiywaV5mTko8sZA+VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    Transformed = Table.FromRows(List.Split(Source[Column2], 2), {"Name", "Sales"})
in
    Transformed

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

Hi Dufo,

 

i have one more query on the same request,

Now my data set /column increased earlier it depends on two column, now i have 4 different column.

 

values to be shown like the below samples...help me out how to convert these values in pivot/unpivot

 

Nowcolumn and data present in the data set
    
WeekDateProcessMapIDFieldNameFieldValue
12-Apr-256229Connect_Duration(Days)62
12-Apr-256229Connect RAGGreen
12-Apr-256229A&R RAGGreen
12-Apr-256229Execute_Duration(Days)160
12-Apr-256229Execute RAGRed
12-Apr-256229ProcessTypeVoice
12-Apr-256229TransitionTypeStandard
16-Apr-256229Connect_Duration(Days)65
16-Apr-256229Connect RAGRed
16-Apr-256229A&R RAGRed
16-Apr-256229Execute_Duration(Days)180
16-Apr-256229Execute RAGGreen
16-Apr-256229ProcessTypeNon Voice
16-Apr-256229TransitionTypeStandard

 

 ThenRequired format/final format required      
  only Field Name & Field Value to transformed      
WeekDateProcessMapIDConnect_Duration(Days)Connect RAGA&R RAGExecute_Duration(Days)Execute RAGProcessTypeTransitionType
12-Apr-25622962GreenGreen160RedVoiceStandard
16-Apr-25622965RedRed180GreenNon VoiceStandard

Hi @ssk_1984, try this:

 

Ouput

dufoq3_1-1744803903474.png

 

Procedure

dufoq3_0-1744803881900.png

 


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

jgeddes
Super User
Super User

One way to do this is to;

1) group by the first column choosing do not aggregate so you get all the rows

2) add an index column to the resulting nested tables

3) expand the nested tables

4) pivot the table on the first column, choosing not to aggregate the values in the second column

5) remove the index column

Here is an example code...

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouLUotzlCK1YlWCk7MSS0GChkagLlQBUGJWSiyRiiywaV5mTko8sZA+VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"All", each Table.SelectColumns(_, {"Column2"}), type table [Column1=text, Column2=text]}}),
    Custom1 = Table.TransformColumns(#"Grouped Rows", {{"All", each Table.AddIndexColumn(_, "index", 1, 1)}}),
    #"Expanded All" = Table.ExpandTableColumn(Custom1, "All", {"Column2", "index"}, {"Column2", "index"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded All", List.Distinct(#"Expanded All"[Column1]), "Column1", "Column2"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"index"})
in
    #"Removed Columns"




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

Top Kudoed Authors