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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
rafaelbruno
New Member

how to do DAX queries in python?

Pessoal, estou tentando criar um comando em Python para executar uma consulta DAX em um conjunto de dados. Já consigo visualizar o relatório usando o bloco de anotações Jupyter no vscode, mas não consigo criar um comando para executar uma consulta DAX.

1 ACCEPTED SOLUTION
xifeng_L
Super User
Super User

Hi @rafaelbruno ,

 

You can try the following python script.

 

from sys import path
path.append(r'C:\Program Files\Microsoft.NET\ADOMD.NET\160')    # Modify to the corresponding path on your computer
from pyadomd import Pyadomd
import pandas as pd

server = "powerbi://api.powerbi.com/v1.0/myorg/WorkspaceName"
username = 'xxxx@xxxx.com'
password = 'xxxxxxxx'
dataset = 'Dataset Name'

conn_str = f'Provider=MSOLAP;Data Source={server};Initial Catalog={dataset};User ID={username};Password={password};'

query = """
EVALUATE 
TableName 
"""

with Pyadomd(conn_str) as conn:
    with conn.cursor().execute(query) as cur:
        df = pd.DataFrame(cur.fetchall(),columns=[i.name.split('[')[-1].split(']')[0] for i in cur.description])

print(df.head())

 

Or use the PowerBI REST API.

https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/execute-queries-in-group

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

View solution in original post

1 REPLY 1
xifeng_L
Super User
Super User

Hi @rafaelbruno ,

 

You can try the following python script.

 

from sys import path
path.append(r'C:\Program Files\Microsoft.NET\ADOMD.NET\160')    # Modify to the corresponding path on your computer
from pyadomd import Pyadomd
import pandas as pd

server = "powerbi://api.powerbi.com/v1.0/myorg/WorkspaceName"
username = 'xxxx@xxxx.com'
password = 'xxxxxxxx'
dataset = 'Dataset Name'

conn_str = f'Provider=MSOLAP;Data Source={server};Initial Catalog={dataset};User ID={username};Password={password};'

query = """
EVALUATE 
TableName 
"""

with Pyadomd(conn_str) as conn:
    with conn.cursor().execute(query) as cur:
        df = pd.DataFrame(cur.fetchall(),columns=[i.name.split('[')[-1].split(']')[0] for i in cur.description])

print(df.head())

 

Or use the PowerBI REST API.

https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/execute-queries-in-group

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.