我需要格式化聚合日期,以便生成可读的和可排序格式。我正在创建一个时间序列,使用以下代码汇总一段时间内的请求数:
let us_date = (t:datetime)
{
strcat( getmonth(t - 8h), "-", dayofmonth(t - 8h))
};
requests
| project timestamp, client_IP, local_date = us_date(timestamp)
| where timestamp >= now() - 30d and timestamp <= now() + 1d
| summarize uniqueUsers = dcount(client_IP) by usdate = us_date(timestamp)
| order by usdate desc
产生:
数据很好,但由于格式化,排序顺序已关闭。
为了正确排序,应该看起来像05-09
,但我看不到这种格式化的方法。
请注意,我可以执行以下操作,并以正确的顺序但丑陋格式获取数据:
let us_date = (t:datetime)
{
strcat( getyear(t - 8h) , '-', getmonth(t - 8h), "-", dayofmonth(t - 8h))
};
requests
| project timestamp, client_IP
| where timestamp >= now() - 30d and timestamp <= now() + 1d
| summarize uniqueUsers = dcount(client_IP) by usdate = bin(timestamp - 8h,1d)
| order by usdate desc
但是这会生成非常详细的列表视图,并且还会将时间嵌入到图表中。
任何想法如何解决数字格式问题或如何在查询中获取另一个值以允许排序而不会丢弃结果?
答案 0 :(得分:1)
a)您可以在project
之后summarize
更改列格式,列名称或列顺序。
b)您可以格式化bin
的结果。
答案 1 :(得分:0)
有点难看,但字符串解析可以解决这个问题:
requests
| project timestamp, client_IP
| where timestamp >= now() - 30d and timestamp <= now() + 1d
| summarize uniqueUsers = dcount(client_IP) by usdate = bin(timestamp - 8h,1d)
| parse tostring(usdate) with year "-" shortDate "T" *
| project shortDate, uniqueUsers**
| order by shortDate desc
解决方案位于parse
行 - 仅占用日期的月份和日期部分