> 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="/files/Aj6eJPWTX1rvygu1IPdE" 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="/files/ey5UpqFyVGyyNQ4AJxr5" 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="/files/F4FUl4BHQpGtBH1FW66y" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
