Creating a query from files
Power Query users (when they start to use and explore the tool) usually start by connecting to a local file. They can see from the start that the main file types are supported and each of these will display data in a readable format.
In this recipe, we will connect to an Excel file and see how to navigate and expand the different sheets and how to connect to cut-off text/CSV files.
Getting ready
In this recipe, in order to test different types of file connectors, you need to download the following files in a local folder:
- The
AdventureWorksSales
Excel file - The
FactResellerSales
CSV file
In this example, we will refer to the C:\Data
folder.
How to do it...
Once you have opened your Power BI Desktop application, perform the following steps:
- Go to Get data and click on Excel workbook:
- Navigate to your local folder where you saved the Excel file, select it, and open it:
- Once you open it, the following window will pop up:
Each item in the left pane matches an item in the Excel file. By only clicking on an item, you will see a preview of the data in the right pane and if you check it, you will include the item in the Power Query view. Therefore, flag the following queries: Customer, Date, and Product. Click on Transform Data.
- Each sheet will correspond to a query. From now on, you can perform all transformations as you would with any other data source type:
Let's add a connection to a CSV file:
- Click on Get data and select the Text/CSV connector:
- Navigate to the local folder where you saved the
FactResellerSales
CSV file. Select it and open it as in the previous section with the Excel file. The following window will pop up:For each file, you can define the following:
a) File Origin: Define the file encoding (in this case, we will keep the default Unicode UTF-8).
b) Delimiter: Select the right delimiter (in this case, we will keep the default Comma):
c.) Data Type Detection: This will refer to the first applied step in Power Query when it detects data types for each column (in this case, we will detect data types based on the first 200 rows):
- On the bottom left of this window, you can also extract information from the CSV file by clicking on Extract Table Using Examples:
The following section will appear:
- You can define your columns and which data to extract by filling in the table at the bottom. Have a look at the following example: name the first column ResellerKey and write in the first row the value
676
, which is the first ResellerKey value you see in the example, and click on Enter: - If you look at row 5 in Figure 2.18 (the left image), you can see that a wrong value has been detected. In this case, you can click on it and insert the right one and you will observe how all values in the column will be corrected:
- You can add a second column and repeat the steps done with the first. Name the second column EmployeeKey and insert the first value. Click Enter and you will see the corresponding rows filled:
- At the end, click on OK and you will see the CSV in the Power Query interface as shown in the following screenshot:
In the APPLIED STEPS section, you will see some activities mapped as a result of Extract Table Using Examples performed previously.
How it works...
Power Query, thanks to these file connectors, allows users to connect to single files and perform some pre-transformation tasks allowing them to load just relevant data in the usual interface. However, these connectors – Excel, TXT/CSV, and also Parquet file are related to single files. We will see in the following recipe how to connect to multiple files.