Python Code to load Local SQL Server Data to Microsoft Fabric Warehouse

Amit Chandak
4 min readJul 7, 2023

--

This is the Python code used in the Youtube video to load data from an on-Premise SQL server to Microsoft Fabric Warehouse using Python code with MFA. The Python code will use libraries Pandas, and sqlalchemy

The video

SQL- Create Table

CREATE TABLE [dbo].[customer](
[Customer_Id] [int] ,
[Age] [int] ,
[City_Id] [int] ,
[City] [varchar](50) ,
[State] [varchar](50) ,
[Name] [varchar](50)
)

Python code

import pandas as pd
import sqlalchemy as sa

#pip install --upgrade sqlalchemy==1.4.47
#Set up the connection string
server = 'localhost\\SQLEXPRESS'
database = 'sales'

# Create a connection string for SQL Server using Windows authentication
connection_string = f'mssql+pyodbc://{server}/{database}?driver=SQL+Server&Trusted_Connection=yes'

# Create an engine using the connection string
engine = sa.create_engine(connection_string)

# Run a SQL query to get a Pandas DataFrame
sql_query = "SELECT * FROM dbo.customer"
df = pd.read_sql(sql_query, engine)

# Print the DataFrame
print(df)



server = '<url>'
database = 'Warehouse2'
driver='ODBC Driver 17 for SQL Server'
user= '<email>' # use %40 inplace of @
pass1='<Password>' # use %40 inplace of @

# Create a connection string for SQL Server using Windows authentication
connection_string = f'mssql+pyodbc://{user}:{pass1}@{server}/{database}?driver={driver}&Trusted_Connection=no&Authentication=ActiveDirectoryInteractive'
#print(connection_string)
# Create an engine using the connection string
engine = sa.create_engine(connection_string, echo=True, connect_args={'autocommit': True}, fast_executemany=True)

# Run a SQL query to get a Pandas DataFrame
#sql_query = "SELECT TOP(10) * FROM [dbo].[Customer]"
df.to_sql(name='customer_py2', con=engine, if_exists='append',dtype={
'Customer_Id': sa.Integer() ,
'Age': sa.Integer(),
'City_Id': sa.Integer() ,
'City': sa.String(50),
'State': sa.String(50) ,
'Name': sa.String(50)}, index = False, chunksize=100, method='multi')

Hope you are able to use this code with the video.

More on Microsoft Fabric:

Comprehensive Free Course for Your Team to Learn Microsoft Power BI, SQL, and Data Factory

Microsoft Fabric: A Brief Introduction

How to enable Microsoft Fabric; on Power BI Service| Start the Fabric (Preview) trial

Microsoft Fabric-What it is, Why Use, Who Should, When and How to use

Microsoft Fabric: Various Components| Microsoft Fabric Create Workspace, Lakehouse, Warehouse

Microsoft Fabric: How to load data in Lakehouse using Dataflow Gen2

Microsoft Fabric: How to load data in Warehouse using Dataflow Gen2

Microsoft Fabric: How to load data in Warehouse using Data Pipeline | End-to-End Scenario

Microsoft Fabric: How to load data in Lakehouse using Spark; Python/Notebook

Microsoft Fabric: Integrated Machine Learning- k-Means clustering

Microsoft Fabric: Create a Date table in Dataflow Gen2, and use in Lakehouse and Warehouse| Time Intelligence with Power BI

Microsoft Fabric: How to load data in Lakehouse using Data Pipeline | End-to-End Scenario

Microsoft Fabric: What are the options to load local files in Lakehouse

Microsoft Fabric: Shortcut with Fabric and for Azure Data Lake | File vs Table Shortcut

Microsoft Fabric: Import Azure SQL Data to Warehouse | Multiple tables using Pipeline

Microsoft Fabric: Incremental Data Load(ETL) for Warehouse using Dataflow Gen 2, SQL Procedure, Data Pipeline

Microsoft Fabric: How about incremental without Dataflow, Only using pipeline and SQL procedure for Microsoft Fabric Warehouse

Microsoft Fabric: Load local SQL server data in Lakehouse using on-premise Gateway and Dataflow Gen2

Microsoft Fabric: Dataflow Gen2: Native warehouse Destination- Power Query Transformation — Pivot, Unpivot, Transpose Data

Microsoft Fabric: Load Azure SQL Data to Lakehouse & Warehouse simultaneously with Dataflow Gen2

Microsoft Fabric: Load Snowflake DB Data to Lakehouse & Warehouse simultaneously with Dataflow Gen2

Microsoft Fabric: How to append only Incremental data using Data Pipeline in Lakehouse

Introduction to Microsoft Fabric in Hindi | What it is, Why to Use, Who Should, When and How to use

Microsoft Fabric in Hindi Part 2: Create Workspace, Lakehouse, Warehouse

Microsoft Fabric in Hindi Part 3: How to load data in Lakehouse using Data Pipeline | End to End Scenario

Microsoft Fabric in Hindi Part 4: How to load data in Lakehouse using Dataflow Gen 2 | End to End Scenario

Microsoft Fabric in Hindi Part 5: How to load data in Warehouse using Dataflow Gen 2 | End to End Scenario

Microsoft Fabric in Hindi Part 6: How to load data in Warehouse using Data Pipeline | End to End Scenario

To take your data journey to the next level, I’ll be providing more examples in my upcoming blogs and videos.

If you want to learn Power BI from the ground up — covering everything from basic data modeling to advanced DAX and real-world analytics — be sure to explore my 20-hour Masterclass, packed with 150+ essential topics to elevate your data analytics and business intelligence skills.

Complete Power BI in one Video — 11 Hours

Click here to access all my blogs and videos in a jiffy via an exclusive glossary using Power BI. Please like, share, and comment on these blogs. I would appreciate your suggestions for improvement, challenges, and suggestions for topics so that I can explore these in more depth.

In addition, I have over 500 videos on my YouTube channel that are related to Power BI, Tableau, and Incorta. With the help of these videos, you will learn hundreds of DAX and Power Query functions, in addition to hundreds of Power BI, use cases that will assist you in becoming an expert in Power BI. Make sure you subscribe, like, and share it with your friends.

Master Power BI
Expertise in Power BI
Power BI For Tableau User
DAX for SQL Users
Learn SQL

--

--

Amit Chandak
Amit Chandak

Written by Amit Chandak

Amit has 20+ years of experience in Business Intelligence, Analytics, Data Science. He is Chief Analytics Officer at Kanerika & Power BI community Super User

Responses (1)