Power BI Advanced Series · Real-Time Streaming · by Raushan Ranjan, MCT

This comprehensive, step-by-step guide will show you how to simulate real-time stock price updates for multiple stocks (AAPL, MSFT, GOOGL), stream this data directly to Power BI, and then build an interactive dashboard that allows you to switch between stocks using a slicer or button.

✅ PROJECT GOAL:

  • Simulate price updates every second for 3 stocks (AAPL, MSFT, GOOGL).
  • Send this simulated data to a Power BI Streaming Dataset via Python.
  • Show real-time updates on a Power BI dashboard.
  • Allow interactive stock selection via a slicer or button on the dashboard.

🔷 PART 1: Create Streaming Dataset in Power BI

First, we need to set up the Power BI environment to receive the streaming data.

🔹 Step 1: Sign In to Power BI Service

🔹 Step 2: Go to Your Workspace

  • From the left sidebar, click on Workspaces.
  • Open My Workspace or create a new one if you prefer.

🔹 Step 3: Create Streaming Dataset

  • Inside your chosen workspace, click + New → Streaming dataset.
  • In the popup, choose API as the source → Click Next.
  • Define schema: Enter the field names and types exactly as follows:
    Field Name Data Type
    symbol Text
    price Number
    timestamp DateTime
  • Crucially: Turn Historic data analysis = On. This will allow Power BI to store the data, which is necessary for creating reports with slicers later.
  • Click Create.
  • After creation, Power BI will display a unique Push URL. It will look something like this:
    https://api.powerbi.com/beta/your-dataset-id/rows?key=somekey
  • Copy this entire URL. You will need it in your Python script.

🔷 PART 2: Stream Stock Prices with Python

🔹 Step 4: Setup Python Environment

  • Ensure you have Python installed on your system.
  • Open your terminal (or command prompt).
  • Install the requests library, which is needed to send HTTP requests:
    pip install requests

🔹 Step 5: Write Python Script (simulate_stocks.py)

Understand the below script

This script will simulate live stock prices and push updates every second to your Power BI dataset.

  • Create a new file named simulate_stocks.py.
  • Paste the following code into it. Remember to replace the PUSH_URL with the actual URL you copied from Power BI in Step 3!
import requests
import json
import time
import random
from datetime import datetime

# Replace with your actual Power BI Push URL (copied from the streaming dataset screen)
PUSH_URL = "https://api.powerbi.com/beta/your-dataset-id/rows?key=your-key-here"

# Initial prices for each stock (these will fluctuate)
stocks = {
    "AAPL": 190.50,
    "MSFT": 330.25,
    "GOOGL": 2800.40
}

def simulate_price(current_price):
    # Simulate a small random price movement (e.g., ±0.2%)
    change_percent = random.uniform(-0.002, 0.002)
    return round(current_price * (1 + change_percent), 2)

# Main loop to continuously send data
while True:
    data = []
    # Use UTC time for consistency across timezones
    timestamp = datetime.utcnow().isoformat() + "Z" # 'Z' indicates UTC time

    # Iterate through each stock, update its price, and prepare data point
    for symbol in stocks:
        new_price = simulate_price(stocks[symbol])
        stocks[symbol] = new_price # Update the stored price for next iteration

        data_point = {
            "symbol": symbol,
            "price": new_price,
            "timestamp": timestamp
        }
        data.append(data_point)

    try:
        # Send the data 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}")
    except Exception as e:
        print(f"Error pushing data: {e}")

    time.sleep(1)  # Wait 1 second before sending the next batch of data

🔹 Step 6: Run the Python Script

  • Open your terminal or command prompt.
  • Navigate to the directory where you saved simulate_stocks.py.
  • Run the script:
    python simulate_stocks.py
  • 📡 This will begin streaming fake prices for AAPL, MSFT, and GOOGL every second to Power BI. Keep this script running while you work on the dashboard.

🔷 PART 3: Create Dashboard with Real-time Tiles

Now, let's visualize the live stock data in a Power BI dashboard.

🔹 Step 7: Create a Dashboard

  • In Power BI Service, go to your Workspace.
  • Click + New → Dashboard.
  • Give it a name, e.g., "Live Stock Dashboard", and click Create.

🔹 Step 8: Add Real-time Tiles

  • On your newly created dashboard, click + Add tile.
  • Choose Custom Streaming Data → Click Next.
  • Select your streaming dataset (the one you created in Part 1).
  • Click Next.
  • Choose the visualization type:
    • Line Chart: Ideal for showing price changes over time.
      • Axis: timestamp
      • Value: price
      • Legend (optional, but recommended for multiple stocks): symbol
    • You can also add a Card to show the latest price, or a Gauge for a specific stock's price range.
  • Click Next → Apply.
  • You can add multiple tiles for different metrics or different stocks.

🔷 PART 4: Add Stock Selection Using Slicer (Works Only with Report Page)

⚠️ Important Note: Slicers do not work directly on real-time streaming tiles that are placed on a Power BI Dashboard. Slicers are interactive elements that require the underlying data to be stored and queryable, which is a feature of Power BI Reports built from datasets with "Historic data analysis" enabled (which we did!). Therefore, we will create a report page first and then pin its visuals to the dashboard.

🔹 Step 9: Create Report from Dataset

  • Go back to your Workspace in Power BI Service.
  • Under the "Datasets + dataflows" tab, find and click on your streaming dataset (not the dashboard).
  • Click the Create Report button (usually visible at the top or next to the dataset name). This will open a new Power BI Report editing canvas.

🔹 Step 10: Add Visuals + Slicer to the Report

  • In the Report canvas, from the "Visualizations" pane, add a Line Chart:
    • Axis: Drag timestamp to the Axis field.
    • Value: Drag price to the Values field.
    • Legend: Drag symbol to the Legend field.
  • From the "Visualizations" pane, add a Slicer:
    • Field: Drag symbol to the Field well of the slicer.
    • You can adjust its orientation (Vertical list, Horizontal, or Dropdown) in the Format pane.
  • Now, you can click on AAPL, MSFT, or GOOGL in the slicer to filter the line chart and view the price movement for that specific stock.

🔹 Step 11: Pin Visuals to Dashboard

To bring these interactive visuals to your dashboard:

  • Select the line chart visual on your report page.
  • Click the 📌 Pin icon (usually in the top-right corner of the visual).
  • In the "Pin to dashboard" dialog, choose your "Live Stock Dashboard" from the list.
  • Repeat this process for the Slicer visual as well.
  • Now, navigate back to your "Live Stock Dashboard". You will see the interactive line chart and slicer. When you use the slicer on the dashboard, it will filter the pinned chart just like it did on the report page!

🧠 Tips and Customization

Feature How to Do
Change Streaming Interval Edit time.sleep(1) in simulate_stocks.py to your preferred frequency (e.g., 0.5 for half a second, 10 for 10 seconds).
Add More Stocks Add new entries to the stocks = {} dictionary in your Python script.
Candlestick Chart For more advanced stock visualizations, use a custom visual like “Candlestick by MAQ Software” available in the Power BI Visuals marketplace (within the report page).
Set Price Alerts In Power BI Service, you can set alerts directly on dashboard tiles (e.g., a card showing the latest price). Click the three dots (...) on a tile, then Manage alerts. You can configure Power BI to send emails or trigger Power Automate flows when a price hits a certain threshold.
Deploy Python Script For continuous operation, deploy your Python script to a cloud service like Azure Functions, AWS Lambda, or a simple VM, rather than running it locally.

📌 Summary:

Congratulations! You have successfully built a real-time, interactive stock price dashboard. You now have:

  • Real-time simulated stock data streaming to Power BI.
  • Line charts and cards to visualize live stock performance.
  • A fully working interactive dashboard with a slicer to dynamically switch between stock views.

This project provides a strong foundation for building more complex real-time analytics solutions.

Quick Knowledge Check

Q1. In the real-time stock dashboard, you are using a Power BI streaming dataset. A business stakeholder asks why the line chart showing stock prices over the last hour is unavailable. What is the correct explanation?

Show Answer

A streaming dataset does not persist historical data. Data pushed to the streaming endpoint is used to update live dashboard tiles in real time but is not stored. To display a historical line chart (e.g., last hour of prices), you need either a push dataset (which stores rows) or a separate database that receives the same data feed. Streaming datasets support only live tile updates — not time-range reports.

Q2. Your Python script is pushing simulated stock price data every second. After several minutes, the dashboard tiles stop updating. Which of the following is the LEAST likely cause?

  • A) The Power BI REST API push URL expired or was regenerated
  • B) The Python script's while True loop raised an unhandled exception and stopped
  • C) The Power BI Free licence was upgraded to Pro mid-session
  • D) The API rate limit for the streaming dataset was exceeded
Show Answer

C. Upgrading a licence mid-session does not interrupt an active push stream. The other options are all valid causes: regenerating the push URL breaks the script's endpoint; an unhandled exception silently stops the loop; and exceeding API rate limits (typically ~1 million rows/hour) can throttle the push stream.

Q3. What is the role of the slicer in a streaming stock dashboard built with Power BI, and what limitation does it have compared to a standard report slicer?

Show Answer

A dashboard slicer on a streaming tile filters the displayed values, but only for that tile's live data — it cannot filter across tiles or drill back into historical data. Standard report slicers cross-filter all visuals on the page and work on the full stored dataset. Streaming dashboard tiles are independent; each shows its own live feed. Cross-tile filtering requires a push dataset combined with a report page, not a pure streaming dataset.

5 Things to Remember
  • Streaming = live tiles, no history — dashboard tiles refresh in near-real-time but no data is stored. Switch to a push dataset if historical charts are needed alongside live tiles.
  • Python push loop is simple — a while True loop with requests.post() and time.sleep() is all that is needed. Wrap in try/except so exceptions don't silently stop the stream.
  • Push URL is secret — contains the API key; treat it like a password. Regenerating it breaks any running scripts until they are updated.
  • Rate limits apply — Power BI streaming endpoints support up to ~1 million rows/hour per dataset. Pushing faster causes throttling, not errors.
  • Dashboard tiles are independent — each live tile shows its own data feed. Cross-filtering across tiles requires a push dataset backed report, not a streaming-only setup.