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
k_mathana
Helper II
Helper II

Batch Import excel files with different column name

Hi

I have trouble to perform batch import from multiple excel files where some file column names differs for example in some file table column mentioned as "Week ID" and in some files "week" and in some files "WEEK" in some some files just "ID" and they are not in the same column some files it is in "col1" in some files it is in "col5' etc. How to manage? this I need you guidelines on this Thanks in advance

4 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
Community Champion

Hello @k_mathana 

 

you can rename them one by one and use the "try otherwise" statement to handle the errors. Your new code could look something like this

    RenamedWeekID = try Table.RenameColumns(PreviousStep,{{"WeekID", "Week"}}) otherwise PreviousStep,
    RenameID = try Table.RenameColumns(RenamedWeekID,{{"ID", "Week"}}) otherwise RenamedWeekID

 

here the complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkktLlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WeekID = _t]),
    PreviousStep = Table.TransformColumnTypes(Source,{{"WeekID", type text}}),
    RenamedWeekID = try Table.RenameColumns(PreviousStep,{{"WeekID", "Week"}}) otherwise PreviousStep,
    RenameID = try Table.RenameColumns(RenamedWeekID,{{"ID", "Week"}}) otherwise RenamedWeekID
in
    RenameID

 

a more elegant solution would be to maintain a translation table (see my step TransformTable) and filter the old value column by your column names of the table to be translated and apply the result in a Table.SelectRows. Here the code

    TransformTable =  #table({"old", "new"},{{"WeekID", "Week"}, {"ID", "Week"}}),
    FilterForWeek = Table.ToRows(Table.SelectRows(TransformTable, each List.Contains(Table.ColumnNames(PreviousStep), _[old]))),
    RenameWeek = try Table.RenameColumns(PreviousStep, FilterForWeek) otherwise PreviousStep

 

here a complete example

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkktLlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WeekID = _t]),
    PreviousStep = Table.TransformColumnTypes(Source,{{"WeekID", type text}}),
    TransformTable =  #table({"old", "new"},{{"WeekID", "Week"}, {"ID", "Week"}}),
    FilterForWeek = Table.ToRows(Table.SelectRows(TransformTable, each List.Contains(Table.ColumnNames(PreviousStep), _[old]))),
    RenameWeek = try Table.RenameColumns(PreviousStep, FilterForWeek) otherwise PreviousStep
in
    RenameWeek

 Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

Dear Jimmy

Thanks for the response, Yes that is the solution I am looking for. my hurdle is I am not an expert so I am finding hard when I try to connect to my folders and not able to modify your Intial source connecting lines, Could you please help?

View solution in original post

Hello @k_mathana 

 

my code you can apply to the tables read from a Excel-sheet and this what you asked for, thinking of that you have this already as a base. Depending if your Excel-files are all the same (one sheet called sheet1 etc.) you have to read first all Excel-files from your folder and then apply your query/function. As this request has nothing to do with your initial request I would propose to  mark my proposed solution as a solution to close this threat, you start out to understand your other requests you could have and open a new topic. Otherwise this here will be a never ending thread 🙂

 

Hope this is ok for you

 

Jimmy

View solution in original post

Hello

 

but please accept my solution as solution, and not your answer

 

many thanks

 

Jimmy

View solution in original post

6 REPLIES 6
Jimmy801
Community Champion
Community Champion

Hello @k_mathana 

 

you can rename them one by one and use the "try otherwise" statement to handle the errors. Your new code could look something like this

    RenamedWeekID = try Table.RenameColumns(PreviousStep,{{"WeekID", "Week"}}) otherwise PreviousStep,
    RenameID = try Table.RenameColumns(RenamedWeekID,{{"ID", "Week"}}) otherwise RenamedWeekID

 

here the complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkktLlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WeekID = _t]),
    PreviousStep = Table.TransformColumnTypes(Source,{{"WeekID", type text}}),
    RenamedWeekID = try Table.RenameColumns(PreviousStep,{{"WeekID", "Week"}}) otherwise PreviousStep,
    RenameID = try Table.RenameColumns(RenamedWeekID,{{"ID", "Week"}}) otherwise RenamedWeekID
in
    RenameID

 

a more elegant solution would be to maintain a translation table (see my step TransformTable) and filter the old value column by your column names of the table to be translated and apply the result in a Table.SelectRows. Here the code

    TransformTable =  #table({"old", "new"},{{"WeekID", "Week"}, {"ID", "Week"}}),
    FilterForWeek = Table.ToRows(Table.SelectRows(TransformTable, each List.Contains(Table.ColumnNames(PreviousStep), _[old]))),
    RenameWeek = try Table.RenameColumns(PreviousStep, FilterForWeek) otherwise PreviousStep

 

here a complete example

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKkktLlGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [WeekID = _t]),
    PreviousStep = Table.TransformColumnTypes(Source,{{"WeekID", type text}}),
    TransformTable =  #table({"old", "new"},{{"WeekID", "Week"}, {"ID", "Week"}}),
    FilterForWeek = Table.ToRows(Table.SelectRows(TransformTable, each List.Contains(Table.ColumnNames(PreviousStep), _[old]))),
    RenameWeek = try Table.RenameColumns(PreviousStep, FilterForWeek) otherwise PreviousStep
in
    RenameWeek

 Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Dear Jimmy

Thanks for the response, Yes that is the solution I am looking for. my hurdle is I am not an expert so I am finding hard when I try to connect to my folders and not able to modify your Intial source connecting lines, Could you please help?

Hello @k_mathana 

 

my code you can apply to the tables read from a Excel-sheet and this what you asked for, thinking of that you have this already as a base. Depending if your Excel-files are all the same (one sheet called sheet1 etc.) you have to read first all Excel-files from your folder and then apply your query/function. As this request has nothing to do with your initial request I would propose to  mark my proposed solution as a solution to close this threat, you start out to understand your other requests you could have and open a new topic. Otherwise this here will be a never ending thread 🙂

 

Hope this is ok for you

 

Jimmy

Dear Jimmy,

Thank you for the response!

Hello

 

but please accept my solution as solution, and not your answer

 

many thanks

 

Jimmy

Greg_Deckler
Super User
Super User

@k_mathana - Sounds like an utter nightmare Freddy Krueger style. Have fun @ImkeF and @edhans 

🙂

 

Yes, Krueger has 2 e's in it, I checked...



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

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.