> For the complete documentation index, see [llms.txt](https://digitalgarden.batamladen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://digitalgarden.batamladen.com/notes/query-languages/kql/summarize-or-bin-or-dcount-or-avg-or-countif.md).

# summarize | bin |dcount |avg | countif

### bin

**`bin()`** groups values into buckets (bins) of a specified size.

When using bin we should specify 2 params, the column that we want to make buckets of and the timeframe.

```
DeviceEvents
| summarize count() by bin(Timestamp,1h)
```

<figure><img src="https://2490252025-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUwIKQPujWqWSVFxQaX98%2Fuploads%2Fuv6ti9K2nUjcreU8EKAh%2Fimage.png?alt=media&amp;token=d5995fb6-70da-4983-90fe-fea0b904a782" alt=""><figcaption></figcaption></figure>

another example:

Guest sign-ins in the past month separated by day

```
SigninLogs
| where Timestamp >= ago(30d)
| where UserType == 'Guest'
| summarize count() by bin(TimeGenerated, 1h)
| sort by TimeGenerated asc
```

***

### avg()

**`avg()`** = calculates the **average (mean)** value of a column.

| Response Time |
| ------------- |
| 100           |
| 200           |
| 300           |

```
| summarize avg(ResponseTime)
```

Result: 200

Example:

```kql
VMConnection
| project TimeGenerated, BytesSent
| summarize count(), min(BytesSent), max(BytesSent) by bin(TimeGenerated, 1h)
| sort by TimeGenerated asc
```

<figure><img src="https://2490252025-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUwIKQPujWqWSVFxQaX98%2Fuploads%2F9mZhIxLQFa3WSc9dLGet%2Fimage.png?alt=media&amp;token=7a89f571-1254-4b13-9a41-266417f80aa6" alt=""><figcaption></figcaption></figure>

if we now add the avg, it will add another field that will display the average maxbytes sent per count.

```
VMConnection
| project TimeGenerated, BytesSent
| summarize count(), min(BytesSent), max(BytesSent), avg(BytesSent) by bin(TimeGenerated, 1h)
| sort by TimeGenerated asc
```

***

### countif

**`countif()`** = counts only the rows where a condition is true.

| User    | Result  |
| ------- | ------- |
| Alice   | Failed  |
| Bob     | Failed  |
| Charles | Success |
| David   | Success |

```
| summarize countif(Result == "Failed")
```

Returns: `2`

Example:

Find the total number of bytes in each hourly bucket, also count records that have value more than 0.

```
VMConnection
| project TimeGenerated, BytesSent
| summarize RecordsPerHour=count(), RecordsGreatherThanZero=countif(BytesSent > 0), min(BytesSent), max(BytesSent), TotalBytesSent=sum(BytesSent), avg(BytesSent) by bin(TimeGenerated, 1h)
| sort by TimeGenerated asc
```

<figure><img src="https://2490252025-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUwIKQPujWqWSVFxQaX98%2Fuploads%2FwGMA9XHj5dM6TCV8LF6H%2Fimage.png?alt=media&amp;token=a1b2f1bc-37cc-480e-94e5-a89df39b3a2b" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://digitalgarden.batamladen.com/notes/query-languages/kql/summarize-or-bin-or-dcount-or-avg-or-countif.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
