mdxpy

1.3.2 · active · verified Thu Apr 16

mdxpy is a Python library designed to simplify the programmatic creation of MDX (Multidimensional Expressions) queries for IBM Planning Analytics (TM1). It eliminates the need for manual string concatenation, reduces syntax errors, and makes MDX generation more robust and easier to refactor. The library is currently at version 1.3.2 and maintains an active release cadence with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates building a basic MDX query for a 'SalesCube' cube, retrieving all leaf members from 'Account' and 'Time' dimensions, including properties for 'Account'. It uses `TM1Service` from `TM1py` to illustrate a common integration pattern.

import os
from TM1py import TM1Service
from mdxpy import MdxBuilder, MdxHierarchySet, DimensionProperty

# TM1 connection details from environment variables for security
TM1_ADDRESS = os.environ.get('TM1_ADDRESS', 'localhost')
TM1_PORT = int(os.environ.get('TM1_PORT', 12354))
TM1_SSL = os.environ.get('TM1_SSL', 'True').lower() == 'true'
TM1_USER = os.environ.get('TM1_USER', 'admin')
TM1_PASSWORD = os.environ.get('TM1_PASSWORD', 'apple')

# This example assumes TM1py is installed and TM1 server is accessible
try:
    with TM1Service(
        address=TM1_ADDRESS,
        port=TM1_PORT,
        ssl=TM1_SSL,
        user=TM1_USER,
        password=TM1_PASSWORD
    ) as tm1:
        query = MdxBuilder.from_cube("SalesCube")
        query.add_hierarchy_set_to_row_axis(
            MdxHierarchySet.all_leaves("Account", "Account")
        )
        query.add_properties_to_row_axis(
            DimensionProperty.of("Account", "Description"),
            DimensionProperty.of("Account", "Type")
        )
        query.add_hierarchy_set_to_column_axis(
            MdxHierarchySet.all_leaves("Time", "Time")
        )

        mdx_query = query.to_mdx()
        print("Generated MDX Query:")
        print(mdx_query)

        # Execute the MDX query using TM1py (optional, for full example)
        # df = tm1.execute_mdx_dataframe(mdx_query)
        # print("\nQuery Results (first 5 rows if TM1py executed):")
        # print(df.head())

except ImportError:
    print("TM1py not installed. Install with 'pip install TM1py' to run full example.")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →