Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin 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.
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?
Solved! Go to Solution.
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]))
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.
@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?
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
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
13 | |
13 | |
10 | |
8 | |
7 |
User | Count |
---|---|
17 | |
10 | |
7 | |
7 | |
6 |