如何在Power Automate中重构项目-用户关联数组?
Power Automate 按用户聚合项目数组解决方案
需求说明
输入数组包含项目与关联用户的一对多/多对多关系,需要重组为以用户为主体,聚合其所有关联项目的结构:
输入示例
[ { "Ref": "B0048", "Desc": "Electricity Management", "User": { "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser", "Claims": "i:0#.f|membership|Gary@example.net", "DisplayName": "Gary", "Email": "Gary@example.net", "Department": "N/A", "JobTitle": "Big Boss" } }, { "Ref": "B0049", "Desc": "New Hardware Business Case", "User": { "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser", "Claims": "i:0#.f|membership|Martin@example.net", "DisplayName": "Martin@example", "Email": "Martin@example.net", "Department": "Maintenance", "JobTitle": "Maintenance Manager" } }, { "Ref": "B0050", "Desc": "Estimating Tool", "User": { "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser", "Claims": "i:0#.f|membership|Toni@example.net", "DisplayName": "Toni", "Email": "Toni@example.net", "Department": "Construction", "JobTitle": "Construction Manager" } }, { "Ref": "B0050", "Desc": "Estimating Tool", "User": { "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser", "Claims": "i:0#.f|membership|Rob@example.net", "DisplayName": "Rob", "Email": "Rob@example.net", "Department": "Construction", "JobTitle": "Construction Coordinator" } }, { "Ref": "B0051", "Desc": "New Software Business Case", "User": { "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser", "Claims": "i:0#.f|membership|Gary@example.net", "DisplayName": "Gary", "Email": "Gary@example.net", "Department": "N/A", "JobTitle": "Big Boss" } } ]
目标输出
[ { "DisplayName": "Gary", "Email": "Gary@example.net", "Projects": [ { "Ref": "B0048", "Desc": "Electricity Management" }, { "Ref": "B0051", "Desc": "New Software Business Case" } ] }, { "DisplayName": "Martin@example", "Email": "Martin@example.net", "Projects": [ { "Ref": "B0049", "Desc": "New Hardware Business Case" } ] }, { "DisplayName": "Toni", "Email": "Toni@example.net", "Projects": [ { "Ref": "B0050", "Desc": "Estimating Tool" } ] }, { "DisplayName": "Rob", "Email": "Rob@example.net", "Projects": [ { "Ref": "B0050", "Desc": "Estimating Tool" } ] } ]
实现步骤
1. 提取并去重用户列表
- 添加Select动作,数据源选择原始输入数组
- 在Select的映射中,只保留用户核心信息:
DisplayName:item()?['User']?['DisplayName']Email:item()?['User']?['Email']
- 添加Compose动作,使用
union()函数去重用户列表,表达式:
(注:union(body('Select'), body('Select'))union需要两个相同数组参数,传入两次Select结果即可自动去重)
2. 初始化结果数组变量
- 添加Initialize variable动作,变量名设为
UserProjectsArray,类型选Array,初始值留空([])
3. 遍历唯一用户,聚合对应项目
- 添加Apply to each动作,数据源选择步骤1中Compose的输出(去重后的用户列表)
- 在循环内添加以下动作:
- Filter Array:筛选原始输入数组中当前用户的所有项目
- 数据源:原始输入数组
- 筛选条件:
item()?['User']?['Email']等于items('Apply_to_each')?['Email']
(用Email作为唯一标识比DisplayName更可靠,避免同名)
- Select:提取筛选后项目的
Ref和Desc- 数据源:Filter Array的输出
- 映射:
Ref:item()?['Ref']Desc:item()?['Desc']
- Append to array variable:将当前用户与聚合后的项目组合成对象,添加到
UserProjectsArray- 变量名:
UserProjectsArray - 值:
{ "DisplayName": "@{items('Apply_to_each')?['DisplayName']}", "Email": "@{items('Apply_to_each')?['Email']}", "Projects": "@{body('Select_2')}" }
- 变量名:
- Filter Array:筛选原始输入数组中当前用户的所有项目
4. 输出最终结果
- 添加Compose动作,数据源选择
UserProjectsArray变量,即可得到目标格式的数组
关键注意事项
- 用Email作为用户唯一标识:避免DisplayName重复导致聚合错误
- 表达式中使用
?安全导航符:防止空值引发流程报错 - 去重时必须用
union:直接Select会保留重复用户,union是Power Automate中高效的数组去重方式
内容的提问来源于stack exchange,提问作者Courty
相关产品推荐
相关产品推荐

