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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DanFromMontreal
Helper III
Helper III

Reordering column using a use define table

Hello dear Power query community,

When I'm importing data from a CSV file and my 6 column header name are sequenced as shown in E3:J3

My final output should be like E5:J5 

I have rearrange my columns manually but since I'm learning the PoweM language, I'm sure there is a better way of doing this.

I thought of a table defining the sequence..  This could come handy if my next file is containing more columns (>20).

for the column name that ARE NOT defined in the table, they should be place at the far right side

 

Any idea?

 

 

DanFromMontreal_0-1649951233657.png

 

2 ACCEPTED SOLUTIONS

Yes, that column is redundant. Use this.

= Table.ReorderColumns(Source,List.Select(tblHeaderSequence[Header], (i)=> List.Contains(Table.ColumnNames(Source),i))&List.Difference(Table.ColumnNames(Source),tblHeaderSequence[Header]))

 

View solution in original post

Great stuff.

Simple and to the point.

Many thanks

View solution in original post

7 REPLIES 7
Vijay_A_Verma
Super User
Super User

I have assumed that second column you will use to sort the first column in tblHeaderSequence

Use below formula to reorder your columns. 

= Table.ReorderColumns(Source,tblHeaderSequence[Header]&List.Difference(Table.ColumnNames(Source),tblHeaderSequence[Header]))

 

@Vijay_A_Verma , worked almost like a charm.

If the column "Qty" was NOT present, I get an error.  Any way to overcome this without having to manually add the column?

Also, I noticed that you do not reference to the column "Column" to get the ordering.  I therefore assume that it is irrelevant and the order is set by where the column name is in the list. 

Anonymous
Not applicable


@DanFromMontreal wrote:

@Vijay_A_Verma , worked almost like a charm.

If the column "Qty" was NOT present, I get an error.  Any way to overcome this without having to manually add the column?


 the solution I proposed should solve the problem of the missing columnS.
Have you tried it?

Anonymous
Not applicable

let
    Source = Table.FromRecords({[CustomerID = 1, Phone = "123-4567", Name = "Bob"]}),
    colnames=Table.ColumnNames(Source),
    neworder={"Phone", "Name"},
    rest=List.Difference(colnames, neworder),
    trc = Table.ReorderColumns(Source, neworder&{"qty"}&rest, MissingField.UseNull)
in
    trc

@Anonymous , thank you for your response but Vijay's solution meet all requirement.

His code made reference to my tblHeaderSequence and was applicable immediately, regardless of which column his missing.

 

Yes, that column is redundant. Use this.

= Table.ReorderColumns(Source,List.Select(tblHeaderSequence[Header], (i)=> List.Contains(Table.ColumnNames(Source),i))&List.Difference(Table.ColumnNames(Source),tblHeaderSequence[Header]))

 

Great stuff.

Simple and to the point.

Many thanks

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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