mdx

A simple package for generating mdx scripts


Keywords
MDX, builder, bi, business, intelligence
License
MIT
Install
pip install mdx==0.1

Documentation

MDX

This is a simple library to help building .mdx scripts to use with microsoft OLAP servers. The aim is to help with building scripts that use large sets and tuples and thus become hard to manage and debug.

The system is composed by:

  • A simple syntax module to generate mdx
  • Functions to generate mdx sets from lists, .json arrays, or prefined .mdx sets
  • A writer class and a writer context manager

The code is pre-alpha. Contributions are much welcome.

Installation

pip install mdx

Usage

from mdx import syntax as s
from mdx import MDXSetConstructor, writerContext
from mdx import loadJson, loadMDX

toyCodeBuilder = MDXSetConstructor(memberPath="[Products].[ProductName]", comment="Product Codes")
colorCodeBuilder = MDXSetConstructor(memberPath="[Colors]", comment="Color Codes")

toys = {
  'dolls' : toyCodeBuilder(codeList=[1,2,3], listName="toy dolls")
}
# This creates the mdx set
# {
#   // Product Codes toy dolls
#   [Products].[ProductName].&[1],
#   [Products].[ProductName].&[2],
#   [Products].[ProductName].&[3]
# }

colors = {
  'blue' : colorCodeBuilder(codeList=['blue', 'cyan'], listName="blues")
}
# This creates the mdx set
# {
#   // Color Codes blues
#   [Colors].&[blue],
#   [Colors].&[cyan]
# }


# Loads .mdx files in path
# Each file becomes available as a tuple member with the file name as key
toys = loadMDX(
  "path/to/toys",
  extraMembers = toys, # Add extra members defined by hand
  toyCodeBuilder
)

# Loads .json files in path
# Each file becomes available as a member of a named tuple with the file name as field
colors = loadJSON(
  "path/to/colors",
  extraMembers = colors,  # Add extra members defined by hand
  colorCodeBuilder
)

# The first two arguments are passed automatically by the context manager
@measure
def volume(writer, BI, name, *expressions, **kwargs):

  (toys, colors) = BI
  # Perform some common .mdx operations here...
  writer.write(
    s.createMeasure(s.measureMember(name),
      s.sumExisting('[Measures].[PurchaseCount]', s.intersect(*expressions))
    )
  )

writer = MDXWriter(name='writer', path='./interestingToys') # by default the path is set on config["OUTPUT_FOLDER"]

# Putting it all together
with writerContext(BI=(toys, colors), writer=writer) as (measures, config):

  measures(volume, someOtherMeasure, etc)(
    # the writer and set variables are passed to the measure after the arguments
    'volume of interesting toy purchases',
    s.tuple(toys.dolls, colors.blue),
    s.exclude(
      s.tuple(toys.cars, colors.any),
      s.tuple(toys.cars, colors.red),
    )
  )

# The output will be available at ./interestingToys.mdx

Configuration options

from mdx import config
config["OUTPUT_FOLDER"] = "./path/to/folder"
config["LIST_BASE_PATH"] = "./path/to/folder"
config["TAB_SIZE"] = 2

Please check mdx/syntax for the available mdx methods and their implementation

Available MDX functions and operators

  • set
  • tuple
  • intersect
  • union
  • filter
  • subtract
  • exclude (alias for except)

Short hand methods

  • sumExisting
  • rate
  • measureMember
  • createMeasure
  • title