DEV Community

Cover image for Connecting Power BI to SQL Databases: A Practical Guide for Data Analysts
Lawrence Murithi
Lawrence Murithi

Posted on

Connecting Power BI to SQL Databases: A Practical Guide for Data Analysts

Introduction

In most modern organizations, data is one of the most valuable assets. Companies collect large amounts of information from sales systems, websites, customer platforms, and operational databases. To make sense of this information, businesses use tools that can transform this raw data into clear insights. One of the most widely used tools for this purpose is the Microsoft Power BI platform.

What is Power BI

Power BI is a business intelligence and data visualization tool developed by Microsoft. It allows users to connect to different data sources, analyze data, and create interactive dashboards and reports. These reports help organizations monitor performance, understand trends, and support decision-making among other uses.

Power BI is commonly used by data analysts, business managers, and decision makers because it can present complex data in simple visual forms such as charts, tables, maps, and dashboards.

Most organizations store their operational and analytical data in SQL databases. SQL databases are designed to store large amounts of structured data in tables. They allow users to query, filter, update, and analyze data efficiently using Structured Query Language (SQL). SQL databases are reliable, secure, and scalable, hence they are widely used in business systems such as sales platforms, customer management systems, and inventory systems.

Connecting Power BI to a database allows analysts to access this stored data directly. Instead of manually exporting data into spreadsheets, Power BI can retrieve the data automatically, refresh it when the database changes, and build dashboards that always reflect the latest information.

This article explains how Power BI connects to SQL databases, how to connect to a local PostgreSQL database, how to connect to a cloud database such as Aiven PostgreSQL, and how the loaded data is modeled for analysis.

Understanding the Power BI Interface

Before connecting to a database, it is helpful to understand the Power BI Desktop interface. Power BI Desktop is the main application used for building reports and dashboards.
BI Desktop
The Power BI Desktop interface includes several sections such as:

  • Ribbon (Top Menu) – Contains commands and tabs such as Get Data, Transform Data, and Publish.
  • Report Canvas – The workspace where charts and dashboards are created.
  • Visualizations Pane – Used to select and customize charts.
  • Fields Pane – Displays the tables and columns loaded into Power BI.

You can download Power BI desktop app here.

Connecting Power BI to a Local PostgreSQL Database

PostgreSQL is one of the most popular open-source relational databases used in data analytics. Many organizations run databases locally on their own servers.
The steps below explain how to connect Power BI to a local PostgreSQL database.

Step 1: Open Power BI Desktop

Start by opening Power BI Desktop on your computer.
When the application opens, a blank report canvas appears. This is where you will build your report after loading the data.

Step 2: Get Data

On the Home tab of the ribbon, click Get Data.
This button opens a list of available data sources. Power BI supports many data sources including:

  • Excel
  • SQL Server
  • PostgreSQL
  • Web APIs The Get Data feature is the starting point for connecting Power BI to any data source. Other data sources are as shown on the image. Get Data

Step 3: Select PostgreSQL Database

From the list of available data connectors, click more to view more options. Scroll down, select PostgreSQL Database and click Connect.

Step 4: Enter the Database Connection Details

After selecting PostgreSQL and clicking connect, Power BI opens a connection window that requires connection details for the connection to go through.
Credentials
Server – The location of the database server. If the database is on your computer, use localhost.

Database – The name of the database you want to connect to.

Step 5: Provide Login Credentials

After a connection is made, Power BI will ask for authentication details.
You will need to provide:
Username
Password

These credentials were set up during installation of the PostgreSQL and allows Power BI to securely access the database.

Once the credentials are entered, click Connect.

Step 6: Select Tables to Import

After connecting successfully, Power BI opens the Navigator Window which displays all available tables in the database.
You can preview the contents of each table before loading them.
There are two options:
Load – Import the data directly.
Transform Data – Clean or modify the data before loading it.

Connecting Power BI to a Cloud Database (Aiven PostgreSQL)

Many organizations now store their databases in the cloud. Cloud databases are accessible through the internet and provide benefits such as scalability, backups, and easier management.
Aiven is a cloud platform that provides managed PostgreSQL databases.
Connecting Power BI to a cloud database is not different to connecting to a local database, only that additional security steps are required.

The steps below explain how to connect Power BI to an Aiven PostgreSQL database.

Step 1: Get the Database Connection Details from Aiven

Login to Aiven and inside the dashboard, you will find the connection information for your PostgreSQL service. These details are used by Power BI to locate and connect to the database.
Connection Details

Step 2: Download and install the SSL Certificate

Cloud database providers often require SSL encryption to secure the connection.
An SSL certificate ensures:

  • Data transferred between Power BI and the database is encrypted
  • Unauthorized users cannot intercept the connection
  • The database server identity is verified

In Aiven, download the certificate file(CA Certificate) from the Connection Information section of the service dashboard.
Rename the downloaded file from ca.pem to ca.crt and install the Certificate on your PC.
SSL Certificate
Choose Local Machine as the location of the installation and click next.
Local Machine
Choose place all certificates in the following store and browse certificate store to Trusted Root Certification Authorities.
Click ok and finish.
Store

Step 3: Connect Power BI

Open Power BI desktop as before, click Get Data, click more, scroll down and select PostgreSQL Database.
Copy the Server Name from the service URL(host_name:port_number) on Connection Information and paste on Power BI.
Input the name of your database and click ok.
server & db
Copy the username and password from Aiven, input them on the Power BI credentials window that opens and click connect.
Once the connection is successful, a navigator window opens and displays all tables in the database.
Select the tables you want to work with and click on load/transfrom data depending on what you wish to do with the data.
Transform data option is used to clean raw data e.g delete any duplicates and address null values using the most appropriate method.
Load data
Successfully loaded data displays on the data pane as shown on figure below.
Tables

Creating Relationships Between Tables

Once loaded, Power BI automatically detects relationships between tables based on matching columns using primary and foreign keys. Relationships not created can be manually created by dragging a column from one table onto the matching column in another table.
These relationships allow Power BI to combine information across multiple tables.
For example:
Connection

In the Model View, the tables appear as connected boxes. The relationships show how data flows between tables.
Relatioships

Data Modeling and Why its Important

Data modeling is the process of defining how data is stored, structured, and related within a database. It ensures that Power BI understands how different tables are related.

Good data modeling allows Power BI to:

  • Filter data correctly
  • Calculate totals accurately
  • Create meaningful visualizations
  • Avoid duplicated values

For example, when analyzing sales:

  • The sales table stores transaction records.
  • The customers table provides customer information.
  • The products table describes the items sold.

why SQL skills are important for Power BI analysts

Power BI is a powerful tool for building reports and dashboards, but it does not replace the need for strong data handling skills. Most business data is stored in SQL databases, and before that data can be visualized in Power BI, it must first be retrieved, cleaned, and structured properly.
SQL skills give Power BI analysts a real edge by providing an easier way to grab just what you need without pulling everything into Power BI.
Without SQL, analysts may rely too much on raw data, which can lead to slow reports, incorrect results, and inefficient workflows.
SQL allows analysts to:

1. Retrieve Data
Analysts can write queries to select specific rows and columns relevant to their analysis from a database.

-- selecting only products name and price columns from products table

SELECT product_name, price
FROM products
Enter fullscreen mode Exit fullscreen mode

Why this matters in Power BI:

  • Reduces the amount of data imported
  • Improves performance
  • Makes the model easier to manage
  • Avoids unnecessary columns

2. Filter Data
In real-world scenarios, not all data is useful for analysis. Analysts often need to focus on specific time periods, regions, or business conditions. SQL thus makes it easy to filter datasets based on a specific criteria before loading them into Power BI.

-- Retrieving only sales from 2024 onwards.

SELECT *
FROM sales
WHERE sale_date >= '2024-01-01'
Enter fullscreen mode Exit fullscreen mode

Why this matters:

  • Reduces dataset size
  • Speeds up report loading
  • Focuses analysis on relevant data
  • Avoids unnecessary processing inside Power BI

3. Perform Aggregations
Aggregation is the process of summarizing data. In business analysis, analysts often need totals, averages, counts, and other summary metrics. SQL can summarize large datasets quickly by using functions such as GROUP BY, SUM, COUNT, and AVG.

-- Calculating total sales per product

SELECT product_id, SUM(quantity) AS total_quantity
FROM sales
GROUP BY product_id;
Enter fullscreen mode Exit fullscreen mode

Why aggregation in SQL is important:

  • Reduces data volume before loading
  • Improves Power BI performance
  • Simplifies data models
  • Avoids heavy calculations in DAX

Preparing data for Analysis

Raw data must be cleaned or transformed before it is ready for visualization.
SQL can be used to:

Joining Tables and Combining Data

Business data is usually stored in multiple tables.
SQL allows analysts to combine these tables using joins. Joined datasets in SQL can simplify the data model.

SELECT c.customer_name, s.sales_amount
FROM customers c
JOIN sales s
ON c.customer_id = s.customer_id;
Enter fullscreen mode Exit fullscreen mode

Why this is important:

  • Combines related data into one dataset
  • Reduces the need for complex relationships in Power BI
  • Makes analysis easier
  • Prevents duplication errors

Data Cleaning and Preparation

Raw data is often messy. It may contain:

  • Missing values
  • Duplicate records
  • Incorrect formats

SQL helps clean and prepare the data before it is loaded into Power BI hence leading to better insights.

-- Eliminating duplicates

SELECT DISTINCT customer_id
FROM customers;
Enter fullscreen mode Exit fullscreen mode
--Handling missing values

SELECT 
  COALESCE(phone_number, 'Not Provided') AS phone
FROM customers;
Enter fullscreen mode Exit fullscreen mode

Why data cleaning matters:

  • Ensures data accuracy
  • Improves report reliability
  • Reduces cleaning work in Power BI
  • Prevents errors in calculations

Creating Calculated Fields

SQL allows analysts to create new columns based on existing data.

-- Calculate total sales

SELECT 
  product_name,
  price,
  quantity,
  price * quantity AS total_sales
FROM sales;
Enter fullscreen mode Exit fullscreen mode

Why calculated fields are useful:

  • Prepares key metrics before loading
  • Reduces need for DAX calculations
  • Keeps logic centralized in the database

Supporting Advanced Analysis

SQL also supports more advanced operations such as:

  • Window functions (running totals, ranking)
  • Subqueries
  • Common Table Expressions (CTEs)
  • Data transformations
SELECT 
  sale_date,
  SUM(sales_amount) OVER (ORDER BY sale_date) AS running_total
FROM sales;
Enter fullscreen mode Exit fullscreen mode

Conclusion

Power BI is a powerful tool that helps organizations transform raw data into meaningful insights. By connecting directly to SQL databases, Power BI allows analysts to access structured data stored in business systems and convert it into interactive dashboards and reports.
SQL prepares the foundation, and Power BI builds the story on top of it. Strong SQL skills allow analysts to work more efficiently, produce accurate reports, and deliver better insights for decision-making.
When SQL and Power BI are used together, they provide a powerful combination for modern data analysis and business intelligence.

Top comments (0)