Complete Guide (1): Real-time Streaming Dashboards with Power BI and Python
Complete Guide: Real-time Streaming Dashboards with Power BI and Python
Have you ever wanted your Power BI dashboards to update instantly, reflecting the latest data as it happens? This comprehensive guide will walk you through the entire process of creating a real-time streaming dataset in Power BI Service and feeding it live data using a simple Python script. By the end, you'll have a fully functional, auto-updating dashboard.
✅ PROJECT GOAL:
We will stream temperature, humidity, and timestamp data from Python to Power BI in real-time, and show it on a live dashboard using streaming tiles. This is perfect for IoT monitoring, live sales tracking, or any scenario requiring immediate data visualization.
🔷 PART 1: Create a Streaming Dataset in Power BI
First, let's set up the data stream in Power BI Service.
🔹 Step 1: Go to Power BI Service
- Open your web browser and navigate to: https://app.powerbi.com
- Sign in using your Microsoft Power BI account.
🔹 Step 2: Go to the Workspace
- In the left sidebar, click on Workspaces.
- Choose any existing workspace (e.g., “My Workspace”) or create a new one using the
+ New Workspace
option.
🔹 Step 3: Create a Streaming Dataset
- Inside your chosen workspace, click
+ New
→Streaming dataset
. - In the popup that appears, select
API
as the source. - Click
Next
.
🔹 Step 4: Define Dataset Schema
This is where you define the structure of the data you'll be sending.
- Enter the field names and data types exactly as follows:
Field Name Data Type temperature Number humidity Number timestamp DateTime - Make sure to toggle Historic data analysis to On. This is crucial if you want to create dashboards or reports that can display historical data from this stream, not just the latest values.
- Click
Create
.
🔹 Step 5: Copy the Push URL
After successful creation, Power BI will display a unique Push URL.
- You’ll see a Push URL similar to this:
https://api.powerbi.com/beta/your-dataset-id/rows?key=somekey
- Copy this entire URL. We'll use it in our Python script to send data.
🔷 PART 2: Write Python Script to Send Data
Now, let's create the Python script that will generate and send data to your Power BI streaming dataset.
🔹 Step 6: Setup Python Environment
- Ensure you have Python installed on your system. You can download it from python.org.
- Open your terminal (or command prompt on Windows).
- Install the
requests
library, which we'll use to send HTTP POST requests:pip install requests
🔹 Step 7: Write Python Script
- Create a new Python file, for example,
stream_to_powerbi.py
. - Paste the following code into the file. Remember to replace
"https://api.powerbi.com/beta/your-dataset-id/rows?key=your-key-here"
with the actual Push URL you copied in Step 5!
import requests import json from datetime import datetime import time import random # Replace this with your actual Push URL from Power BI Service (Step 5) PUSH_URL = "https://api.powerbi.com/beta/your-dataset-id/rows?key=your-key-here" # Infinite loop to send data every 5 seconds while True: # Generate random temperature and humidity data temperature = round(random.uniform(20.0, 35.0), 2) humidity = random.randint(40, 90) timestamp = datetime.now().isoformat() # Get current time in ISO format # Prepare data as a list of dictionaries (JSON array) data = [{ "temperature": temperature, "humidity": humidity, "timestamp": timestamp }] # Send the POST request to Power BI response = requests.post(PUSH_URL, data=json.dumps(data), headers={"Content-Type": "application/json"}) print(f"Sent: {data} | Status: {response.status_code}") time.sleep(5) # Wait 5 seconds before sending the next data point
🔁 This script sends new random values every 5 seconds. You can adjust the time.sleep()
value to change the frequency.
🔷 PART 3: Create a Real-time Dashboard
Now that you have a data stream, let's visualize it in a Power BI dashboard.
🔹 Step 8: Create a Dashboard
- Go back to Power BI Service.
- Inside your workspace, click
+ New
→Dashboard
. - Name your dashboard (e.g., “IoT Live Monitor”) and click
Create
.
🔹 Step 9: Add Real-time Tile to Dashboard
- On your newly created dashboard, click
+ Add tile
. - Choose
Custom Streaming Data
. - Click
Next
.
🔹 Step 10: Choose Dataset and Visual
- Select the streaming dataset you just created (e.g., "IoT_Sensor_Data").
- Click
Next
. - Choose the visualization type:
- Line chart – Ideal for showing changes in temperature or humidity over time (e.g.,
timestamp
on X-axis,temperature
on Y-axis). - Card – To display the latest single value (e.g., current
humidity
). - Gauge – For real-time indicators that show a value against a range (e.g.,
temperature
within a min/max range).
- Line chart – Ideal for showing changes in temperature or humidity over time (e.g.,
- Bind fields: Drag and drop the fields from your dataset to the appropriate visual roles (Axis, Value, etc.).
- For a line chart:
- Axis:
timestamp
- Value:
temperature
- Axis:
- For a card:
- Value:
humidity
- Value:
- For a line chart:
- Click
Next
→Apply
. - You can add multiple tiles for different metrics (e.g., one line chart for temperature, one for humidity, and a card for the latest timestamp).
🔷 PART 4: Run and Watch It Live
🔹 Step 11: Start Python Script
- Open your terminal or command prompt.
- Navigate to the directory where you saved
stream_to_powerbi.py
. - Run the Python script:
python stream_to_powerbi.py
- Keep the script running.
- Now, switch back to your Power BI dashboard in the browser. You will see the values on your tiles updating in real time every 5 seconds (or whatever interval you set in your script)!
🧠 EXTRA NOTES:
🧩 How does it work?
- The streaming dataset created in Power BI Service provides a unique public HTTP endpoint (the Push URL).
- Your Python code acts as a data producer, sending JSON data to this endpoint using an HTTP POST request.
- Power BI's streaming engine immediately ingests this data and updates any dashboard tiles that are connected to this specific streaming dataset.
📦 To Modify or Expand:
- Want to stream from real sensors? Replace
random.uniform
andrandom.randint
in the Python script with actual sensor readings from IoT devices (e.g., via a serial port, MQTT, or a custom API). - Need to store + analyze historical data? By toggling "Historic data analysis" to ON (as we did in Step 4), your streaming dataset also acts as a "Push Dataset," storing data for up to 200,000 rows. You can then create full Power BI reports with filters and slicers on this historical data.
- Want advanced routing and processing? For high-volume or complex real-time data processing, integrate with Azure services like Azure Event Hubs (for ingesting massive streams) and Azure Stream Analytics (for real-time transformations and routing data to Power BI).
Conclusion
You've now successfully built a real-time streaming dashboard in Power BI, powered by Python! This capability is fundamental for modern data solutions, enabling immediate insights and responsive decision-making. Keep experimenting with different data sources and visualizations to unlock even more possibilities for your live dashboards. Happy streaming!
Comments
Post a Comment