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
FredEff
New Member

Combine csv files with different amount of colums

Hi,

 

I have a csv file for every month of the year.

In every csv file I have the following structure:

 

Date01.01.2401.01.2402.01.2403.01.2404.01.2405.01.2405.01.24
Amount1231142

 

I use power query to transform the sample file into this:

 

DateAmount
01.01.241
01.01.242
02.01.243
03.01.241
04.01.241
05.01.244
05.01.242

 

The problem I am facing is, that the sample file will determine the amount of colums it will add also for the other files.

So If the data for ex. februrary will also have data for more days, it wont recognize the data as the sample file only had 7 colums.

 

Is there a way that Power Query will import all colums for all the files?

 

Thanks!

1 ACCEPTED SOLUTION
Omid_Motamedise
Super User
Super User

Consider this which used From Folder feature for extracting CSV files from a folder with different number of columns as below.



 

Omid_Motamedise_0-1725836645318.png

 

 

loading them into Power query results in 

Omid_Motamedise_1-1725836672966.png

 

remove all the column except Content, then add a new column (from add column tab, pick custom column) and use the below formula.

 

Table.Transpose(Csv.Document([Content]))

 

which result in the transppose version for all the csv files like below image

 

Omid_Motamedise_2-1725836791111.png

you can revise the previous formula to the next formula to consider the first row as a header.

Table.PromoteHeaders(Table.Transpose(Csv.Document([Content])))

 

which change the result into the below image,

Omid_Motamedise_3-1725836866666.png

 

 

now expand this new column you can reach the result

 

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

View solution in original post

6 REPLIES 6
Omid_Motamedise
Super User
Super User

Consider this which used From Folder feature for extracting CSV files from a folder with different number of columns as below.



 

Omid_Motamedise_0-1725836645318.png

 

 

loading them into Power query results in 

Omid_Motamedise_1-1725836672966.png

 

remove all the column except Content, then add a new column (from add column tab, pick custom column) and use the below formula.

 

Table.Transpose(Csv.Document([Content]))

 

which result in the transppose version for all the csv files like below image

 

Omid_Motamedise_2-1725836791111.png

you can revise the previous formula to the next formula to consider the first row as a header.

Table.PromoteHeaders(Table.Transpose(Csv.Document([Content])))

 

which change the result into the below image,

Omid_Motamedise_3-1725836866666.png

 

 

now expand this new column you can reach the result

 

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

Thank you. This will work. 

Before transposing, I would also need to combine the first 4 columns. 

Can you also help with that? 🙂 

Sure, in this case, you just need to add another column by using the below formula.

Table.FirtsN(Csv.Document([Content]),4)

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
lbendlin
Super User
Super User

Yes.  You can ignore the "Combine Files"  option that uses the sample file, and create your own process.  Be aware though that Power Query by default includes the number of columns in the meta data for the CSV connection, so you need to remove that attribute to handle variable number of columns.

Hi and thanks for the reply.

Do you mean, I should not even use the "combine files" function? Or should I just ignore the sample file?

The problem I have then is:

It will paste all files under each other and if I transform then, it just paste all the different files into colums, so I cant work with the data...

yes, ignore that function completely. It does way more harm than good.

 

- connect to the folder

- filter for the CSV files you want

- add a custom column that connects to the CSV file  (without the column limit) and unpivots the columns

- Expand that custom column

 

Done.

 

If you like you can provide two sample files (or their sample content).

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.