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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
VijayRbn
New Member

Pivoting the table data in Modeling

Hi,

I am unable to get the below output using Pivot table option.

Table.

IDAttribValue
82SectionA
82SectionB
82Class2
83SectionA
83Class3

 

Output Table;

IDSectionClass
82A2
82B2
83A3


If I use the pivot table option in the modeling to convert the Atrrib column values as columns against each ID, getting an error where we have extract of multiple values attribute (Section in this case).

How to convert Section as new column retaining multiple values as multiple rows for particular IDs.

1 ACCEPTED SOLUTION
BeaBF
Memorable Member
Memorable Member

@VijayRbn Hi! Here the M code for the advanced editor:

let
// Load the original data
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjBS0lEKTk0uyczPA7IclWJ1MASdEILOOYnFxUDaCCJkjE2zMZI6Y6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Attrib = _t, Value = _t]),

// Group the data by ID and Attrib to handle multiple rows for the same key
GroupedTable = Table.Group(
Source,
{"ID", "Attrib"},
{{"Values", each Text.Combine([Value], ","), type text}}
),

// Pivot the grouped table based on the 'Attrib' column
PivotedTable = Table.Pivot(
GroupedTable,
List.Distinct(GroupedTable[Attrib]), // Pivot by the unique values in 'Attrib' (i.e., 'Section' and 'Class')
"Attrib",
"Values"
),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(PivotedTable, {{"Section", Splitter.SplitTextByDelimiter(",", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Section"),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ID", Int64.Type}, {"Section", type text}})
in
#"Changed Type"

 

you'll achieve:

BeaBF_0-1727172799107.png

 

if it's ok, please accept my answer as solution.

 

BBF

View solution in original post

2 REPLIES 2
BeaBF
Memorable Member
Memorable Member

@VijayRbn Hi! Here the M code for the advanced editor:

let
// Load the original data
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjBS0lEKTk0uyczPA7IclWJ1MASdEILOOYnFxUDaCCJkjE2zMZI6Y6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Attrib = _t, Value = _t]),

// Group the data by ID and Attrib to handle multiple rows for the same key
GroupedTable = Table.Group(
Source,
{"ID", "Attrib"},
{{"Values", each Text.Combine([Value], ","), type text}}
),

// Pivot the grouped table based on the 'Attrib' column
PivotedTable = Table.Pivot(
GroupedTable,
List.Distinct(GroupedTable[Attrib]), // Pivot by the unique values in 'Attrib' (i.e., 'Section' and 'Class')
"Attrib",
"Values"
),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(PivotedTable, {{"Section", Splitter.SplitTextByDelimiter(",", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Section"),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ID", Int64.Type}, {"Section", type text}})
in
#"Changed Type"

 

you'll achieve:

BeaBF_0-1727172799107.png

 

if it's ok, please accept my answer as solution.

 

BBF

That really helped my Puzzle! Thanks a lot. I was unaware of the Group concept, that would help in my learning too.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.