Power Query Appending new CSV files to an EXISTING Excel table

meg_a_bytes

New Member
Joined
Jan 11, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have researched but can't find specific instructions on how to append new monthly CSV files to an existing Excel Table. The table is a collection of all of the previous month's CSV's. All of the example i have found explain how to import CSV's to a new table. I have been doing this in the past but now the list of existing CSV's (old and new) is getting long and i don't see a need to re-import previous month CSV's. My current workflow is to extract, transform the CSV's to make them all consistent then load them to a new worksheet and table. Is there a way of preserving the previous month's table and just adding the new month's CSV's? I'm not interested in in doing any VBA code if that is the only solution.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Power Query was made for a job like this. Hopefully the CSV files are all in the same directory, in which case you can use Get Data -> From File -> From Folder. You'll be able to get this set up, and with a little work all you'll have to do is click Refresh All every month! I recommend YouTube playlists here and here. The playlists are long, but there's a good chance only a few are needed. I prefer the latter playlist because before and after example files are provided. And, of course, there's plenty of talent here. Post some data, I'm pretty sure it will be worth it!
 
Upvote 0
It looks like the solution that is most appropriate was from the "Bark01" thread. From what i can understand, the new month's CSV's would be Power Queried into a stand alone workbook then you would open the workbook that contains all of the previous month's data and execute a second PQ to append the current month data into it. Sounds reasonable. I was assuming this could be done with one PQ in one workbook.
Thanks for responding.
 
Upvote 0
It looks like the solution that is most appropriate was from the "Bark01" thread. From what i can understand, the new month's CSV's would be Power Queried into a stand alone workbook then you would open the workbook that contains all of the previous month's data and execute a second PQ to append the current month data into it. Sounds reasonable. I was assuming this could be done with one PQ in one workbook.
Thanks for responding.
No, as I indicated, if all the CSV files are in a given folder, and if there are other CSV files in that folder can be easily filtered by name - such as starts with Sales, one time with PQ could pull all of them in and append them into a single table. Once set up, new files could be appended by just refreshing the table. Haven't seen Bark01's thread, so don't know what he's talking about.
 
Upvote 0
I understand, but can it pull all of the filtered files into an existing table (with previous month data) in a different workbook?

Bark01:
 
Upvote 0
I understand, but can it pull all of the filtered files into an existing table (with previous month data) in a different workbook?

Bark01:
Yes. In fact a different file makes it easier. Attempting to pull in data from the existing workbook is slightly trickier because you have to be sure to filter out the final Worksheet/Table, or else you end up with a very big mess!
This is the code I put together with nothing but the user interface to import and Append (put one CSV file on top of the other) in a few minutes:
Power Query:
let
    Source = Folder.Files("C:\Users\XXXXX\Excel\Sales\CSV2011-2018"),
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Content"}),
    #"Filtered Hidden Files1" = Table.SelectRows(#"Removed Other Columns", each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
    #"Removed Other Columns1" = Table.SelectColumns(#"Invoke Custom Function1", {"Transform File"}),
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Table Column1", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"SalesRepID", Int64.Type}, {"ProductID", Int64.Type}, {"ChannelID", Int64.Type}, {"SegmentID", Int64.Type}, {"RegionID", Int64.Type}, {"Date", type text}, {"Quantity", Int64.Type}, {"Discount", Percentage.Type}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Date", type date}}, "en-GB")
in
    #"Changed Type with Locale"
There is NO hand coding. The only tricky thing was that the Date column was in UK format (dd-mm-yy) so had to use the Locale option to convert it to a US date. There are over NINE MILLION rows of data there from 10 CSV files - one per year. This literally took minutes - LESS time than it took to post this!
That much data had to be loaded as a Connection Only and added to the Data Model so that a Power Pivot report could be created. With less than a million rows of data, it could be loaded to a table in a New Worksheet. Then when subsequent years (or months) are added to the Folder, open the file with the full table, and click Data -> Refresh All to add the new year's/month's data.
Note that when expanding the data, several queries as well as most of the steps above are created. One of those is a Transform Sample File query. Some changes can be made to that and will be done to all files transformed, but one thing that doesn't come from it is the Data Types which is why they're in the final query.
The CSV files were never opened for this.
 
Upvote 0
Yes. In fact a different file makes it easier. Attempting to pull in data from the existing workbook is slightly trickier because you have to be sure to filter out the final Worksheet/Table, or else you end up with a very big mess!
This is the code I put together with nothing but the user interface to import and Append (put one CSV file on top of the other) in a few minutes:
Power Query:
let
    Source = Folder.Files("C:\Users\XXXXX\Excel\Sales\CSV2011-2018"),
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Content"}),
    #"Filtered Hidden Files1" = Table.SelectRows(#"Removed Other Columns", each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
    #"Removed Other Columns1" = Table.SelectColumns(#"Invoke Custom Function1", {"Transform File"}),
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Table Column1", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"SalesRepID", Int64.Type}, {"ProductID", Int64.Type}, {"ChannelID", Int64.Type}, {"SegmentID", Int64.Type}, {"RegionID", Int64.Type}, {"Date", type text}, {"Quantity", Int64.Type}, {"Discount", Percentage.Type}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Date", type date}}, "en-GB")
in
    #"Changed Type with Locale"
There is NO hand coding. The only tricky thing was that the Date column was in UK format (dd-mm-yy) so had to use the Locale option to convert it to a US date. There are over NINE MILLION rows of data there from 10 CSV files - one per year. This literally took minutes - LESS time than it took to post this!
That much data had to be loaded as a Connection Only and added to the Data Model so that a Power Pivot report could be created. With less than a million rows of data, it could be loaded to a table in a New Worksheet. Then when subsequent years (or months) are added to the Folder, open the file with the full table, and click Data -> Refresh All to add the new year's/month's data.
Note that when expanding the data, several queries as well as most of the steps above are created. One of those is a Transform Sample File query. Some changes can be made to that and will be done to all files transformed, but one thing that doesn't come from it is the Data Types which is why they're in the final query.
The CSV files were never opened for this.
This is the Pivot Table that Data Model was able to generate:
Book1big.xlsx
AB
1YearQuantity
220113,824,840
320124,053,304
420133,863,882
520144,462,822
620154,316,000
720164,460,736
820174,399,717
920184,435,346
1020194,601,320
1120204,587,494
12Grand Total43,005,461
CSV2011-2018

One nice thing about using the Data Model is that the format of the Date and Quantity can be set in the Data Model, not in the Pivot Table report!
 
Upvote 0
I'm not following what your trying to do so let me take one last stab at explaining this. I receive 8 csv's from different financial institutions every month. The csv's have different layouts so my first step is to ETL each csv and put them all into a common column format. The 8 etl's are in 1 workbook (lets call it wkbETL) where I save and load all of the 8 etl's to a new table (tblNewMonth).
I then open and existing workbook (wkbReporting). It contains a one table (tblTransactions) with all of the previous months data that was captured and ETL'd as described above. It also contains dashboards and pivot tables that refer to the tblTransactions name, so changing that table name (or creating a new table with the new and old data) is not really an option.
I simply want to append tblNewMonth from wkbETL to the bottom of tblTransactions in wkbReporting.

Is the solution to simply copy/paste tblNewMonth to the bottom of tblTransactions? Sounds pretty "unsophisticated".

Thanks for your patience.
 
Upvote 0
i was not implying that YOUR solution was to copy anything. I was coming to the conclusion that if i created tblNewMonth, i could simply cut and paste it into tblTransactions!
relax
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top