0% found this document useful (0 votes)
12 views

SQLPowerBI en

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

SQLPowerBI en

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

1/12/2024 Data Analysis with

SQL and Power BI


Trường Đại học Ngoại thương

Msc Dao Quoc Phuong


FOREIGN TRADE UNIVERSITY HO CHI MINH CAMPUS
Table of Contents
Introduction........................................................................................................................................1
Module 1: DATA FOR EVERYONE ...................................................................................................2
Step 0: Install MySQL software .......................................................................................................3
Step 1: Import Awesome Chocolates Dataset ................................................................................3
Step 2: Learn SQL for Data Analysis with case study ....................................................................4
How to use SELECT statement to answer business questions .................................................4
Working with WHERE clause .....................................................................................................5
Sorting query results using ORDER BY .....................................................................................5
Using AND, OR, NOT and combining them to create complex queries .....................................5
Creating reports with GROUP BY ..............................................................................................7
Combining data from two or more tables using JOINS ..............................................................7
Conditions with JOINS ................................................................................................................7
GROUP BY with JOINS ..............................................................................................................9
Another option is having condition ............................................................................................10
The Queries ..................................................................................................................................10
Intermediate problems ..............................................................................................................10
Hard problems ..........................................................................................................................10
Solutions ...................................................................................................................................11
Homeworks ...................................................................................................................................13
Module 2: BUSSINESS INTELLIGENCE ANALYST ......................................................................15
How to load and clean-up data with Power BI ..............................................................................16
Understanding Power BI Desktop UI ........................................................................................16
Load data into Power BI ...........................................................................................................17
Data clean-up with Power Query ..............................................................................................17
How to build a data model in Power BI .........................................................................................18
Relationships ............................................................................................................................18
Measures ..................................................................................................................................19
Creating and changing visuals ......................................................................................................19
Adding Visuals ..........................................................................................................................19
Changing Calculations for the Visuals ......................................................................................21
Using DAX & Power Pivot to build measures and calculations ....................................................21
DAX...........................................................................................................................................21
Power Pivot ...............................................................................................................................22
Customizing visuals & interaction effects......................................................................................22
Creating a comprehensive sales performance report ...................................................................23
Adding conditional formatting ...................................................................................................24
Adding title for the report ..........................................................................................................25
Saving and publishing the Power BI files ......................................................................................25
How to share / publish your reports? ........................................................................................26
Updating and data refresh process ...............................................................................................26
Refresh .....................................................................................................................................26
Updating & Refreshing your Report ..........................................................................................26
Use SQL with Power BI ................................................................................................................27
Understanding trends & creating forecast in Power BI .................................................................28
Projects .........................................................................................................................................30
Ecommerce Sales Dashboard ..................................................................................................30
Amazon Product Review Dashboard ........................................................................................31
Customer Complaints Dashboard ............................................................................................32
Finance Report .........................................................................................................................32
Mall Sales Analytics ..................................................................................................................33
Introduction
The “Data Analysis with SQL and PowerBI” course is designed according to a training
path from basic to advanced, suitable for beginners who want to master the foundation
and have the most practical knowledge to get to work in a short time. Real combat training
method, both learning and practicing project work. After the course, you will be able to:

• Proficient in using popular data analysis tools: SQL, Power Bi. and have the
mindset to be able to use other tools easily.
• Master the process of searching and analyzing data to find insights to serve the
business's strategic decision making.
• Master the foundation to be able to advance quickly and further in the data
analysis industry with positions such as Data Engineer, Data Scientist,...
• Either way, if you are interested in gathering insights from any kind of dataset, it is
extremely likely that you have spent a lot of your time playing with Excel, pivot
tables, and formulas.

MODULE 1. DATA FOR EVERYONE


• Students grasp basic knowledge of databases and SQL language to retrieve data
as required.
• Apply learned knowledge and skills to solve real-life problems.

MODULE 2. BUSSINESS INTELLIGENCE ANALYST


• Students know how to use Power BI to visualize data in many different chart
formats and combine SQL language to create data sources.
• Know how to do business analysis at an advanced level, focusing on building and
automating complex analytical reports.
• Design advanced data models with high levels of complexity, find insights from
data and use insights to solve business problems.

1|Page
Module 1:
DATA FOR EVERYONE
Step 0: Install MySQL software
Step 1: Import Awesome Chocolates Dataset
Step 2: Learn SQL for Data Analysis with case study
The Queries

2|Page
Step 0: Install MySQL software

I am using the FREE MySQL Community Edition software to learn & practice SQL at
home. You can get it from here https://dev.mysql.com/downloads/installer/

If you have any other database software available (such as SQL Server or Oracle), you can
use them to follow this tutorial.

Step 1: Import Awesome Chocolates Dataset


You need some data to practice SQL. So I prepared a sample dataset for a fictional
company called Awesome Chocolates.

After you have the file,

1. Open MySQL Workbench, login if necessary


2. Click on the “server administration” tab (see illustration, click to expand)
3. Click on “Data Import/Restore”
4. Select the option “Import from self-contained file”
5. Specify the path of the downloaded awesome-chocolates-data.sql file
6. Start import

3|Page
At the end of these steps, your MySQL should have the awesome chocolates database.
Congratulations!
You can see this from “Schemas” tab on the workbench

Step 2: Learn SQL for Data Analysis with case study


SQL (Structured Query Language) is one of the most important skills for us, data people. So
in this article, get the necessary SQL skills you need for Data Analysis work.
How to use SELECT statement to answer business questions

CLTR + ENTER

4|Page
Working with WHERE clause

Sorting query results using ORDER BY

Using AND, OR, NOT and combining them to create complex queries

5|Page
6|Page
Creating reports with GROUP BY

Combining data from two or more tables using JOINS

Conditions with JOINS

7|Page
8|Page
GROUP BY with JOINS

9|Page
Another option is having condition

The Queries
Intermediate problems
You need to combine various concepts covered in the class to solve these
1. Print details of shipments (sales) where amounts are > 2,000 and boxes are <100?
2. How many shipments (sales) each of the sales persons had in the month of January 2022?
3. Which product sells more boxes? Milk Bars or Eclairs?
4. Which product sold more boxes in the first 7 days of February 2022? Milk Bars or Eclairs?
5. Which shipments had under 100 customers & under 100 boxes? Did any of them occur on
Wednesday?

Hard problems
These require concepts not covered in the class
1. What are the names of salespersons who had at least one shipment (sale) in the first 7
days of January 2022?
2. Which salespersons did not make any shipments in the first 7 days of January 2022?
3. How many times we shipped more than 1,000 boxes in each month?
4. Did we ship at least one box of ‘After Nines’ to ‘New Zealand’ on all the months?
5. India or Australia? Who buys more chocolate boxes on a monthly basis?

10 | P a g e
Solutions
INTERMEDIATE PROBLEMS
— 1. Print details of shipments (sales) where amounts are > 2,000 and boxes are <100?

select * from sales where amount > 2000 and boxes < 100;

— 2. How many shipments (sales) each of the sales persons had in the month of January
2022?

select p.Salesperson, count(*) as ‘Shipment Count’


from sales s
join people p on s.spid = p.spid
where SaleDate between ‘2022-1-1’ and ‘2022-1-31’
group by p.Salesperson;

— 3. Which product sells more boxes? Milk Bars or Eclairs?

select pr.product, sum(boxes) as ‘Total Boxes’


from sales s
join products pr on s.pid = pr.pid
where pr.Product in (‘Milk Bars’, ‘Eclairs’)
group by pr.product;

— 4. Which product sold more boxes in the first 7 days of February 2022? Milk Bars or
Eclairs?

select pr.product, sum(boxes) as ‘Total Boxes’


from sales s
join products pr on s.pid = pr.pid
where pr.Product in (‘Milk Bars’, ‘Eclairs’)
and s.saledate between ‘2022-2-1’ and ‘2022-2-7’
group by pr.product;

— 5. Which shipments had under 100 customers & under 100 boxes? Did any of them
occur on Wednesday?

select * from sales


where customers < 100 and boxes < 100;

select *, case when weekday(saledate)=2 then ‘Wednesday Shipment’ else ”


end as ‘W Shipment’
from sales
where customers < 100 and boxes < 100;

11 | P a g e
HARD PROBLEMS:
— 1. What are the names of salespersons who had at least one shipment (sale) in the
first 7 days of January 2022?

select distinct p.Salesperson


from sales s
join people p on p.spid = s.SPID
where s.SaleDate between ‘2022-01-01’ and ‘2022-01-07’;

— 2. Which salespersons did not make any shipments in the first 7 days of January
2022?

select p.salesperson
from people p
where p.spid not in
(select distinct s.spid from sales s where s.SaleDate between ‘2022-01-01’ and ‘2022-
01-07’);

— 3. How many times we shipped more than 1,000 boxes in each month?

select year(saledate) ‘Year’, month(saledate) ‘Month’, count(*) ‘Times we shipped 1k


boxes’
from sales
where boxes>1000
group by year(saledate), month(saledate)
order by year(saledate), month(saledate);

— 4. Did we ship at least one box of ‘After Nines’ to ‘New Zealand’ on all the months?

set @product_name = ‘After Nines’;


set @country_name = ‘New Zealand’;
select year(saledate) ‘Year’, month(saledate) ‘Month’,
if(sum(boxes)>1, ‘Yes’,’No’) ‘Status’
from sales s
join products pr on pr.PID = s.PID
join geo g on g.GeoID=s.GeoID
where pr.Product = @product_name and g.Geo = @country_name
group by year(saledate), month(saledate)
order by year(saledate), month(saledate);

12 | P a g e
— 5. India or Australia? Who buys more chocolate boxes on a monthly basis?

select year(saledate) ‘Year’, month(saledate) ‘Month’,


sum(CASE WHEN g.geo=’India’ = 1 THEN boxes ELSE 0 END) ‘India Boxes’,
sum(CASE WHEN g.geo=’Australia’ = 1 THEN boxes ELSE 0 END) ‘Australia Boxes’
from sales s
join geo g on g.GeoID=s.GeoID
group by year(saledate), month(saledate)
order by year(saledate), month(saledate);

Homeworks
 Import Adventure Works Dataset
Schema: humanresources

• department, employee, employee, departmenthistory, employeepayhistory,


jobcandidate, shift

Schema: person

• address, addresstype, businessentity, businessentityaddress,


businessentitycontact, contacttype, countryregion, emailaddress, person,
personphone, phonenumbertype, stateprovince

Schema: production

• location, illustration, productcategory, productcosthistory, productlistpricehistory,


productmodelproductdescriptionculture, productreview, scrapreason,
productsubcategory, transactionhistory, transactionhistoryarchive,
productmodelillustration, unitmeasure, productmodel, workorderrouting,
billofmaterials, productdescription, productdocument, culture, product,
productphoto, productinventory, productproductphoto, workorder

Schema: purchasing

• purchaseorderheader, shipmethod, vendor, productvendor, purchaseorderdetail

Schema: sales

• salesorderheadersalesreason, customer, countryregioncurrency, currencyrate,


creditcard, specialoffer, specialofferproduct, currency, store, personcreditcard,
salestaxrate, salespersonquotahistory, salesreason, salesterritoryhistory,
salesterritory, salesperson, salesorderheader, salesorderdetail, shoppingcartitem

13 | P a g e
 Design queries for the following reports

14 | P a g e
Module 2:
BUSSINESS INTELLIGENCE ANALYST

How to load and clean-up data with Power BI


How to build a data model in Power BI
Creating and changing visuals
Using DAX & Power Pivot to build measures and calculations
Customizing visuals & interaction effects
Creating a comprehensive sales performance report
Saving and publishing the Power BI files
Updating and data refresh process
Use SQL with Power BI
Understanding trends & creating forecast in Power BI

15 | P a g e
How to load and clean-up data with Power BI
Understanding Power BI Desktop UI
Open Power BI Desktop application. After you exit the welcome splash screen, you will see
the blank Power BI application. Let’s understand this screen. Here is an illustration explaining
11 important features / buttons in Power BI Desktop.

1. Ribbon. Find most important and regular stuff in Home ribbon. Navigate to other
ribbons for specific functionality.
2. Get Data. Use this button to get data from almost anywhere – Excel files, websites,
databases, APIs etc.
3. View selection, by default you will be on Report view. Change to data or model
view to see behind scenes.
4. Fields Access the tables and fields (columns) of your data here. Use them in
visuals (5) or filters (7) etc.
5. Visualizations add charts, tables, maps, filters etc to the report from here.
6. Visual Fields, Format and analytics use this area to set up and customize your
visualizations (charts etc.) Note the paint-roller, use it to edit colors, fonts, settings etc.
7. Filters - set up chart, page, report level filters here. Anything you restrict will be
removed from all the linked items.
8. Canvas this is where you construct your reports.

16 | P a g e
9. Save your Power BI reports by pressing CTRL+S or clicking on this button. They
will be saved as PBIX files.
10. Publish the reports with this. You can publish them to online (either free
PowerBI.com account or paid plans) so that others can access your reports.
11. Add more pages to your report using the + button.

Load data into Power BI


o Click on Get Data button.
o Select “Excel” as source.
o Point to the downloaded sample data.
o Select “Table1” in the navigator screen
and click on “Load” button.
o Done, your data is loaded.

Data clean-up with Power Query


The blank problem

o Right click column Geography


o Select “Transform” → “Trim”
o Done, your data is fixed

17 | P a g e
How to build a data model in Power BI
Imaging a big black box with all your tables and any relationships between them along with
the measures you have defined. This black box is your data model.

Other common names for data model are Cube, tabular model or simply model.

Relationships
You can link two tables based on a column. This is called relationship.
For example,
Say have two tables – Sales & Customers.
You can link Sales table and Customers table based on Customer ID. We then say Sales &
Customer tables are related.
It means, both columns have the same meaning.
There are two kinds of relationships.
1. One to many relationships: a value in one table is linked to one or more values in
another table. Example: Customers to Sales relationship. Each customer appears once in
Customers table but can have many matching transactions in Sales table.
2. Many to many relationships: Each value in one table can be linked to one more more
values in other table and vice-a-versa. For example: People and Projects. Each person
can be part of any number of projects. Each project can have one or more persons.

18 | P a g e
Measures
Measure or calculations are what gets displayed in visuals / tables / cards.
The Count of Name, Average Salary things we used earlier are measures.
There are three kinds of measures in Power BI.
o Implicit measures: These are automatically created when you drop a filed in the “Value”
area of a chart / visual. Example: Count of Name.
o Explicit measures: These are the ones you create by using DAX language. Example:
=SUM(Table1[Salary])
o Quick measures: These are same as ‘explicit measures’ but instead of typing the DAX
formula, you use Power BI quick measure feature to make them.
You can create measures by right clicking on a table (area 4 in the Power BI Desktop UI) or
clicking on the “New Measure” button on the ribbon.

Creating and changing visuals


Adding Visuals
Working in Power BI feels like playing with your data. This is because of the drag-and-
drop nature of report building process. To add a visual,
1. Click on the type of visual you want.
2. A blank visual will be added to available empty space on your report canvas.
3. Select fields from your data and add them to relevant places.
o X-axis
o Y-axis (values)
o optionally legend

19 | P a g e
Geographic Sales Visual

Interactive nature of Power BI

Fixing "blank" team problem

o Right click column Team


o Select “Replace Values…”
o Value to Find : null
o Relplace With : Special
o Click “Close&Apply”

20 | P a g e
Saving Power BI workbooks

o File → Save

Changing Calculations for the Visuals

You can use two methods to change the calculations for the charts.

o Use default options for calculations – SUM / COUNT / AVERAGE etc.


o Write your own calculations with Power Pivot measures

To change the calculation of a chart with default options, follow below steps.

1. Select the power BI visual


2. Go to Value field.
3. Click on the little down arrow symbol.
4. Select the type of calculation you want.
5. Done.

Using DAX & Power Pivot to build measures and calculations


DAX

DAX stands for Data Analysis eXpressions. This is a language for calculating things with
Power Pivot.

DAX expressions or formulas look almost like Excel formulas.


Example DAX formula:

Total Amount =SUM(sales[Amount])

Sums up Amount column in the sales and presents it wherever you use this [Total
Amount] measure.

21 | P a g e
Adding measures with DAX

o Right click “sales”


o Seletct “New measure”

Power Pivot

Power Pivot is a calculation engine for Power BI. You can use Power Pivot to model
complex data, set up relationships between tables, calculate things to be show in value
field area of tables or visuals.
Think of Power Pivot as a calculation layer between your data and outputs. You can tell
Power Pivot how you want your calculations done thru a language called as DAX and Power
Pivot can give the answers. It is an extremely fast & scalable software.

We can use Power Pivot in either Excel or Power BI.

Customizing visuals & interaction effects


Power BI visuals are interactive. This means, if you have more than one chart on a report
page, when you click on a particular item on a chart, all other charts respond to the selection
and change.

This is quite different from normal Excel charts, but once you get used to it, you will see the
true power of Power BI visualizations.

Here is a quick demonstration of Power BI visual interactions.

22 | P a g e
Here are some of the common questions you may have about Power BI report interactions.

Are all visuals interactive?


By default, all visualizations in Power BI report are interactive. The only exception is card
visuals. They are not interactive. So if you click on them, nothing happens to other charts.
How to unselect ?

Simple, click or touch the selected item again. The interaction will be gone.

How to disable or change interactions?

Select any visual, go to Format ribbon. Now click on “Edit interactions” button. This will show
interaction buttons on top of all your visuals. Click on do not interact button (looks like no
entry sign).

You need to do this for each visual.

I want to filter instead of highlight on interaction…

You can use the “Edit interactions” button to change the style of interaction. There are three
possible interactions (as depicted to the right).

1. Filter
2. Highlight
3. No interaction

Creating a comprehensive sales performance report


Now that you have some understanding of Power BI, let’s create our first Power BI report.
The focus of this report will be,
o For a specific manager
o Show staff distribution by department
o Gender break-down
o All of their staff by salary and rating
This is a fairly simple report, but it does demonstrate the power, elegance and ease of
working with Power BI.
Here is the final output we will create.

23 | P a g e
Step by step instructions for our first Power BI report:
1. Load the employee data into Power BI.
2. Add a column chart.
o Department on axis
o Name (count of name) in values area
3. Add pie chart
o Gender as Legend
o Name (count of name) in values area
4. Add a table with name, age, rating and salary fields
5. Add a slicer with manager as field

Our report is almost ready.

Adding conditional formatting

Background colors on rating field:

Select the table. Click on down arrow symbol next to Rating and apply conditional formatting
> Background color. Set up a color scale as shown below.

24 | P a g e
Data bar for Salary:

Click on conditional formatting for salary, set up data bars as shown below.

Adding title for the report

From Home ribbon in Power BI, click on Text box and type your report title in that. Format the
text and position it on the top.

That is all, your first Power BI report is ready.

Play with slicer or charts to see powerful insights from this report.

Saving and publishing the Power BI files


To save your Power BI report, press CTRL+S. This will save a copy of your report on your
computer. Power BI files use the .PBIX as extension.

25 | P a g e
How to share / publish your reports?

You can publish your reports in various methods.

o Email or share the file: this is the simplest method. Just email or share the file with your
audience. They will need Power BI Desktop to view the reports though. Also, they will
need to access the source data sets to be able to refresh or update the reports.
o Publish to Power BI online: This is the recommended way to sharing your reports. But
you do need PowerBI account (either paid or free) to be able to publish the files to online
workspace. Once you publish your reports to the workspace, you can invite others to view
them or pin parts of it to a dashboard etc.
o Share to Mobile / Tablet via Power BI app: Once you publish the reports to Power BI
workspace, others can view the reports on web or on mobile / tablet apps by accessing
the workspace.

Updating and data refresh process


Refresh

Refresh refers to the concept of updating all the data, calculations and visuals based on
source data changes. You can manually trigger refresh by clicking on the “Refresh” button in
Power BI Desktop Home ribbon.

You can also schedule refresh for online published reports so that every day (or whatever
frequency you determine) Power BI online will refreshes your data and updates the
published reports.

Updating & Refreshing your Report

With Power BI, you create once, use forever. As your business data changes, all you need
to do is, refresh the report. This will automatically fetch any new data from your source,
update all calculations and visuals. If you publish the report again, this will replace the online
version with new one so your colleagues or clients can access updated reports easily.
What if your data format changes?

For example, if you add new columns or rename things, then you may need to rebuild some
visuals or calculations. You will notice any broken items upon refresh and you can easily fix
them.

26 | P a g e
Use SQL with Power BI

Report

27 | P a g e
Understanding trends & creating forecast in Power BI

Trend of Customers
Always make measure !
They make your reports FLEXIBLE

28 | P a g e
Forecasting Power BI

29 | P a g e
Projects
Ecommerce Sales Dashboard

30 | P a g e
Amazon Product Review Dashboard
Summary

Product Table

31 | P a g e
Customer Complaints Dashboard

Finance Report

32 | P a g e
Mall Sales Analytics

33 | P a g e

You might also like