# `xbar`

_Round down_
```syntax
x xbar y    xbar[x;y]
```

Where

- `x` is a positive numeric or timespan atom
- `y` is numeric or temporal

returns `y` rounded down to the nearest multiple of `x`. `xbar` is a [multithreaded primitive](mt-primitives.md).
```q
q)3 xbar til 16
0 0 0 3 3 3 6 6 6 9 9 9 12 12 12 15
q)2.5 xbar til 16
0 0 0 2.5 2.5 5 5 5 7.5 7.5 10 10 10 12.5 12.5 15
q)5 xbar 11:00 + 0 2 3 5 7 11 13
11:00 11:00 11:00 11:05 11:05 11:10 11:10
```

Interval bars are useful in aggregation queries. To get the last price and the total size in 10-minute bars:
```q
q)trade:([]time:09:30:00.000+0 120000 480000 650000 1100000 1500000 1810000 2300000;sym:`IBM;price:55.1 55.32 54.8 54.99 55 54.93 55.2 55.23;size:300 200 500 100 400 200 100 300)
q)select last price, sum size by 10 xbar time.minute from trade where sym=`IBM
minute| price size
------| ----------
09:30 | 54.8  1000
09:40 | 55    500
09:50 | 54.93 200
10:00 | 55.23 400
```

Group symbols by closing price:
```q
q)daily:([]date:2026.01.02;sym:`AIG`DOW`GOOG`PEP`AAPL`HPQ`ORCL;close:26.1 28.5 27 29.9 33.2 45.5 47.1)
q)select sym by 5 xbar close from daily where date=last date
close| sym
-----| -----------------
25   | `AIG`DOW`GOOG`PEP
30   | ,`AAPL
45   | `HPQ`ORCL
```

> **Tip — You can use `bin` to group at irregular intervals.**
>
>
> ```q
> q)x:`s#10:00+00:00 00:08 00:13 00:27 00:30 00:36 00:39 00:50
> q)select count i by x x bin time.minute from ([]time:`s#10:00:00+asc 100?3600)
> minute| x
> ------| --
> 10:00 | 8
> 10:08 | 13
> 10:13 | 24
> 10:27 | 4
> 10:30 | 9
> 10:36 | 3
> 10:39 | 19
> 10:50 | 20
> ```
>
A month is (internally) the count of months since 2000, so you can use `3 xbar` to calculate quarters.
```q
q)`date$3 xbar `month$2019.11.19       / beginning of a quarter
2019.10.01
q)`date$3+3 xbar `month$2019.11.19     / beginning of next quarter
2020.01.01
q)-1+`date$3+3 xbar `month$2019.11.19  / end of that quarter
2019.12.31
```

> **Warning — A timespan `x` is converted to a count of nanoseconds**
>
>
> Use a timespan `x` only with a timestamp or timespan `y`. For other temporal types, use a number in the units of `y`, such as `5 xbar` for a 5-minute bar on minutes, or `300 xbar` for a 5-minute bar on seconds.
>
> ```q
> q)0D00:05 xbar 2026.01.01D10:07
> 2026.01.01D10:05:00.000000000
> q)0D00:05 xbar 10:07:00.000             / 5 minutes as nanoseconds, applied to milliseconds
> 00:00:00.000
> q)300000 xbar 10:07:00.000
> 10:05:00.000
> ```
>
## Implicit iteration

`xbar` is an [atomic function](atomic.md).
It applies to [dictionaries and keyed tables](math.md#dictionaries-and-tables).
```q
q)(3;4 5)xbar(10;20 -30)
9
20 -30

q)kt:`k xkey update k:`abc`def`ghi from t:flip d:`a`b!(10 -21 3;4 5 -6)

q)3 xbar d
a| 9 -21 3
b| 3 3   -6

q)3 xbar kt
k  | a   b
---| ------
abc| 9   3
def| -21 3
ghi| 3   -6
```

## Domain and range

The following table shows the result type for each combination of the type of `x` (rows) and the type of `y` (columns).
For the character codes of the datatypes, refer to [Datatypes](datatypes.md).
```txt
xbar| b g x h i j e f c s p m d z n u v t
----| -----------------------------------
b   | i . i i i j f f i . p m d z n u v t
g   | . . . . . . . . . . . . . . . . . .
x   | i . i i i j f f i . p m d z n u v t
h   | i . i i i j f f i . p m d z n u v t
i   | i . i i i j f f i . p m d z n u v t
j   | j . j j j j f f j . p m d z n u v t
e   | e . e e e e f f e . p m d z n u v t
f   | f . f f f f f f f . f f f f f f f f
c   | . . . . . . f f . . p m d z n u v t
s   | . . . . . . . . . . . . . . . . . .
p   | p . p p p p f f p . . . . . . . . .
m   | m . m m m m f f m . . . . . . . . .
d   | d . d d d d z z d . . . . . . . . .
z   | z . z z z z z z z . . . . . . . . .
n   | j . j j j j f f j . p m d z n u v t
u   | u . u u u u f f u . . . . . . . . .
v   | v . v v v v f f v . . . . . . . . .
t   | t . t t t t f f t . . . . . . . . .
```

For example, rounding down timespans to the nearest multiple of a long produces a timespan.
```q
q)2 xbar 00:00:00.000000001 00:00:00.000000002 00:00:00.000000013
0D00:00:00.000000000 0D00:00:00.000000002 0D00:00:00.000000012
q)type 2 xbar 00:00:00.000000001 00:00:00.000000002 00:00:00.000000013
16h
```

The possible output types are `ijfpmdznuvte`.

----

[`bin`](bin.md), [`floor`](floor.md)


[Mathematics](math.md)
