In this article, we will show you, step by step approach to connect Power BI to SQL Server Database. In addition, we will show you, how to use SQL Queries against the data present in SQL Tables with example.
Step by Step approach to Connect Power BI to SQL Server
If you haven’t started the Power BI Desktop yet, Double click on the Power BI desktop to open. Once it is open, it looks like below screenshot. Please click on the Get Data hyperlink
If you are in the Power BI visualization page. Under the Home tab, Click the Get Data option and select the SQL Server as we shown below. Or, select More option
Selecting the More option will show you the following window. Use this window to select your desired Data source. For now, let me select the SQL Server Database Option
SQL Server Database
- Server: Please provide the SQL Server Instance name. If you installed SQL with default instance, then the instance name will be computer name, or localhost or .
- Database: This is optional. If you want to use custom SQL query then it is required.
- Data Connectivity Mode: Please select whether you want to import or Direct query.
Clicking the Advanced option will show you the advanced properties.
You can write your own SQL Query to extract data from the specified Database. To do this, write your own SQL query inside the SQL Statement box
Custom SQL Query that we used in the above screenshot is:
-- Connecting Power BI to SQL Server SELECT Geo.EnglishCountryRegionName AS Country, Geo.StateProvinceName AS State, SUM(Fact.SalesAmount) AS Sales, SUM(Fact.TaxAmt) AS Tax, COUNT(Fact.OrderQuantity) AS Orders FROM FactResellerSales AS Fact INNER JOIN DimSalesTerritory ON Fact.SalesTerritoryKey = DimSalesTerritory.SalesTerritoryKey INNER JOIN DimGeography AS Geo ON DimSalesTerritory.SalesTerritoryKey = Geo.SalesTerritoryKey GROUP BY Geo.EnglishCountryRegionName, Geo.StateProvinceName HAVING COUNT(Fact.OrderQuantity) > 60000
If you want to use this data then click Load. For now, let me click cancel button because, I want to select multiple tables.
NOTE: Don’t worry above the SQL Query, Just understand the procedure. However, If you want to learn, Please refer our SQL Server Tutorial
Let me use the local instance, and click OK
For now, we are using the current local windows credentials. If you don’t have the permission to access SQL Server, then use other person windows credential by selecting Use alternative credentials.
In real-time, we only use SQL Server Database credentials. For this, select database, and enter the username and password provided by the Admin person.
For now, I am using the current windows credentials
Under the Display options, it will list out the Databases that are available for that Instance. In this example, We are selecting AdventureWorksDW2017 database. You can download this database from Microsoft official Download Center, or GitHub for free.
Expanding the Database folder will display the available Tables, and Views. Selecting the table will show the Data Preview.
From the below screenshot you can see that, we are selecting required tables. Remember, if you are confused to select tables, then select one Measure table (Orders, or Sales) and click Select Related Tables button.
- Load: This will load data from selected tables to Power BI. As I know that the data is clean, let me click Load button
- Edit: Click this button to alter the columns, or to cleanse the data
Please wait until the load is complete
Now you can see the Tables that we selected from the SQL Server database under the Fields section.
Expand any table to see the columns (fields) inside that table.
TIP: This is the Data set that we are going to use in most of the Power BI tutorial.
Thank You for Visiting Our Blog