Bring ChatGPT into Power BI: Step-by-Step Integration via Power Query - Why This Matters

You’ve probably used ChatGPT to write DAX, clean M code, or brainstorm visuals - but did you know you can embed ChatGPT directly into Power BI through Power Query? This guide walks you through exactly how to connect Power BI to OpenAI’s API, send prompts, and dynamically get responses from ChatGPT - enabling real AI-powered transformations.
Jan 22 / datatraining
Step 1: Get Your OpenAI API Key

1. Visit platform.openai.com.
2. Navigate to Settings → API Keys.
3. Click Create new secret key, name it (e.g., "Power BI demo") and save it securely.
Note: The key is shown only once. Don’t hardcode it in production - use parameters or a more secure method later.

Step 2: Insert Code into Power Query


In Power BI:
1. Open Transform Data → Advanced Editor
2. Paste the M code from ChatGPT.
3. Replace the placeholder with your actual API key.
If you get a Bad Request error, ask ChatGPT to fix it. The latest endpoint should include /chat/ in the URL.

Step 3: Fix API Version and Prompt Format

Update the model to "gpt-4" or any available newer model. Also, structure your API prompt using system and user roles:

"messages": {
  [ "role": "system", "content": "You are a helpful assistant" ],
  [ "role": "user", "content": "What is the capital of France?" ]
}

Click Done and test the query. You should see the AI response returned!

Step 4: Turn the Script into a Custom Function


Refactor the query into a function with parameters:

Step 5: Apply Function to a Dataset


Let’s use a real-world dataset for testing. Example:
• Dataset: One Direction song lyrics (CSV)
• Goal: Use ChatGPT to categorize the emotion of each song
1. Load the lyrics dataset into Power BI.
2. You can clean the text of a certain column (depends on the dataset): Transform → Format → Clean.
3. Go to Add Column → Invoke Custom Function.
4. Choose your ChatGPT function.

  • Question: Assign a one-word category to this song
  • Input: Lyrics column
The function will call ChatGPT row-by-row and return AI-generated insights (e.g., "Romance", "Freedom"). You might need to adjust the prompt depends on what is your expected output.

Step 6: Visualize the AI Output

Now that you have AI-generated categories in a column, visualize them in a report:
1. Go to Report View
2. Add a bar chart with Song Category as axis
You’ll instantly see what your songs are about - romance, heartbreak, love, etc.

Business Use Cases

This integration has many practical uses:
• Sentiment analysis on survey responses
• Categorizing invoice descriptions
• Generating tags or summaries from text fields
• Translating text, enriching data

Final Thoughts

Using ChatGPT from inside Power BI opens the door to AI-enhanced data modeling, cleansing, and classification without ever leaving your data environment.

It’s flexible, and incredibly powerful for any Power BI developer ready to level up their workflows.

Hope you like it!

Give it a try and see how it works for you! I’d love to hear what you think or see how you use this trick in your own reports.

How to Power BI

Watch it here

Launch Power BI Reports
that bring your organization
to a fully 
data-driven world.

Power BI Launch

After years of consulting we have developed a holistic solution for launching Power BI Reports in 3 months. From key metrics discovery, to report design, implementation and stakeholders' training. We know precisely how to launch Power BI reports that drive organizations' growth.

Power BI Trainings

Our technical trainings for report developers instantly upskill your teams. Alongside our unique business user trainings the improvement in overall organizational data literacy becomes immediately actionable.

Take your Skills to the Next Level

Power BI Trainings

__________