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
siva_powerbi
Helper IV
Helper IV

Convert rows to columns and expand all columns at once

Hello Experts

 

I have generated the tables as a list from List.Generate but my requirment is to convert rows in list as each row is a column and then expand all columns at once, when I try to expand indivudal columns then end result is like  a cross join.

 

siva_powerbi_0-1604316248926.png

 

Thanks

Siva

1 ACCEPTED SOLUTION

Hi, @siva_powerbi 

 

I modified data to reproduce your scenario. The pbix file is attached in the end.

Table:

a1.png

 

You may paste the following m codes in 'Advanced Editor' to apply the corresponding transformations.

let
    Source = Table.FromRecords(
        {
            [Column1=Table.FromRecords(
                {[ColumnID=1],[ColumnID=2],[ColumnID=3]}
            )],
            [Column1=Table.FromRecords(
                {[ColumnID=4],[ColumnID=5],[ColumnID=6]}
            )],
            [Column1=Table.FromRecords(
                {[ColumnID=7],[ColumnID=8],[ColumnID=9]}
            )]
        }
    ),
    #"Transposed Table" = Table.Transpose(Source),
    #"Added Custom" = Table.AddColumn(#"Transposed Table", "Custom", each let x = Table.RowCount([Column1]) in 
List.Generate(
    ()=>0,
    each _<=x-1,
    each _+1
)),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    Custom1 = Table.TransformRows(#"Expanded Custom",each let c = _[Custom] in
[Column1=_[Column1][ColumnID]{c},Column2=_[Column2][ColumnID]{c},Column3=_[Column3][ColumnID]{c}]

),
    #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Column1", "Column2", "Column3"}, {"Column1.Column1", "Column1.Column2", "Column1.Column3"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Column1",{{"Column1.Column1", "Column1"}, {"Column1.Column2", "Column2"}, {"Column1.Column3", "Column3"}})
in
    #"Renamed Columns"

 

Result:

a2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-alq-msft
Community Support
Community Support

Hi, @siva_powerbi 

 

Based on your description, I created datat to reproduce your scenario. The pbix file is attached in the end,

Table:

b1.png

 

b2.png

 

Then you may expand the 'Column1'.

b3.png

 

Finally you need to make the table selected, click 'Transpose' in 'Transform' ribbon and set data type for each column to make each row as a column.

b4.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-alq-msft Thanks Allan for the reply and work book.

 

My requirement is a bit different compared to the one added in report. 

 

I would apologize as I didn't mention in question, i will be having a single in the list with each row of the list is a Table.

 

I have updated the workbook and attached same. Also attached the image of the required output.

 

Data in source:

 

thyw.png

 Required output:

 

siva_powerbi_0-1604394686672.png

 

Unable to add the files as getting message pbix files are not supported, In the attached report in your answer need only column (CustomerID)

Thanks

Siva

Hi, @siva_powerbi 

 

I modified data to reproduce your scenario. The pbix file is attached in the end.

Table:

a1.png

 

You may paste the following m codes in 'Advanced Editor' to apply the corresponding transformations.

let
    Source = Table.FromRecords(
        {
            [Column1=Table.FromRecords(
                {[ColumnID=1],[ColumnID=2],[ColumnID=3]}
            )],
            [Column1=Table.FromRecords(
                {[ColumnID=4],[ColumnID=5],[ColumnID=6]}
            )],
            [Column1=Table.FromRecords(
                {[ColumnID=7],[ColumnID=8],[ColumnID=9]}
            )]
        }
    ),
    #"Transposed Table" = Table.Transpose(Source),
    #"Added Custom" = Table.AddColumn(#"Transposed Table", "Custom", each let x = Table.RowCount([Column1]) in 
List.Generate(
    ()=>0,
    each _<=x-1,
    each _+1
)),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    Custom1 = Table.TransformRows(#"Expanded Custom",each let c = _[Custom] in
[Column1=_[Column1][ColumnID]{c},Column2=_[Column2][ColumnID]{c},Column3=_[Column3][ColumnID]{c}]

),
    #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"Column1", "Column2", "Column3"}, {"Column1.Column1", "Column1.Column2", "Column1.Column3"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Column1",{{"Column1.Column1", "Column1"}, {"Column1.Column2", "Column2"}, {"Column1.Column3", "Column3"}})
in
    #"Renamed Columns"

 

Result:

a2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks for the answer, 

 

Sorry for the late reply, solved my problem.

 

Unable to understand the below mentioned code, can you please help to explan.

 

= Table.TransformRows(#"Expanded Custom",each let c = _[Custom] in
[Column1=_[Column1][ColumnID]{c},Column2=_[Column2][ColumnID]{c},Column3=_[Column3][ColumnID]{c}]

)

 

Thanks

Siva

Fowmy
Super User
Super User

@siva_powerbi 

Can you share a sample file with the same status as you have and show the expected output?

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

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
Top Kudoed Authors