Best Corporate Training & Consulting For Tableau, PowerBI, QlikSense, Python , Data Analytics & Data Science - ML, DL in Gurgaon

AdventureWorksDW – Python Series#1 – Computing & Plot Yearly Sales for a Territory

Prerequisites To Follow this Exercise :

  1. Microsoft SQL Server Database Express Edition & Adventure Works DataWarehouse – If you Don’t have a Microsoft SQL Server express Database and want to install it in your system and also install AdventureWorks DW , Follow –https://instrovate.com/2019/05/22/download-install-free-microsoft-sql-server-install-adventureworks-database-data-warehouse/
  2. Python Installed in your System : If you are a new user to Python and want to know how to install Python via the Anaconda Distribution , You can go through the step by step Blog i have written to install Python via Anaconda Distribution & start using Jupyter Notebook : https://instrovate.com/2019/06/09/python-anaconda-distribution-how-to-download-and-install-it-and-run-the-first-python-program/

Once you have the Microsoft SQL Server Express Edition and Python Installed in your system you are Good to Go ahead and follow the below Use Case and Example.

If you prefer watching videos rather than reading blogs , Make Sure to visit our YouTube Channel and Click on Red Button to Subscribe as we plan to post these in Video Form soon. https://www.youtube.com/c/instrovatetechnologies

Computing & Plot Yearly Sales for a Territory in AdventureWorksDW – Python.

Considerations:

  • The SQL Server 2017 is used as a back end to store AdventureWorksDW
  • We will connect Python to connect to SQL Server to fetch the required data
  • We will use pyodbc library of python to connect to SQL Server from Python
  • The territory for which we will be computing Sales is United Kingdom

The transaction of orders are stored in fact table named FactInternetSales in the Database AdventureWorksDW.

The column SalesAmount in the table FactInternetSales  has the sales amount of the order in consideration.

The dimension table DimSalesTerritory has all the details of territories from which orders are being received.

As marked above the column SalesTerritoryKey is the joining column between the fact and dim table FactInternetSales and DimSalesTerritory. The column SalesTerritoryCountry has the country name. For our problem we will take United KIngdom as the territory for yearly sales analysis.

So, our final output which will be having yearly sales of the territory named United Kingdom will be having data from two tables i.e. FactInternetSales and DimSalesTerritory.

So, the sql query to fetch the desired details are as follows :

Year  is the sql server inbuilt function to fetch the value of year from the date field. IN the query summation is performed on column SalesAmount of the table FactInternetSales . And hence, using the other columns in the group by clause which are present in select clause.

To learn how to connect python to sql server you can refer to the below Instrovate Technologies Blog:

https://instrovate.com/2019/04/12/how-to-connect-python-with-sql-server/

Below is the python code to solve the problem :

Code Key Points:

  • After the query execution is done in python initialize two python lists namely years and total_sales.
  • While iterating through each row the list years is being populated with second indexed value in the row and total_sales is being populated with fourth indexed value.
  • Please note the index count starts from zero
  • The matplotlib  library of python is being used to plot total sales against year.
  • The plot  function from matplotlib.pyplot is used to plot 2 dimensional Data. For more details below blog can be referred:

https://instrovate.com/forums/topic/plotting-actual-vs-predicted-sales/

After executing the above python program below is the outcome displayed  :

Instrovate Technologies

We at Instrovate Technologies, provide Corporate Training , Academic Training , Online Training & Consulting Services on various modules around Data Visualization , Data Analytics , Data Science , Digital Marketing & Agile Methodologies using Tools & Techniques like Tableau , Power BI , Python , Qlik Sense, Looker , R ,Machine Learning , Deep Learning & AI , Google Analytics , Adobe Analytics , Agile ,Scrum & SAFe Methodologies . Reach out to us at info@instrovate.com or Whatsapp / Call at +91 74289 52788 for any other information.

View Comments

Recent Posts

PL-100T00-A: Microsoft Power Platform App Maker

3 Days ( 24 Hrs ) Instructor-led trainingIntermediateEnglish We wil be providing you Microsoft Official Course…

3 years ago

MB-910T00: Microsoft Certified: Dynamics 365 Fundamentals (CRM)

2 DaysInstructor-led trainingBeginnerEnglish Skills gained Describe the capabilities and functionality of Dynamics 365 MarketingDescribe the…

3 years ago

PL-400T00-A: Microsoft Power Platform Developer

5 Days ( 40 Hrs ) Instructor-led trainingIntermediateEnglish We wil be providing you Microsoft Official Course…

3 years ago

PL-200T00: Microsoft Power Platform Functional Consultant

5 Days ( 40 Hrs ) Instructor-led trainingIntermediateEnglish We wil be providing you Microsoft Official…

3 years ago

PL-900T00: Microsoft Power Platform Fundamentals

2 DaysInstructor-led trainingBeginnerEnglish Prerequisites No prerequisites for this course Skills gained Describe Microsoft Power Platform…

3 years ago

Power Platform Training – 3 Days – Power Apps , Power Automate Flows , Power Virtual Agents

Course Content Day-1: Power Platform Introduction Power Platform Introduction.Components of Power PlatformConnectors, Triggers & ActionsOverview…

3 years ago