Convert Power BI/Tabular TMDL and PBIP semantic models to JSON and generate Mermaid ERDs.
Project description
TMDL to JSON Converter
A Python3 utility to convert Tabular Model Definition Language (TMDL) files into JSON format. This tool parses the indentation-based TMDL syntax and outputs a structured JSON representation, making it easier to process or analyze Power BI/Analysis Services semantic models programmatically.
For contributor and tooling conventions, see AGENTS.md. For a detailed breakdown of supported features and extraction capabilities, see docs/TECHNICAL_SPEC.md.
Features
- TMDL Parsing: Handles root objects (
table,database,model) and nested elements like columns, partitions, measures, annotations, and relationships. - Relationship Enrichment: Derives
fromTable/fromColumnNameandtoTable/toColumnNamefromfromColumn/toColumn. - Multi-line Support: Normalizes multi-line blocks (e.g., partition
sourceM scripts, measure expressions) by stripping common indentation. - Partition Source Enrichment: Extracts
{[Schema="...",Item="..."]}references and attempts base64 decode/decompression forBinary.FromText(..., BinaryEncoding.Base64). - Batch Processing: Converts a single file or an entire directory of
.tmdlfiles. - PBIP Parsing: Parses PBIP inputs (report root folder,
.pbipfile path, or semantic model folder) and aggregates model definition files into one JSON document. - ERD Generation: Produces Mermaid ER diagrams from JSON output, with PNG export via:
- remote
mermaid.ink(default) - local Mermaid CLI (
mmdc) - Python
mermaid-clilibrary
- remote
Project Structure
.
├── AGENTS.md
├── README.md
├── config_loader.py
├── erd_generator.py
├── pbip_definition.json
├── pbip_parser.py
├── requirements.txt
├── tmdl_parser.py
├── test_erd_generator.py
├── test_pbip_parser.py
├── test_tmdl_parser.py
└── docs/
└── TECHNICAL_SPEC.md
Usage
Run commands from the code directory using the in-repo virtual environment Python (../env/Scripts/python on Windows, or ../env/python if you use a shim).
Install (PyPI)
Install from PyPI:
python -m pip install powerbi-tmdl-tools
Optional (enable --png-mode python for ERD PNG rendering):
python -m pip install "powerbi-tmdl-tools[png-python]"
After installation, the following console commands are available:
tmdl-to-jsonpbip-to-jsontmdl-erd
1. Convert a single TMDL file
Print to console:
../env/Scripts/python tmdl_parser.py tmdl/DimCountry.tmdl
Save to a specific JSON file:
../env/Scripts/python tmdl_parser.py tmdl/DimCountry.tmdl -o output.json
2. Convert a directory of .tmdl files
Convert all .tmdl files in a directory and save them to an output folder:
../env/Scripts/python tmdl_parser.py tmdl -o json_output
Note: If json_output does not exist, it will be created.
3. Parse a PBIP folder
Parse a .pbip folder and write the aggregated model JSON:
../env/Scripts/python pbip_parser.py path\to\Report.pbip --output model.json
tmdl_parser.py can also accept:
- a PBIP report root folder containing a single
.pbipfile (it resolves.pbip -> .Report/definition.pbir -> .SemanticModel/definition) - a
.pbipfile path directly
4. Help
View all available options:
../env/Scripts/python tmdl_parser.py --help
Testing
Unit tests are provided to verify parser functionality. Run them from the code directory:
../env/Scripts/python -m unittest
ERD Generation
The erd_generator.py utility generates Entity Relationship Diagrams (ERD) from the JSON output produced by tmdl_parser.py or pbip_parser.py.
Features
- Mermaid Syntax: Generates standard Mermaid ERD diagrams compatible with GitHub, Notion, and other tools.
- Smart Filtering: Automatically excludes system tables (
DateTableTemplate,LocalDateTable) to focus on your business logic. - Clean Output: Trims DAX formulas from column names for better readability.
- PNG Export: Can export diagrams directly to PNG images using the mermaid.ink API (requires internet access).
- Default: remote
mermaid.ink - Offline: local
mmdc(Mermaid CLI) or Pythonmermaid-cli(see options below)
- Default: remote
Usage
1. Generate Mermaid Markdown:
../env/Scripts/python erd_generator.py input.json --output diagram.md
2. Generate PNG Image:
../env/Scripts/python erd_generator.py input.json --png-output diagram.png
3. Generate both Markdown and PNG:
../env/Scripts/python erd_generator.py input.json --output diagram.md --png-output diagram.png
Options
input_file: Path to the JSON input file (output fromtmdl_parser.py).--output,-o: Path to output Mermaid file (e.g.output.md). If ending in.md, wraps content in a mermaid code block.--png-output: Path to output PNG file.--png-mode: PNG rendering mode:remote(default): usesmermaid.ink(requires internet access)local: uses Mermaid CLI (mmdc) (offline; requiresmmdcinstalled)python: uses Pythonmermaid-cli(offline; install viarequirements.txt)
--mmdc-path: Path tommdcexecutable (for--png-mode local)MMDC_PATH: Environment variable alternative to--mmdc-path(for--png-mode local)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file powerbi_tmdl_tools-1.0.5.tar.gz.
File metadata
- Download URL: powerbi_tmdl_tools-1.0.5.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e27791c04c03b72d43b8bbc8904692f7458a0ef46f285f63026341fe40bce94c
|
|
| MD5 |
7332e9effdee238127e60bb45b385ad0
|
|
| BLAKE2b-256 |
8bc9c50d2914a21472fa3a3c2d3092bb252ff4956f7b33ed905b43a6850e3714
|
File details
Details for the file powerbi_tmdl_tools-1.0.5-py3-none-any.whl.
File metadata
- Download URL: powerbi_tmdl_tools-1.0.5-py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
519355f715af1e60c5a58333ce1ad5feeaae017ce3bb02daf2bcf0d8e0320eff
|
|
| MD5 |
2b2ab491dec386c45ca4f004ef020aaf
|
|
| BLAKE2b-256 |
438c245df3bbeb118dd20661ad89db1e1ea87fcc84cefb0e30e669b8723883d2
|