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 a database

This recipe shows how to connect to a database and how tables and views are displayed while selecting which tables to display and work with in Power Query.

You have two generic options:

  • Select tables or views as you would see them with a database viewer such as SQL Server Management Studio.
  • Retrieve tables by writing SQL statements in a specific section that will pop up.

Getting ready

In this recipe, in order to connect to a SQL database, you need to have an Azure SQL Database instance with AdventureWorks data, database credentials, or access through Azure Active Directory authentication (log in with your Microsoft account).

How to do it...

Once you open the Power BI Desktop application, you are ready to perform the following steps:

  1. Go to Get data, click on More, and browse for Azure SQL database:
    Figure 2.32 - Azure Connectors section

    Figure 2.32 - Azure Connectors section

  2. Enter the following information (expanding Advanced options):

    a) Server: Server name

    b) Database: Adventureworks

    c) Data Connectivity mode: Import

    d) SQL statement: This is a SQL view executed using the data source based on two tables in the database. The view is built as a SQL join between FactResellerSales and DimSalesTerritory:

    SELECT s.[ProductKey]
          ,s.[SalesTerritoryKey]
          ,s.[SalesOrderNumber]
          ,s.[SalesOrderLineNumber]
          ,s.[RevisionNumber]
          ,s.[OrderQuantity]
          ,s.[UnitPrice]
          ,s.[ExtendedAmount]
          ,s.[UnitPriceDiscountPct]
          ,s.[DiscountAmount]
          ,s.[ProductStandardCost]
          ,s.[TotalProductCost]
          ,s.[SalesAmount]
          ,s.[OrderDate]
          ,p.[SalesTerritoryRegion]
          ,p.[SalesTerritoryCountry]
          ,p.[SalesTerritoryGroup]
      FROM [dbo].[FactResellerSalesXL_CCI] s
    LEFT OUTER JOIN [dbo].[DimSalesTerritory] p ON s.[SalesTerritoryKey] = [p.SalesTerritoryKey]
  3. Copy and paste the code in the SQL statement section in order to get this view as the output table you will work on in Power Query:
    Figure 2.33 – SQL Server database

    Figure 2.33 – SQL Server database

  4. Enter authentication details:
    Figure 2.34 – SQL Server database authentication

    Figure 2.34 – SQL Server database authentication

  5. After signing in, the output of the SQL statement will pop up as follows:
    Figure 2.35 – Table preview

    Figure 2.35 – Table preview

  6. Click on Transform Data in order to open the Power Query interface:
    Figure 2.36 – Data preview in Power Query

    Figure 2.36 – Data preview in Power Query

  7. Click on Get data and select the connector Azure SQL Database. In this case, we won't enter a SQL statement, but we will select an existing table in the database:
    Figure 2.37 – SQL Server database connector

    Figure 2.37 – SQL Server database connector

  8. After signing in, a preview interface will appear, and you will be able to select the tables that you want to open in Power Query after clicking on OK:
    Figure 2.38 – SQL Database Navigator

    Figure 2.38 – SQL Database Navigator

  9. You will see on the left a set of queries as an output of connecting directly to the database tables and writing a SQL statement querying the database as you would do with any other database viewing tool:
Figure 2.39 – Data preview in Power Query

Figure 2.39 – Data preview in Power Query

If you open Advanced Editor for both types, you will notice that if you need to change the SQL code or you have to change the table name, you can do that directly from the Advanced Editor window:

a) The SalesData table's Advanced Editor code where you can see the details of the query run against the data source:

Figure 2.40 – Advanced Editor code for a SalesData query

Figure 2.40 – Advanced Editor code for a SalesData query

b) The DimDate table's Advanced Editor code where you can see the details of the connection Source, the database retrieved from the server, Adventureworks, and the table selected from the data source DimDate:

Figure 2.41 – Advanced Editor code for the DimDate Query

Figure 2.41 – Advanced Editor code for the DimDate Query

How it works...

The Azure SQL Database connector also reflects how other database connectors work. If you connect to Amazon Redshift or an Oracle database, the experience will be very similar. Power Query provides a wide range of options for relational data sources and some of them may need the installation of specific drivers. For example, if you connect to SAP or Oracle, you have to install additional components (for example, in Oracle, the additional components will be the Oracle Data Access Components (ODAC)).

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 €18.99/month. Cancel anytime