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
Anonymous
Not applicable

Transforming rows to column transpose problem

Hi experts, I want to have 1 column of attribute for multiple rows of value.
so it i want to transform this

attributevalue
Oid0
DIAMETER254
Q 
ID34
LABELP-0976
LENGTH672
MATERIALDuctile Iron
STARTNODE31
START_LJ-1
STOPNODE33
STOP_NODELJ-2
V 
Oid1
DIAMETER254
Q 
ID37
LABELP-0977
LENGTH271
MATERIALDuctile Iron
STARTNODE33
START_LJ-2
STOPNODE36
STOP_NODELJ-3
V 

into this

OidDIAMETERQIDLABELLENGTHMATERIALSTARTNODESTART_LSTOPNODESTOP_NODELV
0254 34P-0976672Ductile Iron31J-133J-2 
1254 37P-0977345wood2J-202J-1 

 

Here is my actual attemp:

let
Source = API CALL.Contents(),
Source1 = Source{[Name="Source1"]}[Data],
#"1221" = Source1{[ID=1424]}[Data],
CAP = #"1221"{[Name="CAP"]}[Data],
Pipes = CAP{[Name="Pipes"]}[Data],
list1 = Table.FromList(Pipes, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
test = Table.ExpandListColumn(list1, "Column1"),
expandColumn1 = Table.ExpandRecordColumn(test, "Column1", {"attribute", "value"}, {"attribute", "value"}),
#"Transposed Table" = Table.Transpose(expandColumn1),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true])
in
#"Promoted Headers"

 

Basically I've

1. expand list

2. expand records

3.transpose column attribute

4. promote first row as header

 

The problem is when i transpose and set the first row as the header, the columns are expending instead of creating a new row for each attribute.

 

OidDIAMETERQIDLABELLENGTHMATERIALSTARTNODESTART_LSTOPNODESTOP_NODELVOid_1DIAMETER_2Q_3ID_4LABEL_5LENGTH_6MATERIAL_7STARTNODE_8START_L_9STOPNODE_10STOP_NODEL_11V_12
0254 34P-0976672Ductile Iron31J-133J-2 1254 37P-0977271Ductile Iron33J-236J-3 

 

I also tryed to pivot instead but it says
 Expression.Error: There were too many elements in the enumeration to complete the operation.Details:[List]

 

Thank you for your time

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

If you have an index to keep track of which record is which, (like this)

AlexisOlson_0-1614202481286.png

then you can do this in a single step, i.e., pivot the [attribute] column (Transform tabe > Pivot Column):

 

AlexisOlson_1-1614202679546.png

 

To generate this index column, one option is to generate a standard index column from 0 and then integer divide by the number of distinct attributes.

 

Full M code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s9MUdJRMlCK1YlWcvF09HUNcQ0CChiZmoCFAoFsBTDL0wXINIaI+jg6ufoAuQG6BpbmZhAhVz/3EA+gmJm5EVjA1xFokqcjSJlLaXJJZk6qgmdRfh5YLjjEMSjEz9/FFWSkIUIoHqTaSxcm4h8AU2MMF4kHCUGUQewJg7sQ4hVDYr1ijukVc1SvGJkbkuYVYwyvGGF4xQybV4zhXlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [attribute = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"attribute", type text}, {"value", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    NumAttr = Table.RowCount(Table.Distinct(#"Added Index", "attribute")),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, NumAttr), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[attribute]), "attribute", "value")
in
    #"Pivoted Column"

 

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

This is working like a charm, i understand where my error was. thank you

AlexisOlson
Super User
Super User

If you have an index to keep track of which record is which, (like this)

AlexisOlson_0-1614202481286.png

then you can do this in a single step, i.e., pivot the [attribute] column (Transform tabe > Pivot Column):

 

AlexisOlson_1-1614202679546.png

 

To generate this index column, one option is to generate a standard index column from 0 and then integer divide by the number of distinct attributes.

 

Full M code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s9MUdJRMlCK1YlWcvF09HUNcQ0CChiZmoCFAoFsBTDL0wXINIaI+jg6ufoAuQG6BpbmZhAhVz/3EA+gmJm5EVjA1xFokqcjSJlLaXJJZk6qgmdRfh5YLjjEMSjEz9/FFWSkIUIoHqTaSxcm4h8AU2MMF4kHCUGUQewJg7sQ4hVDYr1ijukVc1SvGJkbkuYVYwyvGGF4xQybV4zhXlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [attribute = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"attribute", type text}, {"value", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    NumAttr = Table.RowCount(Table.Distinct(#"Added Index", "attribute")),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, NumAttr), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[attribute]), "attribute", "value")
in
    #"Pivoted Column"

 

 

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.

Top Solution Authors