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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
DanFromMontreal
Helper IV
Helper IV

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors