First function in Power Query¶

Functions in Power Query as in other programming languages is used for some actions which combined physically in some stack that could be repeated multiple times.

Today we present example how implement function that returns a files list. In this example we will use MS Office 365, Power Query is integrated in this program product. After run of the MS Excel and navigate to the tap “Data” we could use functionality of the Power Query:

<caption>

We will divide our task into subtasks:

  1. Get list of files from a specific folder;
  2. Rewrite the request in a such way that it takes as a parameter the path to the target folder;
  3. Create a parameter that contains a value of the target folder path.

For subtask #1 we have to navigate to the tab Data then click on: Get Data->From File->From Folder:

<caption>

For example, we create a temp folder on the disk C destination. In your case you could use your working folder. There are no additional requirements. The folder could be also at the network folder. When Power Query connects to the target folder you will see a table with files content. At this step notice that in the next steps we will keep only two columns: Name and Folder Path.

<caption>

At the next step you should click on the button "Transform Data":

<caption>

When you click on Transform Data you will navigate to Power Query Editor. To the left side we have query temp_folder_for_tutorials. There is a generated table in the middle of the editor.

<caption>

To the right of the editor we have Query Settings. In this section we rename our generated query. Our new name of the query is temp_folder_function:

<caption>

Select all columns except: Name, Folder Path.

<caption>

We should delete unnecessary columns:

<caption>

<caption>

We add some additional column at the next step:

<caption>

We specify the name of the column "FullPath":

<caption>

<caption>

We should to concatenate two field, use operator of concatenation &. Our final formula [Folder Path] & [Name]

<caption>

<caption>

We completely finish our subtask #1.

Subtask #2. Transform query to function. Navigate to Advanced Editor:

<caption>

<caption>

You need to define an input variable for the function:

<caption>

Next, you need to change the constant of the Folder.Files function - "C:\temp_folder_for_tutorials" (you will have the path to the folder that you specified during execution subtask #1).

<caption>

You need to check that there is a checkmark “No syntax errors have been detected”:

<caption>

Then you should click on "Done" button. Then we navigate to Power Query Editor:

<caption>

Close Power Query Editor and Keep changes:

<caption>

Subtask #2 is completed.

First of all, we have to create table. It will contain our parameter for the folder path:

<caption>

<caption>

<caption>

We should to generate a query that connects to the Table1 at the Sheet1. Navigate to the tab Data and click button From Table/Range

<caption>

Program generated a query, named "Table1":

<caption>

Next, we should transform the table into list and then change formula. In this case we will use {0} - get first element from sequence ({1} - get second element from sequence, and etc.):

<caption>

<caption>

Finally, we have to create acnew query for our final result table:

<caption>

In [ ]:

<caption>

<caption>

<caption>

We create our final table. Exit Query Editor and Keep changes:

<caption>

Our subtask #3 and example is successfully completed:

<caption>

See new examples for Power BI and Power Query at https://virtualanalytics.azurewebsites.net/ Thank you!