Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Manand
Regular Visitor

Calling Azure Open AI in Power BI

I have been using ChatGPT API in Power BI as a function to get response to prompts.

I am using the below code - 

 

let
GetOpenAIResponse = (message as text) =>
let
apiKey = "YOUR_OPENAI_API_KEY",
modelId = "text-davinci-003",
openaiEndpoint = "https://api.openai.com/v1/engines/" & modelId & "/completions",
pre = "Analyze the following review : ",
postmsg = "prompt",
requestBody = "{ ""prompt"": """ & pre & message & postmsg & """, ""max_tokens"": 1024 }",
options = [
Headers = [
#"Authorization" = "Bearer " & apiKey,
#"Content-Type" = "application/json"
],
Content = Text.ToBinary(requestBody)
],
response = Web.Contents(openaiEndpoint, options),
jsonResponse = Json.Document(response),
choices = jsonResponse[choices],
text = choices{0}[text]
in
text,

ChatWithOpenAI = (message as text) =>
let
response = GetOpenAIResponse(message)
in
response
in
ChatWithOpenAI

 

and this works perfectly fine.. 

 

However, when i try to use the same code to access Azure OpenAI.. its throwing an authentication issue -

Adding that code too for reference..

 

let
GetOpenAIResponse = (message as text) =>
let
apiKey = "YOUR_OPENAI_API_KEY",

modelId = "my model id",
//modelId = "operational-excellence-model",
openaiEndpoint = "https://<deployment name>-openai-dev.openai.azure.com/openai/deployments/" & modelId & "/completions?api-version=2023-03-15-preview",
pre = "Analyze the following review : ",
postmsg = "prompt",
requestBody = "{ ""prompt"": """ & pre & message & postmsg & """, ""max_tokens"": 500}",
options = [
Headers = [
#"Authorization" = "Bearer" & apiKey,
#"Content-Type" = "application/json"

],
Content = Text.ToBinary(requestBody)
],

response = Web.Contents(openaiEndpoint, options),


jsonResponse = Json.Document(response),
choices = jsonResponse[choices],
text = choices{1}[text]
in
text,

ChatWithOpenAI = (message as text) =>
let
response = GetOpenAIResponse(message)
in
response
in
ChatWithOpenAI

 

Could soneone please help me with it?

2 REPLIES 2
Moritz975
New Member

Hi, 

another way simpler way that works well for us is a ChatGPT window via powerapps. Querying the API via Power Automate and/or Azure functions involves far fewer hurdles and the flow is very easy to integrate into PowerApps.

AI-Lens
Advocate I
Advocate I

You can take a look at the Power Bi Visual we are developing here. It essentially accopmplishes what you want. In the data source area of the visual it allows you to hook up the data sources you want OpenAI to have access to and then gives you a chatgpt like interface to talk about your data. We interact with OpenAI's API for a secure and confident visual. Let us know if you have any questions!

You can download it for free here: https://www.powerbiailens.com/

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors