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

求助:基于现有表数据用Kusto构建新表并填充值

Kusto 生成日期范围并按规则填充值的实现方案

现有表结构

假设你的源表名为SourceTable,结构如下:

let SourceTable = datatable(month_end_date:datetime, value:int)
[
    datetime(2023-04-30), 0,
    datetime(2023-05-31), 1,
    datetime(2023-06-30), 0
];

实现查询

以下是优化后的Kusto查询,无需硬编码关键日期,直接从源表动态获取规则参数,生成指定日期范围并按要求填充值:

let SourceTable = datatable(month_end_date:datetime, value:int)
[
    datetime(2023-04-30), 0,
    datetime(2023-05-31), 1,
    datetime(2023-06-30), 0
];
// 从源表提取关键日期:最新月末日期、value为1的月末日期
let KeyDateParams = SourceTable
| summarize 
    latest_month_end = max(month_end_date),
    target_month_start = max_if(month_end_date, value == 1);
// 生成目标日期范围并展开为单条记录
print date_part = range(datetime(2023-05-28), datetime(2023-07-03), 1d)
| mvexpand date_part to typeof(datetime)
// 关联关键日期参数
| join kind=cross KeyDateParams
// 按规则填充值
| extend filled_value = case(
    date_part >= latest_month_end, 0,
    date_part >= target_month_start, 1,
    0
)
// 输出最终结果列
| project date_part, filled_value

规则说明

查询中的case函数严格遵循需求逻辑:

  • 当date_part ≥ 最新月末日期(2023-06-30)时,填充0
  • 当date_part ≥ value为1的月末日期(2023-05-31)且 < 最新月末日期时,填充1
  • 其余日期(< 2023-05-31)填充0

注意事项

  • 实际使用时,删除let SourceTable = ...这一段,直接替换为你的真实表名
  • 若需要固定日期范围,调整range函数中的起始、结束日期即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 05:22:56