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:
- Go to Get data, click on More, and browse for Azure SQL database:
- 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
andDimSalesTerritory
: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]
- 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:
- Enter authentication details:
- After signing in, the output of the SQL statement will pop up as follows:
- Click on Transform Data in order to open the Power Query interface:
- 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:
- 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:
- 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:
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:
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
:
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)).