youdata-data-model-attribution-analysis
SKILL.md
有数数据模型归因分析技能
功能
该技能允许用户对指定的数据模型执行归因分析,找出哪些维度导致了指标的变化。通过对比两个时间段的数据,分析各维度对指标变化的贡献度。
使用场景
- 当用户想知道某个指标(如销售额、利润)在两个时间段之间变化的原因时
- 当用户想分析哪些维度(如地区、产品类别)对指标变化的贡献最大时
- 数据波动的根因分析
依赖环境
- Node.js 20+
- 需要网络连接访问知数平台API
- 需要安装 skill
youdata-env-config并完成配置 - 建议先使用 skill
youdata-data-model-meta-query获取数据模型元数据,以便了解可用的字段信息(包括 fieldId)
使用方法
- 先使用
youdata-data-model-meta-query获取数据模型的字段信息(了解有哪些度量和维度以及它们的 fieldId) - 构造归因分析配置(JSON格式)
- 调用脚本执行归因分析
调用示例
- step1:使用 skill
youdata-env-config获取 env(json字符串格式),同一次会话youdata系列的skill可以复用,不用二次生成 - step2:使用 skill
youdata-data-model-meta-query获取数据模型元数据,了解可用字段和 fieldId - step3:获取当前 SKILL.md 的绝对目录地址 skillFolder
- step4:构造归因分析配置JSON,调用脚本
node ${skillFolder}/scripts/index.js <dataModelId> '<analysisConfig>' <env>
参数说明
dataModelId: 数据模型的唯一标识符(数字)analysisConfig: JSON字符串,包含归因分析的配置参数(结构见下方)env: 环境变量JSON字符串(来自 youdata-env-config)
analysisConfig 字段详细说明
detectMeasure(归因度量,必填)
归因分析的主指标,即需要分析变化原因的度量字段。
fieldId(string,必填):度量字段的 fieldId,从数据模型元数据中获取aggregator(string,选填):聚合方式,如"SUM","AVG","COUNT","MAX","MIN"等。不传时使用字段默认聚合方式
dimensions(归因维度,必填)
用于分析归因的维度字段数组,分析哪些维度值导致了指标变化。数组中每个元素:
fieldId(string,必填):维度字段的 fieldId
detectFilter(归因日期筛选,必填)
归因时间段的筛选条件,即"当前时间段"。仅支持日期/日期时间字段筛选。
pill.fieldId(string,必填):用作筛选的日期字段 fieldIdfilter.match(MatchCondition,必填):筛选条件
compareFilter(对比日期筛选,必填)
对比时间段的筛选条件,即"对比时间段"。日期字段必须和 detectFilter 一致。
pill.fieldId(string,必填):用作筛选的日期字段 fieldId(应与 detectFilter 一致)filter.match(MatchCondition,必填):筛选条件
structuredQueryFilters(普通维度筛选,选填)
额外的筛选条件数组,用于过滤数据。每个元素格式与 detectFilter 相同。默认传空数组 []。
MatchCondition 类型说明
filter.match 支持以下条件类型:
| $type | 字段 | 说明 | 示例 |
|---|---|---|---|
Range |
range: { minBound?, maxBound?, excludeMin?, excludeMax? } |
范围筛选(日期筛选推荐使用此类型) | {"$type":"Range","range":{"minBound":"2026-03-01 00:00:00","maxBound":"2026-04-01 00:00:00", "excludeMin": false, "excludeMax": true}} |
Select |
select: [值数组] |
列表筛选(仅筛选单个值时也使用此类型) | {"$type":"Select","select":["华东","华北"]} |
Unselect |
unselect: [值数组] |
排除筛选(仅排除单个值时也使用此类型) | {"$type":"Unselect","unselect":["华南"]} |
Contains |
contains: string |
子串包含 | {"$type":"Contains","contains":"北京"} |
Without |
without: string |
子串不包含 | {"$type":"Without","without":"测试"} |
IsJust |
isJust: string |
等于 | {"$type":"IsJust","isJust":"华东"} |
Isnt |
isnt: string |
不等于 | {"$type":"Isnt","isnt":"未知"} |
StartWith |
startWith: string |
以...开头 | {"$type":"StartWith","startWith":"华"} |
EndWith |
endWith: string |
以...结尾 | {"$type":"EndWith","endWith":"区"} |
ContainsList |
containsList: [string] |
包含列表中任一 | {"$type":"ContainsList","containsList":["北京","上海"]} |
WithoutList |
withoutList: [string] |
不包含列表中任一 | {"$type":"WithoutList","withoutList":["测试","demo"]} |
IsNull |
isNull: boolean |
是否为空 | {"$type":"IsNull","isNull":true} |
IsBool |
isBool: boolean |
布尔匹配 | {"$type":"IsBool","isBool":true} |
FindInSet |
findInSet: [值数组] |
集合包含 | {"$type":"FindInSet","findInSet":["A","B"]} |
NotStartWith |
notStartWith: string |
不以...开头 | {"$type":"NotStartWith","notStartWith":"测"} |
NotEndWith |
notEndWith: string |
不以...结尾 | {"$type":"NotEndWith","notEndWith":"区"} |
Like |
like: string |
模式匹配 | {"$type":"Like","like":"%北%"} |
analysisConfig 结构示例
{
"detectMeasure": {
"fieldId": "12345",
"aggregator": "SUM"
},
"dimensions": [
{ "fieldId": "12346" },
{ "fieldId": "12348" }
],
"detectFilter": {
"pill": { "fieldId": "12347" },
"filter": {
"match": {
"$type": "Range",
"range": {
"minBound": "2026-03-01 00:00:00",
"maxBound": "2026-04-01 00:00:00",
"excludeMin": false,
"excludeMax": true
}
}
}
},
"compareFilter": {
"pill": { "fieldId": "12347" },
"filter": {
"match": {
"$type": "Range",
"range": {
"minBound": "2026-02-01 00:00:00",
"maxBound": "2026-03-01 00:00:00",
"excludeMin": false,
"excludeMax": true
}
}
}
},
"structuredQueryFilters": [
{
"pill": { "fieldId": "12349" },
"filter": {
"match": {
"$type": "Select",
"select": ["华东"]
}
}
}
]
}
返回结果
返回JSON格式的归因分析结果,包含各维度对指标变化的贡献度分析数据。
错误处理
- 如果没有安装 skill
youdata-env-config,会提示安装该技能 - 缺少参数时会提示正确用法
- 配置不完整时会提示配置信息
- 网络错误会显示具体错误信息
- 脚本查询失败时显示错误信息并立刻终止问答