Back to Blog
2 min read

Power BI Copilot: AI-Powered Business Intelligence

Power BI Copilot transforms how users interact with data through natural language. Here’s how it works.

Power BI Copilot Integration

# Power BI Copilot patterns and custom extensions

from azure.ai.openai import AzureOpenAI
import requests

class PowerBICopilotExtension:
    """Extend Power BI Copilot capabilities."""

    def __init__(self, openai_client: AzureOpenAI, pbi_client):
        self.openai = openai_client
        self.pbi = pbi_client

    async def natural_language_report(self, description: str, dataset_id: str) -> dict:
        """Generate report from natural language description."""
        # Get dataset schema
        schema = await self.pbi.get_dataset_schema(dataset_id)

        # Generate report specification
        response = await self.openai.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "system",
                "content": f"""Create a Power BI report specification.
                Dataset schema: {schema}

                Return JSON with:
                - pages: list of report pages
                - visuals: visuals on each page with type and data bindings
                - filters: report-level filters"""
            }, {
                "role": "user",
                "content": f"Create a report that: {description}"
            }],
            response_format={"type": "json_object"}
        )

        return json.loads(response.choices[0].message.content)

    async def generate_dax_measure(self, description: str, context: dict) -> str:
        """Generate DAX measure from description."""
        response = await self.openai.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "system",
                "content": f"""Generate a DAX measure.
                Tables: {context['tables']}
                Existing measures: {context['measures']}

                Return only the DAX code with proper formatting."""
            }, {
                "role": "user",
                "content": description
            }]
        )

        return response.choices[0].message.content

    async def explain_visual(self, visual_data: dict) -> str:
        """Generate narrative explanation of visual."""
        response = await self.openai.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "user",
                "content": f"""Explain this Power BI visual data in business terms:

Visual type: {visual_data['type']}
Data: {visual_data['data']}
Title: {visual_data['title']}

Provide:
1. Summary of what the visual shows
2. Key insights and trends
3. Notable outliers or anomalies
4. Business implications"""
            }]
        )

        return response.choices[0].message.content

    async def suggest_visuals(self, data_summary: dict, goal: str) -> list:
        """Suggest best visuals for the data and goal."""
        response = await self.openai.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "system",
                "content": """Recommend Power BI visuals based on the data and goal.
                Consider:
                - Data types and cardinality
                - Analysis goal
                - Best practices for visualization
                - User experience"""
            }, {
                "role": "user",
                "content": f"Data: {data_summary}\nGoal: {goal}"
            }],
            response_format={"type": "json_object"}
        )

        return json.loads(response.choices[0].message.content)["recommendations"]

    async def create_qa_model(self, dataset_id: str) -> dict:
        """Create Q&A linguistic schema."""
        schema = await self.pbi.get_dataset_schema(dataset_id)

        response = await self.openai.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "system",
                "content": """Create a Power BI Q&A linguistic schema with:
                - Synonyms for tables and columns
                - Phrasings (noun, adjective, verb)
                - Entity relationships"""
            }, {
                "role": "user",
                "content": f"Schema: {schema}"
            }],
            response_format={"type": "json_object"}
        )

        return json.loads(response.choices[0].message.content)

Power BI Copilot makes data analysis accessible to everyone through natural language.

Michael John Peña

Michael John Peña

Senior Data Engineer based in Sydney. Writing about data, cloud, and technology.