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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. 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
Resident Rockstar
Resident Rockstar

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

 

 

View solution in original post

6 REPLIES 6
Omid_Motamedise
Resident Rockstar
Resident Rockstar

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

 

 

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)

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Kudoed Authors