# Semantic Model Operations Comprehensive guide for working with semantic models (Power BI datasets) using the Fabric CLI. ## Overview Semantic models in Fabric use TMDL (Tabular Model Definition Language) format for their definitions. This guide covers getting, updating, exporting, and managing semantic models. ## Getting Model Information ### Basic Model Info ```bash # Check if model exists fab exists "Production.Workspace/Sales.SemanticModel" # Get model properties fab get "Production.Workspace/Sales.SemanticModel" # Get model with all details (verbose) fab get "Production.Workspace/Sales.SemanticModel" -v # Get only model ID fab get "Production.Workspace/Sales.SemanticModel" -q "id" ``` ### Get Model Definition The model definition contains all TMDL parts (tables, measures, relationships, etc.): ```bash # Get full definition (all TMDL parts) fab get "Production.Workspace/Sales.SemanticModel" -q "definition" # Save definition to file fab get "Production.Workspace/Sales.SemanticModel" -q "definition" -o /tmp/model-def.json ``` ### Get Specific TMDL Parts ```bash # Get model.tmdl (main model properties) fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[?path=='model.tmdl'].payload | [0]" # Get specific table definition fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[?path=='definition/tables/Customers.tmdl'].payload | [0]" # Get all table definitions fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[?starts_with(path, 'definition/tables/')]" # Get relationships.tmdl fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[?path=='definition/relationships.tmdl'].payload | [0]" # Get functions.tmdl (DAX functions) fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[?path=='definition/functions.tmdl'].payload | [0]" # Get all definition part paths (for reference) fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[].path" ``` ## Exporting Models ### Export as PBIP (Power BI Project) PBIP format is best for local development in Power BI Desktop or Tabular Editor: ```bash # Export using the export script python3 scripts/export_semantic_model_as_pbip.py \ "Production.Workspace/Sales.SemanticModel" -o /tmp/exports ``` ### Export as TMDL The export script creates PBIP format which includes TMDL in the definition folder: ```bash python3 scripts/export_semantic_model_as_pbip.py \ "Production.Workspace/Sales.SemanticModel" -o /tmp/exports # TMDL files will be in: /tmp/exports/Sales.SemanticModel/definition/ ``` ### Export Specific Parts Only ```bash # Export just tables fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[?starts_with(path, 'definition/tables/')]" -o /tmp/tables.json # Export just measures (within tables) fab get "Production.Workspace/Sales.SemanticModel" -q "definition.parts[?contains(path, '/tables/')]" | grep -A 20 "measure" ``` ## Listing Model Contents ```bash # List all items in model (if OneLake enabled) fab ls "Production.Workspace/Sales.SemanticModel" # Query model structure via API fab api workspaces -q "value[?displayName=='Production'].id | [0]" | xargs -I {} \ fab api "workspaces/{}/items" -q "value[?type=='SemanticModel']" ``` ## Updating Model Definitions **CRITICAL**: When updating semantic models, you must: 1. Get the full definition 2. Modify the specific parts you want to change 3. Include ALL parts in the update request (modified + unmodified) 4. Never include `.platform` file 5. Test immediately ### Update Workflow ```bash # 1. Get workspace and model IDs WS_ID=$(fab get "Production.Workspace" -q "id") MODEL_ID=$(fab get "Production.Workspace/Sales.SemanticModel" -q "id") # 2. Get current definition fab get "Production.Workspace/Sales.SemanticModel" -q "definition" -o /tmp/current-def.json # 3. Modify definition (edit JSON file or use script) # ... modify /tmp/current-def.json ... # 4. Wrap definition in update request cat > /tmp/update-request.json </users" ``` Output: ```json { "value": [ { "emailAddress": "user@domain.com", "groupUserAccessRight": "Admin", "displayName": "User Name", "principalType": "User" } ] } ``` Access rights: `Admin`, `Member`, `Contributor`, `Viewer` ## Find Reports Using a Model Check report's `definition.pbir` for `byConnection.semanticmodelid`: ```bash # Get model ID fab get "ws.Workspace/Model.SemanticModel" -q "id" # Check a report's connection fab get "ws.Workspace/Report.Report" -q "definition.parts[?contains(path, 'definition.pbir')].payload | [0]" ``` Output: ```json { "datasetReference": { "byConnection": { "connectionString": "...semanticmodelid=bee906a0-255e-..." } } } ``` To find all reports using a model, check each report's definition.pbir for matching `semanticmodelid`. ## Performance Tips 1. **Cache model IDs**: Don't repeatedly query for the same ID 2. **Use JMESPath filtering**: Get only what you need 3. **Batch DAX queries**: Combine multiple queries in one request 4. **Export during off-hours**: Large model exports can be slow 5. **Use Power BI API for queries**: It's optimized for DAX execution ## Security Considerations 1. **Row-Level Security**: Check roles before exposing data 2. **Credentials in data sources**: Don't commit data source credentials 3. **Sensitive measures**: Review calculated columns/measures for sensitive logic 4. **Export restrictions**: Ensure exported models don't contain sensitive data ## Related Scripts - `scripts/create_direct_lake_model.py` - Create Direct Lake model from lakehouse table - `scripts/export_semantic_model_as_pbip.py` - Export model as PBIP - `scripts/execute_dax.py` - Execute DAX queries