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

如何使用KQL计算requiredValue字段并应用取整/取上限操作?

使用KQL计算requiredValue列

我的数据包含timestamp(时间戳)和value列,希望计算出requiredValue列,计算逻辑如下:
计算逻辑说明

示例数据的KQL定义:

let initialValue = 5;
datatable (timestamp: datetime, value: int)
[
    '2023-01-01', 10, 
    '2023-01-02', 20, 
    '2023-01-03', 30, 
    '2023-01-04', -10, 
    '2023-01-05', 40, 
    '2023-01-06', -50
]

实现代码

使用scan运算符可实现基于初始值的逐行累加计算,需先确保数据按时间顺序排列:

let initialValue = 5;
datatable (timestamp: datetime, value: int)
[
    '2023-01-01', 10, 
    '2023-01-02', 20, 
    '2023-01-03', 30, 
    '2023-01-04', -10, 
    '2023-01-05', 40, 
    '2023-01-06', -50
]
| order by timestamp asc
| scan with (
    step s: 
        requiredValue = case(isempty(s.requiredValue), initialValue + value, s.requiredValue + value);
)

逻辑说明

  • order by timestamp asc:保证数据按时间戳升序处理,确保计算顺序符合时间线
  • scan运算符:逐行迭代数据,通过case判断是否为第一行:
    • 第一行用初始值initialValue加上当前行的value得到requiredValue
    • 后续行用上一行的requiredValue累加当前行的value,生成最终目标列

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 22:32:27