Integrating Power BI with OpenAI: A Comprehensive Guide

Introduction:

As the need for data-driven decision-making grows, integrating artificial intelligence (AI) with business intelligence (BI) tools has become an invaluable asset for businesses. Two popular tools in these fields are Power BI by Microsoft and OpenAI’s GPT-4. In this blog post, we’ll walk you through a detailed process to integrate Power BI with OpenAI, unlocking powerful analytics and AI capabilities for your organization.

Power BI is a suite of business analytics tools that helps you visualize and share insights from your data. OpenAI is a cutting-edge AI research lab that has developed the GPT-4, a large language model capable of understanding and generating human-like text. By integrating Power BI with OpenAI, you can enhance your data analysis and generate insights using AI techniques.

Note – Please be aware that this solution involves interacting with OpenAI’s API. I encourage users to familiarize themselves with OpenAI’s data usage policy (https://platform.openai.com/docs/data-usage-policy) and take necessary precautions to ensure the privacy and security of their data.

Prerequisites:

  1. A Power BI Pro or Premium account
  2. OpenAI API Key (Sign up for OpenAI’s API service at https://beta.openai.com/signup/)

Step 1: Install Python and Required Libraries

To install Python on your computer, follow these steps:

  1. Visit the official Python website: https://www.python.org/downloads/
  2. You will see the download buttons for the latest Python version available for your operating system (Windows, macOS, or Linux). Click on the appropriate button to download the installer. If you need a different version or want to explore other installation options, click on the “View the full list of downloads” link below the buttons.
  3. Once the installer is downloaded, locate the file in your downloads folder or wherever your browser saves downloaded files.
  4. Run the installer by double-clicking on the file.

For Windows:

  • In the installer, check the box that says “Add Python to PATH” at the bottom. This will allow you to run Python from the command prompt easily.
  • Select the “Customize installation” option if you want to change the default installation settings, or just click on “Install Now” to proceed with the default settings.
  • The installer will install Python and set up the necessary file associations.

For macOS:

  • Follow the installation prompts in the installer.
  • Depending on your macOS version, Python may already be pre-installed. However, it’s usually an older version, so it’s still recommended to install the latest version from the official website.

For Linux:

  • Most Linux distributions come with Python pre-installed. You can check the installed version by opening a terminal and typing python --version or python3 --version. If you need to install or update Python, use your distribution’s package manager (such as apt, yum, or pacman) to install the latest version.

After installation, open a command prompt or terminal and type python --version or python3 --version to ensure that Python has been installed correctly. You should see the version number of the installed Python interpreter.

Now you have Python installed on your computer and are ready to proceed with installing the required libraries and running Python scripts.

2. Install the following Python libraries using pip:

pip install openai pandas powerbiclient
pip install msal

Step 2: Create a Power BI Dataset and Table

  1. Sign in to Power BI service at https://app.powerbi.com/
  2. Navigate to your workspace and click on ‘Datasets + dataflows’ in the left pane.
  3. Click on the ‘+ Create’ button and select ‘Streaming Dataset.’
  4. Choose ‘API’ as the connection type and click ‘Next.’
  5. Provide a name for your dataset, such as ‘OpenAI_Insights,’ and click ‘Create.’
  6. You will receive an API URL and an authentication token. Save these for later use.

Step 3: Create a Python Script to Fetch Data and Push to Power BI

  1. Create a new Python script (e.g., ‘openai_powerbi_integration.py’) and import the required libraries:
import openai
import pandas as pd
from powerbiclient import Report, models
import requests
from msal import PublicClientApplication

2. Set up OpenAI API and Power BI authentication:

# Set up OpenAI API
openai.api_key = "your_openai_api_key"

# Set up Power BI authentication
POWER_BI_CLIENT_ID = "your_power_bi_client_id"
AUTHORITY = "https://login.microsoftonline.com/common"
SCOPE = ["https://analysis.windows.net/powerbi/api/.default"]
app = PublicClientApplication(POWER_BI_CLIENT_ID, authority=AUTHORITY)

result = None
accounts = app.get_accounts()
if accounts:
    result = app.acquire_token_silent(SCOPE, account=accounts[0])

if not result:
    flow = app.initiate_device_flow(scopes=SCOPE)
    print(flow["message"])
    result = app.acquire_token_by_device_flow(flow)

powerbi_auth_token = result["access_token"]

Replace your_openai_api_key with your OpenAI API key and your_power_bi_client_id with your Power BI client ID. The script will prompt you to authenticate with Power BI by providing a URL and a device code.

To obtain your Power BI client ID, you need to register an application in the Azure Active Directory (Azure AD) associated with your Power BI account. Here’s a step-by-step guide to help you get your Power BI client ID:

  1. Sign in to the Azure portal: Go to https://portal.azure.com/ and sign in with the account you use for Power BI.
  2. Navigate to Azure Active Directory: Once you’re logged in, click on “Azure Active Directory” from the left-hand menu or find it using the search bar at the top.
  3. Register a new application: In the Azure Active Directory overview page, click on “App registrations” in the left-hand menu, and then click on the “+ New registration” button at the top.
  4. Configure your application:
    • Provide a name for your application (e.g., “PowerBI_OpenAI_Integration”).
    • Choose the supported account types (e.g., “Accounts in this organizational directory only” if you want to restrict access to your organization).
    • In the “Redirect URI” section, choose “Web” and provide a URL (e.g., “https://localhost“). This is just a placeholder and won’t be used for our Python script.
  5. Click on the “Register” button at the bottom to create the application.
  6. Obtain your client ID: After registering your application, you’ll be redirected to the application’s overview page. Here, you’ll find the “Application (client) ID.” This is the Power BI client ID you need for your Python script. Make sure to copy and save it securely.
  7. Grant API permissions:
    • In the application’s main menu, click on “API permissions.”
    • Click on the “+ Add a permission” button and select “Power BI Service.”
    • Choose “Delegated permissions” and check the “Dataset.ReadWrite.All” permission.
    • Click on the “Add permissions” button to save your changes.
  8. Don’t forget to update the Power BI client ID in your Python script!

3. Create a function to fetch data from OpenAI and process it:

def fetch_openai_data(prompt):
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=100,
n=1,
stop=None,
temperature=0.7,
)
generated_text = response.choices[0].text.strip()
return generated_text

4. Create a function to push data to Power BI:

def push_data_to_powerbi(api_url, auth_token, dataframe):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {auth_token}",
}
data_json = dataframe.to_json(orient="records")
response = requests.post(api_url, headers=headers, data=data_json)
return response.status_code

5. Use the functions to fetch data from OpenAI and push it to Power BI:

# Define your OpenAI prompt
prompt = "Summarize the key factors affecting the global economy in 2023."

# Fetch data from OpenAI
openai_data = fetch_openai_data(prompt)

# Create a DataFrame with the data
data = {"OpenAI_Insight": [openai_data]}
dataframe = pd.DataFrame(data)

# Push data to Power BI
api_url = "your_power_bi_api_url"
auth_token = powerbi_auth_token
status_code = push_data_to_powerbi(api_url, auth_token, dataframe)

# Print status code for confirmation
print(f"Data push status code: {status_code}")

6. Save and run the Python script:

python openai_powerbi_integration.py

If successful, you should see the status code ‘200’ printed, indicating that the data push was successful.

Step 4: Create a Power BI Report and Visualize Data

  1. Go back to Power BI service and navigate to the ‘OpenAI_Insights’ dataset.
  2. Click on the dataset to create a new report.
  3. In the report editor, create a table or any other visualization type to display the ‘OpenAI_Insight’ data.
  4. Save and publish the report to share insights with your team.

Conclusion:

In this blog post, we walked you through the process of integrating Power BI with OpenAI. By following the steps, you can use Power BI to visualize and share insights generated by OpenAI’s GPT-4, enhancing your data analysis capabilities. This integration opens up new possibilities for advanced data-driven decision-making, enabling your organization to stay ahead of the competition.

Remember that this is just a starting point. You can further customize the Python script to fetch and process more complex data from OpenAI, and even create dynamic, real-time dashboards in Power BI to keep your team updated with the latest AI-generated insights.

Learn More

If you’re interested in learning more, here are three example follow-up questions you could ask ChatGPT about the blog post:

  1. How can I customize the OpenAI prompt to generate more specific insights or analyses for my Power BI dashboard?
  2. What are some best practices for visualizing the AI-generated insights in Power BI to create effective and easy-to-understand reports?
  3. Can you provide examples of other use cases where the integration of Power BI and OpenAI can be beneficial for businesses or organizations?

Make sure you provide the context of the blogpost when asking your follow-up questions. Here is an example of how you could ask it:

In the blog post about integrating Power BI with OpenAI, you mentioned creating a Python script to fetch data and push it to Power BI. How can I customize the OpenAI prompt within the script to generate more specific insights or analyses for my Power BI dashboard?

Thanks for reading!

This blogpost was created with help from ChatGPT Pro.

6 thoughts on “Integrating Power BI with OpenAI: A Comprehensive Guide

  1. Chris: the step #2 above seems to not be working exactly. You need to supply the values from the stream prior to creating the service endpoint. Step 2-6 doesn’t return a token as well. Can you please clarify. thank you.

    Like

  2. Great work! Do you have an example of what this enables? I can’t quite understand what this actually helps achieve.

    Like

    1. Hahahahaha. This comment made my night – you’re right, it’s a pretty basic scenario that’s really just visualizing data from the API. Ultimately the idea of posting these is just to get people thinking and maybe spark new ideas about using these together.

      Like

Comments are closed.