Power BI | Importing multiple files

Getting Started

To import multiple files from a folder, the following two steps had to be done:

  • create a list of all files in the folder
  • for each file: read the file and add it to the result table

When importing files with Power BI, you can do both tasks together or each task separately.

The decision, which way to go, ist done after selection the folder:

In order to better understand the process, we show the two steps separately: one after the other

Load the list of files from folder

Start Power BI and close the start screen, if it is still visible.

Then, click on the Get Data Button in the Home Ribbon

Now, here is the point to decide, which way to go:

  • Combine
    • Read list of files and combine all files into on table
  • Load
    • Just keep the list of files and return to Power BI
  • Transform
    • Keep the list of files and open the Power Query Editor

We will choose to load the files, because we will do each step later separately

Combine all files into one table

To add additional steps, we need the Power Query Editor.

So click on the 3 dots at the right side of the Query name Samples and choose Edit Query

Click on OK to start the import.

Additionally, you will see four other elements:

Each query consists of a list of steps, which are process one after another. Normaly, each step is using the result (data) of the previous step, performs some modifications and has a result (data) for the next step.

So, each step is modifying the whole data of the previous step. Describing some modifications means either

  • do one thing, e.g. add an additional column

or

  • do something for each row in the data
    This means, we need some sort of a loop, like „do xyz for each row in the data

Lets see, how Power Query solves this task.

In the query Samples, exampine the Step Invoke Custom Function1

  • table: which is normaly the name of the prevoius step
    In our example #“Filtered Hidden Files1″
  • newColumnName: the name for the column to be added
    „Transform File“
  • columnGenerator: a function which is called for each row in the input table and creates the new column content
    each #“Transform File“([Content])

This results in the following procedure:

  • for each row of the list of files (output from step #“Filtered Hidden Files1″)
  • get the content of the column Content (this will be the parameter for the function call)
  • call the function „Transform File“([Content]) to create the column with one parameter: the value of the column ([Content] i

Helper Queries (Required)

Helper queries (Optional)

For the resulting query Samples to work, only the function definition is required.

But Power Query add some additional elements, to test the function and show the result

If the selected folder contains files with different format, the result is not what you may be expect:

The list of files contains all files, both csv files and xls files

But, looking at the resulting query, the data of the xls files still looks strange:

<pre class="EnlighterJSRAW" data-enlighter-group="" data-enlighter-highlight="" data-enlighter-language="csharp" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-theme="" data-enlighter-title="">Source = Csv.Document(Parameter3,[Delimiter=",", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None]),

The source document (each file in the list of files) is interpreted as csv file.

So, the xls files are also read in as csv files. This leads to the strange result.

You can fix this by adding an additional filter step in the query to select only csv files:

The Latest