Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Power Query Cookbook

You're reading from   Power Query Cookbook Use effective and powerful queries in Power BI Desktop and Dataflows to prepare and transform your data

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781800569485
Length 412 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Andrea Janicijevic Andrea Janicijevic
Author Profile Icon Andrea Janicijevic
Andrea Janicijevic
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Chapter 1: Getting Started with Power Query 2. Chapter 2: Connecting to Fetch Data FREE CHAPTER 3. Chapter 3: Data Exploration in Power Query 4. Chapter 4: Reshaping Your Data 5. Chapter 5: Combining Queries for Efficiency 6. Chapter 6: Optimizing Power Query Performance 7. Chapter 7: Leveraging the M Language 8. Chapter 8: Adding Value to Your Data 9. Chapter 9: Performance Tuning with Power BI Dataflows 10. Chapter 10: Implementing Query Diagnostics 11. Other Books You May Enjoy

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.

Figure 2.5 – Get Data File section

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:

  1. Go to Get data and click on Excel workbook:

    Figure 2.6 – Get data Excel connector

  2. Navigate to your local folder where you saved the Excel file, select it, and open it:
    Figure 2.7 – Local folder view

    Figure 2.7 – Local folder view

  3. Once you open it, the following window will pop up:
    Figure 2.8 – Excel data preview

    Figure 2.8 – Excel data preview

    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.

  4. Each sheet will correspond to a query. From now on, you can perform all transformations as you would with any other data source type:
Figure 2.9 – Power Query interface

Figure 2.9 – Power Query interface

Let's add a connection to a CSV file:

  1. Click on Get data and select the Text/CSV connector:
    Figure 2.10 – Get data Text/CSV connector

    Figure 2.10 – Get data Text/CSV connector

  2. 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:
    Figure 2.11 – CSV data preview

    Figure 2.11 – CSV data preview

    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).

    Figure 2.12 – Define the file encoding

    b) Delimiter: Select the right delimiter (in this case, we will keep the default Comma):

    Figure 2.13 – Define the delimiter

    Figure 2.13 – Define the delimiter

    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):

    Figure 2.14 – Define Data Type Detection

    Figure 2.14 – Define Data Type Detection

  3. On the bottom left of this window, you can also extract information from the CSV file by clicking on Extract Table Using Examples:
    Figure 2.15 – Extract Table Using Examples button

    Figure 2.15 – Extract Table Using Examples button

    The following section will appear:

    Figure 2.16 – Extract Table Using Examples interface

    Figure 2.16 – Extract Table Using Examples interface

  4. 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:
    Figure 2.17 – Insert values example

    Figure 2.17 – Insert values example

  5. 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:
    Figure 2.18 – Insert value detail example (left) and fill in missing or wrong values (right)

    Figure 2.18 – Insert value detail example (left) and fill in missing or wrong values (right)

  6. 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:
    Figure 2.19 – Create a second column example

    Figure 2.19 – Create a second column example

  7. At the end, click on OK and you will see the CSV in the Power Query interface as shown in the following screenshot:
Figure 2.20 – Power Query interface

Figure 2.20 – Power Query interface

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.

You have been reading a chapter from
Power Query Cookbook
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781800569485
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime