building-dbt-semantic-layer by dbt-labs/dbt-agent-skills
npx skills add https://github.com/dbt-labs/dbt-agent-skills --skill building-dbt-semantic-layer此技能指导如何创建和修改 dbt 语义层组件:语义模型、实体、维度和指标。
语义层 YAML 规范有两个版本:
在项目中查找现有的语义层配置:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
semantic_models: 键 → 旧版规范semantic_model: 块 → 最新规范如果语义层已存在:
如果不存在语义层:
一旦确定使用哪个规范,请遵循相应指南的实现工作流程(步骤 1-4)进行所有 YAML 编写。这些指南是自包含的,包含完整示例。
用户可能会以几种不同的方式提出与使用语义层构建指标相关的问题。以下是需要注意的常见切入点:
当用户描述指标或分析需求时(例如,“我需要按细分市场跟踪客户生命周期价值”):
当用户指定要公开的模型时(例如,“向 customers 模型添加语义层”):
两种路径最终会收敛到相同的实现工作流程。
用户要求为未指定的项目或模型构建语义层。(“为我的项目构建语义层”)
两种规范都支持这些指标类型。关于 YAML 语法,请参阅特定规范指南。
直接聚合单个列表达式。最常见的指标类型,也是所有其他指标的基础。
metrics: 下定义,包含 type: simple、agg 和 exprmetrics:,通过 type_params.measure 引用一个度量使用数学表达式组合多个指标。用于计算利润(收入 - 成本)或增长率(使用 offset_window 的环比)等。
在滚动窗口或期初至今期间聚合指标。需要时间轴。用于运行总计、滚动窗口(例如,7 天移动平均)或期初至今(MTD、YTD)。
注意:window 和 grain_to_date 不能在同一累积指标上同时使用。
在两个指标之间创建比率(分子 / 分母)。用于转化率、百分比和比例。分子和分母都可以有可选的筛选器。
衡量在特定时间窗口内,一个事件导致另一个事件针对特定实体发生的频率。用于漏斗分析(例如,访问到购买的转化率)。支持 constant_properties 以确保两个事件间维度值的一致性。
筛选器可以添加到简单指标或高级指标的指标输入中。使用 Jinja 模板语法:
filter: |
{{ Entity('entity_name') }} = 'value'
filter: |
{{ Dimension('primary_entity__dimension_name') }} > 100
filter: |
{{ TimeDimension('time_dimension', 'granularity') }} > '2026-01-01'
filter: |
{{ Metric('metric_name', group_by=['entity_name']) }} > 100
重要提示:筛选表达式只能引用在语义模型中声明为维度或实体的列。未定义为维度的原始表列不能在筛选器中使用——即使它们出现在度量的 expr 中。
此技能引用了 dbt-autofix,这是一个由 dbt Labs 维护的一方工具,用于自动化弃用修复和包更新。
编写 YAML 后,分两个阶段进行验证:
dbt parse(或 Fusion 的 dbtf parse)以确认 YAML 语法和引用dbt sl validate(使用 dbt 平台时的 dbt Cloud CLI 或 Fusion CLI)mf validate-configs(MetricFlow CLI)重要提示:mf validate-configs 从编译后的清单读取,而不是直接从 YAML 文件读取。如果自上次解析后编辑了 YAML,必须在 mf validate-configs 看到更改之前再次运行 dbt parse(或 dbtf parse)。
注意:当在本地使用 Fusion 和 MetricFlow(不使用 dbt 平台)时,dbtf parse 会显示 warning: dbt1005: Skipping semantic manifest validation due to: No dbt_cloud.yml config。这是预期的——在此设置中,使用 mf validate-configs 进行语义层验证。
在两项验证都通过之前,不要认为工作已完成。
修改现有语义层配置时:
| 陷阱 | 修复方法 |
|---|---|
| 缺少时间维度 | 每个包含指标/度量的语义模型都需要一个默认时间维度 |
同时使用 window 和 grain_to_date | 累积指标只能有一个 |
| 混合规范语法 | 不要在最新规范中使用 type_params,也不要在旧版规范中使用直接键 |
| 在非维度列上筛选 | 筛选表达式只能使用声明的维度/实体,不能使用原始列 |
mf validate-configs 显示过时结果 | 首先重新运行 dbt parse / dbtf parse 以重新生成清单 |
MetricFlow 安装破坏 dbt-semantic-interfaces | 安装 dbt-metricflow(而不是裸 metricflow)以获得兼容的依赖版本 |
每周安装次数
78
仓库
GitHub 星标数
252
首次出现
Jan 29, 2026
安全审计
安装于
github-copilot53
opencode51
gemini-cli50
codex50
kimi-cli47
amp47
This skill guides the creation and modification of dbt Semantic Layer components: semantic models, entities, dimensions, and metrics.
There are two versions of the Semantic Layer YAML spec:
Look for existing semantic layer configuration in the project:
semantic_models: key in YAML files → legacy specsemantic_model: block nested under a model → latest specIf semantic layer already exists:
uvx dbt-autofix deprecations --semantic-layer or the migration guide. They don't have to upgrade; continuing with legacy is fine.If no semantic layer exists:
Once you know which spec to use, follow the corresponding guide's implementation workflow (Steps 1-4) for all YAML authoring. The guides are self-contained with full examples.
Users may ask questions related to building metrics with the semantic layer in a few different ways. Here are the common entry points to look out for:
When the user describes a metric or analysis need (e.g., "I need to track customer lifetime value by segment"):
When the user specifies a model to expose (e.g., "Add semantic layer to customers model"):
Both paths converge on the same implementation workflow.
User asks to build the semantic layer for a project or models that are not specified. ("Build the semantic layer for my project")
Both specs support these metric types. For YAML syntax, see the spec-specific guides.
Directly aggregate a single column expression. The most common metric type and the building block for all others.
metrics: on the model with type: simple, agg, and exprmetrics: referencing a measure via type_params.measureCombine multiple metrics using a mathematical expression. Use for calculations like profit (revenue - cost) or growth rates (period-over-period with offset_window).
Aggregate a metric over a running window or grain-to-date period. Requires a time spine. Use for running totals, trailing windows (e.g., 7-day rolling average), or period-to-date (MTD, YTD).
Note: window and grain_to_date cannot be used together on the same cumulative metric.
Create a ratio between two metrics (numerator / denominator). Use for conversion rates, percentages, and proportions. Both numerator and denominator can have optional filters.
Measure how often one event leads to another for a specific entity within a time window. Use for funnel analysis (e.g., visit-to-purchase conversion rate). Supports constant_properties to ensure the same dimension value across both events.
Filters can be added to simple metrics or metric inputs to advanced metrics. Use Jinja template syntax:
filter: |
{{ Entity('entity_name') }} = 'value'
filter: |
{{ Dimension('primary_entity__dimension_name') }} > 100
filter: |
{{ TimeDimension('time_dimension', 'granularity') }} > '2026-01-01'
filter: |
{{ Metric('metric_name', group_by=['entity_name']) }} > 100
Important : Filter expressions can only reference columns that are declared as dimensions or entities in the semantic model. Raw table columns that aren't defined as dimensions cannot be used in filters — even if they appear in a measure's expr.
This skill references dbt-autofix, a first-party tool maintained by dbt Labs for automating deprecation fixes and package updates.
After writing YAML, validate in two stages:
dbt parse (or dbtf parse for Fusion) to confirm YAML syntax and referencesdbt sl validate (dbt Cloud CLI or Fusion CLI when using the dbt platform)mf validate-configs (MetricFlow CLI)Important : mf validate-configs reads from the compiled manifest, not directly from YAML files. If you've edited YAML since the last parse, you must run dbt parse (or dbtf parse) again before mf validate-configs will see the changes.
Note : When using Fusion with MetricFlow locally (without the dbt platform), dbtf parse will show warning: dbt1005: Skipping semantic manifest validation due to: No dbt_cloud.yml config. This is expected — use mf validate-configs for semantic layer validation in this setup.
Do not consider work complete until both validations pass.
When modifying existing semantic layer config:
| Pitfall | Fix |
|---|---|
| Missing time dimension | Every semantic model with metrics/measures needs a default time dimension |
Using window and grain_to_date together | Cumulative metrics can only have one |
| Mixing spec syntax | Don't use type_params in latest spec or direct keys in legacy spec |
| Filtering on non-dimension columns | Filter expressions can only use declared dimensions/entities, not raw columns |
mf validate-configs shows stale results | Re-run dbt parse / dbtf parse first to regenerate the manifest |
Weekly Installs
78
Repository
GitHub Stars
252
First Seen
Jan 29, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot53
opencode51
gemini-cli50
codex50
kimi-cli47
amp47
Excel财务建模规范与xlsx文件处理指南:专业格式、零错误公式与数据分析
45,800 周安装
MetricFlow install breaks dbt-semantic-interfaces | Install dbt-metricflow (not bare metricflow) to get compatible dependency versions |