Skip to content

marcelodfelman/PowerBI-BPA-Analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Š Best Practices Analyzer through TMDL - Agent

A comprehensive tool for analyzing Power BI TMDL (Tabular Model Definition Language) files against Microsoft's Analysis Services best practice rules, with optional AI-enhanced recommendations.An AI-powered tool that analyzes Power BI TMDL (Tabular Model Definition Language) files against Microsoft's Analysis Services best practice rules.

πŸš€ Quick Start - Features

Web Interface (Recommended)

  • πŸ” Comprehensive Analysis: Analyzes tables, measures, columns, and relationships in TMDL files
  • πŸ“Š Best Practices Validation: Checks against Microsoft's official Analysis Services best practice rules
  • 🚨 Detailed Reporting: Generates detailed reports with violations, severity levels, and fix suggestions
  • 🌐 Web Interface: Easy-to-use web interface for uploading and analyzing models

Then open http://localhost:5000 in your browser.

  • πŸ“„ Export Reports: Download analysis reports in Markdown format

  • ⚑ Fast Processing: Efficiently parses and analyzes large TMDL models

Command Line Interface

python run_analyzer.py "path/to/YourModel.SemanticModel"
  1. Clone or download this repository

  2. Install Python dependencies:

   pip install -r requirements.txt
   pip install -r requirements.txt

πŸ“ Project Structure

PBIP/3. Ensure you have the BPARules.json file (automatically downloaded from Microsoft's repository)

β”œβ”€β”€ run_web_interface.py # Launch web interface

β”œβ”€β”€ run_analyzer.py # Command-line tool## OpenAI Integration (Optional)

β”œβ”€β”€ requirements.txt # Python dependencies

β”œβ”€β”€ .gitignore # Git ignore rulesThe analyzer works perfectly without OpenAI, but you can enhance it with AI-powered features:

β”œβ”€β”€ config_template.py # OpenAI configuration template

β”‚### Setting Up OpenAI API Key

β”œβ”€β”€ src/ # Source code

β”‚ β”œβ”€β”€ init.pyOption 1: Environment Variable (Recommended)

β”‚ β”œβ”€β”€ tmdl_analyzer.py # Core analyzer```bash

β”‚ β”œβ”€β”€ ai_enhanced_analyzer.py # AI-enhanced analyzer# Windows PowerShell

β”‚ β”œβ”€β”€ web_interface.py # Flask web app$env:OPENAI_API_KEY = "sk-your-api-key-here"

β”‚ └── config.py # Configuration (create from template)

β”‚# Linux/Mac

β”œβ”€β”€ data/ # Data filesexport OPENAI_API_KEY="sk-your-api-key-here"

β”‚ └── BPARules.json # Microsoft best practice rules```

β”‚

β”œβ”€β”€ tests/ # Test filesOption 2: Configuration File

β”‚ β”œβ”€β”€ test_ai_analyzer.py1. Copy config_template.py to config.py

β”‚ β”œβ”€β”€ test_ai_enhancements.py2. Add your API key: OPENAI_API_KEY = "sk-your-api-key-here"

β”‚ β”œβ”€β”€ test_column_references.py

β”‚ β”œβ”€β”€ test_rules.pyGet API Key: Visit OpenAI Platform

β”‚ └── debug_column_refs.py

β”‚### AI-Enhanced Features

β”œβ”€β”€ examples/ # Example scripts- πŸ€– Smart Explanations: Context-aware violation explanations

β”‚ β”œβ”€β”€ demo.py- πŸ“Š Strategic Recommendations: High-level improvement strategies

β”‚ β”œβ”€β”€ quick_start_ai.py- ⚑ DAX Analysis: Intelligent pattern detection

β”‚ └── example_with_openai.py- πŸ’‘ Custom Fixes: Specific, actionable suggestions

β”‚

β”œβ”€β”€ docs/ # DocumentationSee OPENAI_SETUP.md for detailed instructions.

β”‚ β”œβ”€β”€ OPENAI_SETUP.md

β”‚ β”œβ”€β”€ TROUBLESHOOTING.md## Usage

β”‚ β”œβ”€β”€ AI_CONFIGURATION_FIXED.md

β”‚ β”œβ”€β”€ AI_FIXES_COMPLETE.md### Option 1: Command Line Interface

β”‚ β”œβ”€β”€ AI_PERFORMANCE_FIXES.md

β”‚ β”œβ”€β”€ AI_RULE_TYPE_OPTIMIZATION.md```bash

β”‚ β”œβ”€β”€ RULE_LEVEL_AI_ENHANCEMENT.mdpython tmdl_analyzer.py <model_path> [rules_file] [output_file]

β”‚ └── ALL_RULES_VISIBILITY.md```

β”‚

└── reports/ # Generated reportsExamples:

β”œβ”€β”€ analysis_report.md```bash

└── demo_analysis_report.md# Basic analysis
## πŸ“¦ Installation# With custom rules file and output

python tmdl_analyzer.py "Sales Dashboard.SemanticModel" BPARules.json report.md

1. **Clone or download** this repository

# Analyze different model

2. **Install dependencies:**python tmdl_analyzer.py "C:\Models\MyModel.SemanticModel" BPARules.json analysis.md**

```bash

   pip install -r requirements.txt

Option 2: Web Interface

  1. **(Optional) Configure OpenAI for AI-enhanced analysis:**1. Start the web server:
   python web_interface.py

Windows python web_interface.py

   copy config_template.py src\\config.py   ```

Linux/Mac2. Open your browser and go to: http://localhost:5000

cp config_template.py src/config.py

  1. Upload your TMDL model directory using the web interface

Edit src/config.py and add your OpenAI API key




## 🎯 Features## Best Practice Rules Checked



### Core AnalysisThe analyzer checks for violations in the following categories:

- βœ… Parses TMDL files from Power BI semantic models

- βœ… Checks 8+ best practice rules from Microsoft### Performance Rules

- βœ… Identifies violations with severity levels- ❌ Avoid floating point data types (Double)

- βœ… Provides fix suggestions where available- ⚑ Set IsAvailableInMdx to false on non-attribute columns

- βœ… Generates detailed markdown reports- πŸ”„ Avoid excessive bi-directional relationships

- βœ… Shows all rules checked (even those with 0 violations)- πŸ“Š Model should have a date table

- 🎯 Hide foreign keys

### AI-Enhanced Analysis (Optional)- πŸ“ˆ Reduce usage of calculated columns

- πŸ€– Strategic recommendations for your model

- πŸ€– Rule-level explanations (not per-violation to save costs)### DAX Expression Rules

- πŸ€– Priority ranking and effort estimates- πŸ“ Column references should be fully qualified

- πŸ€– Implementation strategies- 🎯 Measure references should be unqualified

- πŸ€– 90% cost reduction vs. individual enhancements- βž— Use DIVIDE function instead of `/` operator

- 🚫 Avoid IFERROR function

### Web Interface- πŸ” Filter expressions optimization

- 🌐 Drag & drop file upload- πŸ“‹ No duplicate measures

- 🌐 Real-time analysis

- 🌐 Interactive results viewer### Formatting Rules

- 🌐 Expandable rule checklist- 🏷️ Provide format strings for measures

- 🌐 Download reports- πŸ“Š Hide fact table columns used in measures

- 🌐 AI analyzer selector- πŸ”€ Object naming conventions

- πŸ“… Date and month column formatting

## πŸ“– Usage- πŸ”’ Numeric column summarization settings



### Method 1: Web Interface### Error Prevention Rules

- βœ… Data columns must have source columns

```bash- πŸ“œ Expression-reliant objects must have expressions

python run_web_interface.py- πŸ”— Relationship column data type consistency

```- 🚫 Avoid invalid characters in names



1. Open http://localhost:5000### Maintenance Rules

2. Choose analyzer type (Regular or AI-Enhanced)- 🧹 Remove unnecessary columns and measures

3. Upload your `.SemanticModel` folder- πŸ”— Fix referential integrity violations

4. View results and download report- πŸ“– Add descriptions to objects

- 🎭 Remove unused perspectives and roles

### Method 2: Command Line

## Output

**Basic analysis:**

```bashThe analyzer generates comprehensive reports including:

python run_analyzer.py "Sales Dashboard.SemanticModel"

```- **Summary Statistics**: Count of tables, measures, columns, relationships

- **Violation Counts**: By severity (Error, Warning, Info) and category

**AI-enhanced analysis:**- **Detailed Violations**: Specific issues found with:

```bash  - Rule name and description

python run_analyzer.py "Sales Dashboard.SemanticModel" --ai  - Affected object and location

```  - Severity level

- Fix suggestions (where available)

**Custom output:**

```bash### Sample Output

python run_analyzer.py "Sales Dashboard.SemanticModel" --output "reports/my_report.md"

TMDL Best Practices Analysis Report

Method 3: Python API

Model: Sales Dashboard.SemanticModel


import sys

sys.path.insert(0, 'src')## Summary

- Tables: 9

from tmdl_analyzer import TMDLBestPracticesAgent- Measures: 56

- Columns: 106

# Create analyzer- Relationships: 6

agent = TMDLBestPracticesAgent('data/BPARules.json')- Total Violations: 166



# Analyze model### Violations by Severity

result = agent.analyze_model('Sales Dashboard.SemanticModel')- WARNING: 90

- ERROR: 76

# Generate report

agent.generate_report(result, 'reports/output.md')### Violations by Category

```- Performance: 63

- DAX Expressions: 43

**AI-enhanced:**- Formatting: 60

```python```

from ai_enhanced_analyzer import AIEnhancedTMDLAnalyzer

## File Structure

agent = AIEnhancedTMDLAnalyzer('data/BPARules.json')

result = agent.analyze_model('Sales Dashboard.SemanticModel')```

```PBIP/

β”œβ”€β”€ tmdl_analyzer.py          # Main analyzer engine

## πŸ”§ Configurationβ”œβ”€β”€ web_interface.py          # Web interface

β”œβ”€β”€ BPARules.json            # Microsoft's best practice rules

### OpenAI API Key (for AI features)β”œβ”€β”€ requirements.txt         # Python dependencies

β”œβ”€β”€ README.md               # This file

Create `src/config.py`:└── Sales Dashboard.SemanticModel/  # Sample TMDL model

```python    β”œβ”€β”€ definition/

# OpenAI Configuration    β”‚   β”œβ”€β”€ model.tmdl

OPENAI_API_KEY = "sk-proj-your-api-key-here"    β”‚   β”œβ”€β”€ relationships.tmdl

OPENAI_MODEL = "gpt-4"  # or "gpt-3.5-turbo" for faster/cheaper    β”‚   └── tables/

MAX_TOKENS = 300    β”‚       β”œβ”€β”€ Fact_Sales.tmdl

TEMPERATURE = 0.3    β”‚       β”œβ”€β”€ Dim_Customers.tmdl

```    β”‚       └── ...

    └── ...

Or set environment variable:```

```bash

# Windows## Technical Details

set OPENAI_API_KEY=sk-proj-your-key-here

### TMDL Parser

# Linux/Mac- Parses table definitions and extracts measures, columns, properties

export OPENAI_API_KEY=sk-proj-your-key-here- Handles multi-line DAX expressions with proper escaping

```- Extracts relationship definitions and cardinalities

- Supports complex TMDL syntax and annotations

## πŸ“Š Analysis Results

### Rule Engine

### What Gets Checked- Implements Microsoft's Analysis Services best practice rules

- Supports rule expressions with pattern matching

The analyzer checks all 8 rules from Microsoft's BPARules.json:- Evaluates DAX code for common anti-patterns

- Provides contextual fix suggestions

1. **[Performance] Do not use floating point data types**

2. **[Performance] Set IsAvailableInMdx to false on non-attribute columns**### Web Interface

3. **[DAX Expressions] Column references should be fully qualified**- Flask-based web application

4. **[DAX Expressions] Measure references should be unqualified**- Drag-and-drop file upload with directory support

5. **[DAX Expressions] Use the DIVIDE function for division**- Real-time progress tracking

6. **[DAX Expressions] Avoid using the IFERROR function**- Interactive results visualization

7. **[Formatting] Provide format string for measures**- Downloadable reports

8. **[Formatting] Hide foreign keys**

## Supported TMDL Elements

### Results Include

- βœ… **Tables**: Data tables and calculated tables

- **Summary:** Object counts, violation totals by severity/category- βœ… **Measures**: DAX measures with expressions and formatting

- **Rules Checked:** All 8 rules showing passed βœ… or violations ❌- βœ… **Columns**: Data columns, calculated columns, and properties

- **Violations:** Detailed list with object info, descriptions, fix suggestions- βœ… **Relationships**: All relationship types and cardinalities

- **AI Recommendations:** (AI mode) Strategic guidance and implementation plan- βœ… **Partitions**: Data source and query information

- βœ… **Model Properties**: Model-level settings and annotations

## πŸ“š Documentation

## Best Practices Covered

- **[docs/OPENAI_SETUP.md](docs/OPENAI_SETUP.md)** - Configure AI features

- **[docs/TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md)** - Common issues and solutionsThe analyzer implements rules from Microsoft's official best practices guide:

- **[docs/RULE_LEVEL_AI_ENHANCEMENT.md](docs/RULE_LEVEL_AI_ENHANCEMENT.md)** - How AI analysis works- [Analysis Services Best Practice Rules](https://github.com/microsoft/Analysis-Services/tree/master/BestPracticeRules)

- **[docs/ALL_RULES_VISIBILITY.md](docs/ALL_RULES_VISIBILITY.md)** - Understanding the rules checklist- Performance optimization recommendations

- DAX coding standards

## πŸ§ͺ Testing- Data modeling best practices

- Security and maintenance guidelines

Run tests:

```bash## Contributing

# Navigate to tests directory

cd testsTo add new rules or improve existing ones:



# Run individual tests1. **Add rules to BPARules.json** following Microsoft's format

python test_rules.py2. **Implement rule logic** in the `BestPracticesChecker` class

python test_ai_analyzer.py3. **Add tests** for new functionality

```4. **Update documentation** with new rule descriptions



## πŸ’‘ Examples## Troubleshooting



Check the `examples/` folder for sample scripts:### Common Issues

- `demo.py` - Basic usage example

- `quick_start_ai.py` - AI-enhanced quick start**"Definition folder not found"**

- `example_with_openai.py` - Advanced AI integration- Ensure you're pointing to the `.SemanticModel` directory

- Check that the `definition` folder exists with TMDL files

## 🀝 Contributing

**"BPARules.json not found"**

1. Fork the repository- Ensure the rules file is in the same directory as the analyzer

2. Create a feature branch- Download from the Microsoft repository if missing

3. Make your changes

4. Test thoroughly**Web interface not starting**

5. Submit a pull request- Check that Flask is installed: `pip install flask`

- Ensure port 5000 is not in use

## πŸ“„ License- Try running with different port: modify `app.run(port=5001)`



This project uses Microsoft's BPARules.json for best practice rules.## License



## πŸ†˜ SupportThis tool is provided as-is for educational and analysis purposes. The best practice rules are from Microsoft's official Analysis Services repository.



- Check [docs/TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md) for common issues## Version History

- Review example scripts in `examples/`

- Check documentation in `docs/`- **v1.0.0** - Initial release with core analysis functionality

- **v1.1.0** - Added web interface and improved reporting

## 🎯 Roadmap- **v1.2.0** - Enhanced TMDL parsing and additional rule support


- [ ] Add more Microsoft best practice rules
- [ ] Support for custom rules
- [ ] Export to different formats (JSON, CSV)
- [ ] Integration with Power BI pipelines
- [ ] Enhanced AI features

---

**Made with ❀️ for the Power BI community**

About

Power BI Best Practices Analyzer through TMDL files

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages