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 asdax_expression_no_comments).extract_comments(): Returns a list of comment strings (accessible ascomments).extract_artifact_references(): Returns table/column/measure references asDAXReferenceobjects (accessible astable_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 instancesnumber_of_violations: Total count of violations across all rulesprint_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.
- Source: TabularEditor GitHub Repository
- License: MIT License
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
Metadata
Release files for PyDAXLexer 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydaxlexer-0.3.1.tar.gz | 71.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydaxlexer-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 142.7 kB
Release files / pydaxlexer-0.3.1.tar.gz
| Download URL | pydaxlexer-0.3.1.tar.gz |
|---|---|
| Size | 71.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eac69cdeb33eb62f5ee50e2317abadefd45ea083eede91894763e8a4170c7f88
|
|
BLAKE2b-256 checksum How to use checksums |
b00a632b41a8e74927a78950499182a1e3c4bfd3f0d625c0604d4e170b729fca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.8
|
Release files / pydaxlexer-0.3.1-py3-none-any.whl
| Download URL | pydaxlexer-0.3.1-py3-none-any.whl |
|---|---|
| Size | 70.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e265a331fffcb08d4d4528e6562c20db8fb183723aad52b273682f526dcf8ae6
|
|
BLAKE2b-256 checksum How to use checksums |
7f94e275eb7284c5ecc77f2fa689ce861dd24abe67af98d8159392eea63561c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.8
|