You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KQL基于汇总值计算分类百分比的问题求助

解决KQL计算分组占比的错误问题

你的错误原因是第二次summarize未指定分组依据,且Initial_replan作为第一次summarize的分组列,在无分组的二次聚合中无法直接引用——聚合后该列会被折叠,无法单独调用。

以下是两种正确的实现方式:

方法1:使用窗口函数(推荐)

通过sum() over (partition by Geography)获取每个地区的总计数,直接计算占比:

Table1 
| summarize Count1 = dcount(ShipmentId) by Geography, Initial_replan
| extend Total_per_Geography = sum(Count1) over (partition by Geography)
| extend Percentage = round(Count1 * 100.0 / Total_per_Geography, 2)
| project Geography, Initial_replan, Count1, Total_per_Geography, Percentage

方法2:先汇总地区总数再关联

先单独计算每个地区的总计数,再通过join关联原汇总表计算占比:

let Summary = Table1 | summarize Count1 = dcount(ShipmentId) by Geography, Initial_replan;
let GeographyTotal = Summary | summarize Total_per_Geography = sum(Count1) by Geography;
Summary
| join kind=inner GeographyTotal on Geography
| extend Percentage = round(Count1 * 100.0 / Total_per_Geography, 2)
| project Geography, Initial_replan, Count1, Total_per_Geography, Percentage

两种方法都能实现按Geography分组,计算Initial和Replan各自的占比。窗口函数写法更简洁,适合大多数场景;关联法则在复杂多维度计算时更灵活。

内容的提问来源于stack exchange,提问作者Yoonsik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 22:25:20