0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【KQL】ago(7d) と bin(TimeGenerated, 7d) の違いを具体例で理解する

Posted at

Microsoft Sentinel や Azure Monitor で使われる Kusto Query Language (KQL) では、時系列データの分析に便利な関数として ago()bin() がよく登場します。

この記事では、ago(7d)bin(TimeGenerated, 7d) の違いを、実際のクエリ例と出力結果を使って解説します。


1. ago(7d) とは?

ago(7d) は、「今からちょうど7日前の時刻」を表す関数です。
通常は where 句と組み合わせて、過去7日間のデータを抽出する ために使われます。

使用例

SecurityEvent
| where TimeGenerated > ago(7d)
| summarize count() by TimeGenerated

出力例(イメージ)

TimeGenerated count
2025-03-30 11:42:00 15
2025-03-30 12:10:00 8
2025-03-31 08:00:00 20

このように、TimeGenerated はそのままの時刻で表示され、生ログに近い形でデータが出力されます。

参考(Microsoft公式):
ago() - KQL 関数リファレンス


2. bin(TimeGenerated, 7d) とは?

bin() 関数は、時刻を一定の単位で丸めてグルーピングするために使います。
bin(TimeGenerated, 7d) は、時刻を7日単位でまとめることを意味します。

使用例

SecurityEvent
| summarize count() by bin(TimeGenerated, 7d)

出力例(イメージ)

TimeGenerated count
2025-03-01 00:00:00 320
2025-03-08 00:00:00 405
2025-03-15 00:00:00 390

このように、TimeGenerated が週の開始時刻(00:00)に丸められて集計されます。

参考(Microsoft公式):
bin() - KQL 関数リファレンス


違いのまとめ

項目 ago(7d) bin(TimeGenerated, 7d)
目的 データの期間を制限する データを時間単位で集約する
使い方 where TimeGenerated > ago(7d) summarize by bin(TimeGenerated, 7d)
出力 生の時刻データがそのまま出る 時間が7日単位で丸められて出る
主な用途 過去◯日間のデータ取得 週単位などでトレンドを見る

注意点

bin(TimeGenerated, ...) を使って時間単位で集計する場合でも、対象期間の制限は自動では行われません
明示的にデータの範囲を絞るには、以下のいずれかを必ず設定しましょう。

  • クエリ内で where TimeGenerated > ago(◯d) を記述する
  • ポータルの「時間の範囲」を適切に設定する(例:過去30日間 など)

時間の範囲を指定しないと、クエリの結果が返らない可能性があります。

1.結果が見つからない例 (TimeGeneratedは7dだが、実際に参照しているデータは24H分)

image.png

2.こちらは結果が返る例 (時間の範囲:クエリで指定と表示が変わった。s範囲の7d分の結果が返る)
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?