创建用户定义的函数以根据日期时间列计算四分之一 (KQL/Kusto/Azure 数据资源管理器)

Create User-Defined Function to Calculate Quater from Datetime Column (KQL / Kusto / Azure Data Explorer)

下面的第一个查询产生了正确的输出;但是,我的数据集有许多日期时间列,我想在其中计算季度。我想使用用户定义的函数产生相同的输出。我试图创建该函数,但出现 'General_BadRequest' 错误并且无法弄清楚原因。有人可以帮助更正我下面的功能吗?

另外,Kusto 中是否已经内置了计算季度的函数?

工作查询 - 输出正确

range Date from todatetime('2021-01-01') to todatetime('2021-12-31') step 7d
| extend CaseMethod=case(
getmonth(Date) between (7 .. 9), 1,
getmonth(Date) between (10 .. 12), 2,
getmonth(Date) between (1 .. 3), 3,
getmonth(Date) between (4 .. 6), 4, 0)

使用函数查询 - 结果为 'General_BadRequest'

let QtrFunc = (x:datetime) {
toscalar(
datatable ( Qtr:long, Mo_Start:long, Mo_End:long )
[ 1, 7,  9,
  2, 10, 12,
  3, 1,  3,
  4, 4,  6  ]
| where getmonth(x) between (Mo_Start .. Mo_End)
| project Qtr ) } ;
range DateCol from todatetime('2021-01-01') to todatetime('2021-12-31') step 7d
| extend FunctionMethod=QtrFunc(DateCol)

I would like to produce the same output using a user-defined function.

您可以使用 case() 函数,而不是在您的函数中使用 datatable

let get_quarter = (dt:datetime) {
    case(monthofyear(dt) between (7 .. 9), 1,
         monthofyear(dt) between (10 .. 12), 2,
         monthofyear(dt) between (1 .. 3), 3,
         monthofyear(dt) between (4 .. 6), 4,
         0)
}
;
range col1 from datetime(2021-01-01) to datetime(2021-12-31) step 7d
| extend result = get_quarter(col1)

Also, is there a function already built into Kusto that calculates quarters?

对于一年如何划分季度,不同的人可能有不同的定义。例如:你的情况是7月到9月是第一个季度,但是别人的情况是1月到3月是第一个季度。