Microsoft Fabric Community Conference 2025, March 31 - April 2, Las Vegas, Nevada. Use code FABINSIDER for a $400 discount.
Register nowThe Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.
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
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the February 2025 Power BI update to learn about new features.
User | Count |
---|---|
27 | |
26 | |
18 | |
12 | |
10 |
User | Count |
---|---|
27 | |
24 | |
23 | |
17 | |
13 |