Skip to main content

PyDAX is designed to analyze DAX, it can extract comments, remove comments, and identify columns and measures referenced in DAX expressions.

Project description

PyDAX

PyDAX is a package designed to analyze DAX expressions. It can extract comments, remove comments, and identify columns and measures referenced in DAX expressions.

Installation

To install the package, use pip:

pip install PyDAXLexer

Usage

Here's how to use PyDAXLexer to analyze a DAX expression and surface helpful insights.

The example below intentionally violates a few best-practice rules (notably Unused Variables and FILTER patterns), and demonstrates how to:

  • Extract comments and remove comments
  • Extract table/column/measure references
  • Verify best practices and list violating tokens
  • Generate HTML highlighting violations
from PyDAX import DAXExpression

if __name__ == '__main__':
    # Intentionally problematic DAX (for demo purposes):
    # Two unused variables
    # FILTER on table+column inside CALCULATE
    # FILTER on table with measure predicate inside CALCULATE
    dax_expression = """
    // Demo measure with intentional violations
    VAR UnusedVar1 = 123
    VAR SalesPerCustomer = SUM(Sales[Amount]) / COUNTROWS(VALUES(Customers[CustomerID]))
    VAR UnusedVar2 = IFERROR(SUM('Sales'[DiscountAmount]), 0)
    RETURN
    CALCULATE(
        [Total Sales],
        FILTER('Sales', 'Sales'[Quantity] > 10), // column filter (violation)
        FILTER('Sales', [Total Sales] > 1000) // table + measure filter (violation)
    )
    """

    # Initialize the analyzer
    expression = DAXExpression(dax_expression)

    #Comments and comment-free expression
    print("Comments:", expression.comments)
    print("Expression without comments:", expression.dax_expression_no_comments)

    #Table/Column/Measure references
    print("Table/Artifact references:")
    for ref in expression.table_column_references:
        # DAXReference(table_name: str, artifact_name: str)
        print(f" - Table='{ref.table_name}' Artifact='{ref.artifact_name}'")

    #Best practices
    expression.print_best_practices_violations()
    print("Total best-practice violations:", expression.number_of_violations)

    # HTML highlighting (optional)
    # html_code = expression.generate_html_with_violations(name="Demo")
    # expression.save_html_with_violations_to_file("demo_violations.html", name="Demo")

Additional Features

The DAXExpression class provides several utility methods:

  • remove_comments(): Returns the expression with comments removed (accessible as dax_expression_no_comments).
  • extract_comments(): Returns a list of comment strings (accessible as comments).
  • extract_artifact_references(): Returns table/column/measure references as DAXReference objects (accessible as table_column_references).
  • generate_html(light: bool): Generates HTML output with syntax coloring.
  • generate_html_with_violations(name: str, light: bool): Generates HTML and highlights best-practice violations.
  • save_html_to_file(file_name: str): Saves the syntax-colored HTML output to a file.
  • save_html_with_violations_to_file(file_name: str): Saves the violations-highlighted HTML output to a file.

Best-practices overview

When DAXExpression is created, it initializes a set of best-practice rules and verifies them by default. You can access:

  • best_practice_rules: List of rule instances
  • number_of_violations: Total count of violations across all rules
  • print_best_practices_violations(): Print rule names and violating tokens with locations

Included rules (subject to change):

  • Use DIVIDE instead of division operator
  • Avoid IFERROR
  • Use TREATAS instead of INTERSECT
  • Filter column values with proper syntax
  • Filter measure values by columns, not tables
  • Unused variables
  • Avoid 1 - x/y syntax
  • Avoid EvaluateAndLog in production

License

This project is licensed under the MIT License.

Acknowledgments

TabularEditor

The lexer grammar used in this project is adapted from the TabularEditor GitHub repository.

ANTLR

This project uses ANTLR (ANother Tool for Language Recognition) to generate the lexer and parser for DAX expressions.

  • Project: The ANTLR Project
  • License: BSD-3-Clause License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydaxlexer-0.2.2.tar.gz (64.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydaxlexer-0.2.2-py3-none-any.whl (65.2 kB view details)

Uploaded Python 3

File details

Details for the file pydaxlexer-0.2.2.tar.gz.

File metadata

  • Download URL: pydaxlexer-0.2.2.tar.gz
  • Upload date:
  • Size: 64.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for pydaxlexer-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ba29173919cca1f35192aaa01b382e08938efb455a52886defc62f489d0d7eae
MD5 f276c839f146c174b5a072e5c1238d78
BLAKE2b-256 5adcbe4e3312a4a952f1d50778bfef05090185ac711d2b63184a7000ad475181

See more details on using hashes here.

File details

Details for the file pydaxlexer-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: pydaxlexer-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 65.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for pydaxlexer-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c23b73c78d2eee4070b0daccc02f3a7d3ac83a3907a1d439b3cb5d88ed2dabaf
MD5 1621beda21ad888f7e236a8413ff3c92
BLAKE2b-256 405b51f4c34ec65618cc7a34876114f3b3c1f55a46d03a33d4a4bc4c0cda4f08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page