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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request 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
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors