Skip to main content

About

I wanted to consume some info from Azure Analysis Services from python and didn't see a convenient way to to do, so I wrote this. It should also work just fine with XMLA endpoints on Power BI Premium.

pymsasdax is a small Python Module for running DAX queries against Microsoft Analysis Services, using COM Interop. It does some basic typesniffing and returns a best guess Pandas Dataframe.

This does assume that the MSOLAP client is installed - you can get it from here

I've done very little testing, so consider this alpha code. If you run into timeouts, make sure you're setting the timeout to an appropriate duration when creating the Connection.

tidy_column_names will remove brackets and replace spaces with underscores in the returned dataframe's columns. Set it to False in the Connection init if you don't want this behavior.

Also, this is my first module up on pypi and I'm not exactly an expert on python, so feel free to submit an issue or a pull request. If I ended up reinventing the wheel here (ha!) and there was an easier way to do this, also please let me know.

I hope you find this useful!

Python before 3.9

This should actually work fine with for python 3 under 3.9. I've used this code for a couple of years now without incident -- I was just lazy when building this package. I think you'd need backports to support dateparser. Feel free to path and submit a PR if you like.

Usage examples

Have an interactive prompt for Login to the resource

from pymsasdax import dax

with dax.Connection(
        data_source='asazure://<region name>.asazure.windows.net/<instance here>,
        initial_catalog='<my tabular database>'
    ) as conn:
    df = conn.query('EVALUATE ROW("a", 1)')
    print(df)

Query a Power BI Premium Workspace XMLA endpoint

You can also find the endpoint in your workspace settings, as shown below. You'll use the dataset name as the initial_catalog.

Screen capture of powerbi workspace settings
from pymsasdax import dax

with dax.Connection(
        data_source='powerbi://api.powerbi.com/v1.0/myorg/<workspace name, spaces are fine>',
        initial_catalog='<dataset name - spaces are fine>'
    ) as conn:
    df = conn.query('EVALUATE ROW("a", 1)')
    print(df)

Use an app id

from pymsasdax import dax

with dax.Connection(
        data_source='asazure://<region name>.asazure.windows.net/<instance here>',
        initial_catalog='<my tabular database>'
        uid='app:<client id>@<tenant id>',
        password='<client secret>'
    ) as conn:
    df = conn.query('EVALUATE ROW("a", 1)')
    df.to_csv("raw_data.csv", index=False)        

Rename columns your way

from pymsasdax import dax

def my_column_renamer(colname):
    return colname.lower()

with dax.Connection(
        data_source='asazure://<region name>.asazure.windows.net/<instance here>',
        initial_catalog='<my tabular database>',
        tidy_map_function = my_column_renamer
    ) as conn:
    df = conn.query('EVALUATE SUMMARIZECOLUMNS (etc....etc...etc...)')
    print(df)

Dev Notes

Version History

  • 2023.1020
    • fix timeout not being honored as CommandTimeout
  • 2023.1018
    • fix bug using effective_user_name or kwargs
  • 2023.1017
    • fix bug using effective_user_name
  • 2023.1016
    • Add at least some docstrings
    • Add Premium XMLA endpoint example
    • Add effective_user_name parameter to connection
    • Pass **kwargs as additional connection string key value pairs
  • 2023.1013
    • Fix issue with column names populating from when i made initial package version
    • Allow specificiation of column name cleanup function
  • 2023.1001 - Initial

Tests

Yes. There aren't any. Feel free to submit a PR.

Building

This might not be right but if you ever go to update pypi -

bumpver update --dry --set-version="2023.1020"
pip-compile pyproject.toml
python -m pip install -e . 
#test
python -m build
twine check dist/*
twine upload dist/*

Release files for pymsasdax 2023.1020

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymsasdax 2023.1020
File Size Uploaded
pymsasdax-2023.1020.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymsasdax 2023.1020
File Interpreter ABI Platform
pymsasdax-2023.1020-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / pymsasdax-2023.1020.tar.gz

Download URL pymsasdax-2023.1020.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7adafd5935e1b8c3e7cc836ccc2f43991c1050b888cdae98dfd39762b8bcbc7e
BLAKE2b-256 checksum
How to use checksums
039fc1cd5bacec0cb7bfcc7543344006a068aa944233d71546988a40d535c829
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / pymsasdax-2023.1020-py3-none-any.whl

Download URL pymsasdax-2023.1020-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f87229642392c0d0de015bf2c340f3c71ed783f8bb705d2588b5494fcca12b97
BLAKE2b-256 checksum
How to use checksums
0870085afe6702a8f0820cd7e74d7effd37d93a18839e6e9f107b24bbeea0a2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

2023.1020 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page