Power BI 语义模型生成
fabric-semantic-model
从 Microsoft Fabric 湖仓表推断并生成 Power BI 语义模型(TMDL 格式)。涵盖星型架构检测、表分类(事实表/维度表)、关系推断、DAX 度量值生成、数据类型映射以及通过 REST API 部署。适用场景:创建语义模型、构建数据模型、星型架构、Power BI 模型、TMDL、事实表、维度表。
- License 许可
- MIT
- 最近更新
- 2026-06-04
- Stars
- ★ 3
- Forks
- 1
01 前言 Front Matter
- name
- fabric-semantic-model
- description
- Infer and generate Power BI semantic models (TMDL format) from Microsoft Fabric lakehouse tables. Covers star-schema detection, table classification (fact/dimension), relationship inference, DAX measure generation, data type mapping, and deployment via REST API. Use when: create semantic model, build data model, star schema, Power BI model, TMDL, fact table, dimension table.
- compatibility
- Requires the Fabric MCP Server VS Code extension for table discovery. Generates TMDL files deployable to Microsoft Fabric via REST API.
02 资源文件 1
- 脚本scripts/fabric_semantic_model.py11.9 KB
03 SKILL.md 正文
Fabric Semantic Model Creator — Reference
Generates Power BI semantic models in TMDL format by analyzing lakehouse table schemas and sample data. The agent reasons about table names, column names, data types, cardinality, and sample values to infer the best star-schema model.
Table Classification Rules
Classify each lakehouse table as fact or dimension based on these signals:
| Signal | Fact Table | Dimension Table |
|---|---|---|
| Name patterns | Contains fact_, fct_, sales, orders, transactions, events, log, movements |
Contains dim_, lookup, calendar, date, product, customer, employee, geography, category, status |
| Column patterns | Has multiple _id/_key foreign keys + numeric measure columns (amount, quantity, price, total, count) |
Has a single primary key + descriptive text columns (name, description, label, title, code) |
| Row count | Typically high (transactions) | Typically low (reference data) |
| Numeric density | Many numeric columns relative to total | Few numeric columns, mostly strings |
If ambiguous, default to dimension. Better to have an extra dimension than miss a fact table's relationships.
Relationship Inference Rules
Detect relationships by matching column names across tables:
- Exact key match:
orders.customer_id→customers.customer_id(orcustomers.id) - Name-based match: column named
{table_name}_idor{table_name}_keyin a fact table points to the dimension with that name - Single-column PK in dimension: if a dimension has exactly one
_id/_keycolumn and a fact table has a column with the same name, infer the relationship - Date keys: columns named
date,date_id,fecha,order_date, etc. connect to a date/calendar dimension
All relationships are many-to-one (fact → dimension) unless the agent detects a bridge/junction table (many-to-many).
Cross-filter direction: Single by default. Only use Both for bridge tables.
DAX Measure Generation Rules
Generate measures based on column classification:
| Column Pattern | Measure | DAX Expression |
|---|---|---|
amount, total, revenue, sales, importe |
Sum | SUM('{table}'[{column}]) |
quantity, qty, count, cantidad, units |
Sum | SUM('{table}'[{column}]) |
price, cost, precio, coste |
Average | AVERAGE('{table}'[{column}]) |
discount, descuento |
Sum + Average | Both measures |
| Any numeric in fact table | Row Count | COUNTROWS('{table}') (one per fact table) |
id/key in dimension |
Distinct Count | DISTINCTCOUNT('{table}'[{column}]) |
Naming convention: Total {Column}, Avg {Column}, # {Table} (row count), # Unique {Column} (distinct count). Use business-friendly names, not technical ones.
Data Type Mapping
Map lakehouse (Spark) types to TMDL types:
| Spark Type | TMDL dataType |
Notes |
|---|---|---|
string |
string |
|
int, integer |
int64 |
TMDL uses int64 for all integers |
long, bigint |
int64 |
|
float |
double |
TMDL has no float, use double |
double |
double |
|
decimal |
decimal |
|
boolean |
boolean |
|
date |
dateTime |
TMDL uses dateTime for dates |
timestamp |
dateTime |
TMDL File Structure
The semantic model is a folder of .tmdl files plus metadata:
{model_name}/
├── definition/
│ ├── database.tmdl # Database-level settings
│ ├── model.tmdl # Model-level settings (culture, access)
│ ├── relationships.tmdl # All relationships
│ └── tables/
│ ├── {table1}.tmdl # Table definition + columns + measures + partition
│ ├── {table2}.tmdl
│ └── ...
└── definition.pbism # Semantic model settings
database.tmdl template
database '{MODEL_NAME}'
compatibilityLevel: 1604
model.tmdl template
The model must include a dataSource block pointing to the lakehouse's SQL analytics endpoint. The agent must fetch this endpoint from the Fabric REST API: GET /v1/workspaces/{id}/lakehouses/{id} → look for properties.sqlEndpointProperties.connectionString.
model Model
culture: es-ES
defaultPowerBIDataSourceVersion: powerBI_V3
discourageImplicitMeasures
sourceQueryCulture: es-ES
dataSource '{LAKEHOUSE_NAME}'
type: structured
connectionDetails
protocol: tds
address
server: {SQL_ENDPOINT}
database: {LAKEHOUSE_NAME}
credential
AuthenticationKind: OAuth2
Table .tmdl template (for each table)
Partitions use Power Query m expressions with mode: directQuery connecting to the SQL analytics endpoint. Do NOT use entity partitions or expressionSource: DatabaseQuery — those fail when deploying via REST API.
table '{TABLE_NAME}'
measure '# {TABLE_NAME}' = COUNTROWS('{TABLE_NAME}')
formatString: #,##0
column {COLUMN_NAME}
dataType: {TMDL_TYPE}
sourceColumn: {SOURCE_COLUMN}
summarizeBy: {none|sum|average|count}
partition '{TABLE_NAME}' = m
mode: directQuery
source =
let
Source = Sql.Database("{SQL_ENDPOINT}", "{LAKEHOUSE_NAME}"),
dbo_{TABLE_NAME} = Source{{[Schema="dbo",Item="{TABLE_NAME}"]}}[Data]
in dbo_{TABLE_NAME}
relationships.tmdl template
relationship {GUID}
fromColumn: '{FACT_TABLE}'.{FK_COLUMN}
toColumn: '{DIM_TABLE}'.{PK_COLUMN}
definition.pbism
{
"version": "4.0",
"settings": {}
}
Sample Data Analysis
When classifying tables and inferring relationships, analyze sample data (first 100 rows) to:
- Confirm cardinality: A column with 10 distinct values in 10000 rows is likely a FK to a dimension
- Detect date formats: Sample values reveal if a string column is actually a date
- Detect key patterns: IDs that are sequential integers vs UUIDs vs composite
- Validate relationships: Check that FK values actually exist in the referenced dimension's PK
Gotchas
- Star schema is mandatory. DAX is optimized for star schema. Flat/denormalized tables work but give poor performance and confuse AI tools. If the lakehouse has a single flat table, the agent should recommend splitting it into fact + dimensions (but not do it automatically).
- Partitions must use Power Query
mwithmode: directQuery. Do NOT useentitypartitions,expressionSource: DatabaseQuery, ormode: directLake— those all fail when deploying via the REST API. The correct pattern is a Power QuerySql.Database()expression pointing to the lakehouse SQL analytics endpoint. See the table template above. - The model needs a
dataSourceblock. Without it, the partition'sSql.Database()call has no endpoint. Fetch the SQL analytics endpoint fromGET /v1/workspaces/{id}/lakehouses/{id}→properties.sqlEndpointProperties.connectionString. Usecredential: AuthenticationKind: OAuth2. - Use the script for SQL endpoint and schema discovery, not MCP. The MCP server doesn't expose the SQL analytics endpoint and
onelake_get_tablefails for some lakehouses. Use the script'ssql-endpointcommand for the endpoint andlist-tablescommand for table schemas. These use the REST API + Delta log reading and are reliable. summarizeBy: nonefor all non-measure columns. Only measure columns should havesum/average. SettingsummarizeByon regular columns creates implicit measures which pollute the model.- Use
discourageImplicitMeasuresin model.tmdl. This forces explicit DAX measures and prevents automatic SUM/COUNT on numeric columns. - Spanish locale: Use
culture: es-ESandsourceQueryCulture: es-ESin model.tmdl. Format strings use.for thousands and,for decimals in Spanish. - Relationship GUIDs: Generate a unique UUID for each relationship. Use Python's
uuid.uuid4(). - Column names with spaces or special chars: Wrap in single quotes in TMDL:
column 'Total Amount'. - Key columns should be
isHiddenandisKey: Primary keys in dimensions should be hidden from report view and marked as the table key. - One
COUNTROWSmeasure per fact table: Always generate this as the base row-count measure. - The Fabric API requires
definitionas a required field for semantic model creation (unlike notebooks where it's optional). Thedefinition/folder (TMDL) must be included in the payload. displayNamemust be unique. Use the script'slistcommand to check before deploying. If a model with the same name exists, ask the user — do not overwrite.
Deployment, Management & Cleanup
Use ./scripts/fabric_semantic_model.py to manage semantic models in Fabric via the REST API. Uses DefaultAzureCredential (Azure CLI session).
Install dependencies (one-time):
pip install azure-identity requests
Deploy a semantic model
Reads a local TMDL folder, base64-encodes each file, and creates a SemanticModel item via POST /v1/workspaces/{id}/semanticModels. Includes the .platform and definition.pbism files.
python scripts/fabric_semantic_model.py deploy <workspace_id> <model_name> <tmdl_folder_path>
List semantic models
python scripts/fabric_semantic_model.py list <workspace_id> [<model_name>]
Delete a semantic model
python scripts/fabric_semantic_model.py delete <workspace_id> <model_id>
List tables with schemas
Returns all tables in a lakehouse with their column names and types as JSON. Uses the Fabric REST API to list tables, then reads each table's Delta log to extract the schema. Use it instead of MCP onelake_get_table if it fails.
python scripts/fabric_semantic_model.py list-tables <workspace_id> <lakehouse_id>
Get lakehouse SQL analytics endpoint
Returns the SQL analytics endpoint required for the dataSource block in model.tmdl. Run this in Phase 1 and use the output when generating TMDL files.
python scripts/fabric_semantic_model.py sql-endpoint <workspace_id> <lakehouse_id>
Deployment gotchas
- The
definitionfield is required for semantic model creation — unlike notebooks, you cannot create an empty model and update it later. displayNamemust be unique in the workspace.- TMDL format is the default and recommended. Use
definition/folder structure. - The API may return 202 for provisioning — the script polls automatically.
Global Rules
- TMDL format only. Generate TMDL files, not model.bim (TMSL). TMDL is the modern format, text-based, and Git-friendly.
- Star schema. Always model as star schema with clear fact and dimension tables.
- Explicit measures only. Use
discourageImplicitMeasuresand create all measures explicitly in DAX. - Business-friendly names. Tables, columns, and measures should use clear names like "Total Revenue" not "TR_AMT".
- Non-destructive. Never overwrite an existing semantic model. Deploy with a unique name or ask the user first.
04 同仓库更多 Skills
Fabric 合成数据生成
fabric-synthetic-data
生成合成数据并上传到 Microsoft Fabric 湖仓表。涵盖星型架构生成、逼真的假数据(姓名、日期、ID、交易)、可配置的行数、表之间的引用完整性、Parquet 导出以及 OneLake 上传。适用场景:合成数据、测试数据、假数据、样本数据、生成表、填充湖仓、种子数据、模拟数据。
Fabric 湖仓优化审查
fabric-optimization-review
审查并优化 Microsoft Fabric 湖仓中的数据存储、数据模型和语义模型。涵盖 Delta 表优化(V-Order、Z-Order、压缩、分区)、数据模型反模式检测、语义模型 DAX 审查、关系优化、AI/智能体就绪度(描述、同义词、语言模式、显示文件夹)、工作区审计(Fabric 最佳实践、维度建模、Direct Lake)以及可操作的建议报告。适用场景:优化、性能、审查、调优、最佳实践、V-Order、Z-Order、分区、压缩、DAX 优化、数据模型审查、AI 就绪度、智能体、NLQ、Copilot、自然语言、工作区审计、Direct Lake、维度建模、审计。
Fabric 元数据创建
fabric-metadata-creation
为 Microsoft Fabric lakehouse 表和语义模型创建可供审查的元数据提案。涵盖模式分析、简洁的技术与业务描述、术语表术语、分类、类似 Purview 的敏感度标签提案、PII 与敏感数据检测、关系与血缘提示、数据质量规则、CDE 候选、数据产品、JSON/PDF 输出,以及管理员审查问题。适用场景:元数据创建、数据目录、Purview 映射、术语表推断、数据分类、敏感度标签、管理员审查、治理元数据。
Microsoft Fabric 数据清洗
fabric-data-cleaner
提供算法参考和自包含的 PySpark notebook 模板,用于清理 Microsoft Fabric lakehouse 表。涵盖数据画像、重复检测、空值分析、类型验证、统计、IQR 异常值检测、日期格式验证、西班牙 DNI/NIE 校验和验证,以及 email/phone 格式检查。每个 notebook 都可独立上传并在 Fabric 中运行。