Beanie Budgets

Beancount query language

Beancount's query language, BQL, asks a ledger questions in a form close to SQL: what was spent on groceries each month, what a holiday cost, what every account held at the end of a year. Below are working examples and a reference to the language.

The same language runs in bean-query, in Fava's Query page and in Query in Beanie Budgets, which is part of Pro. Every example on this page was run through bean-query under beancount 2.3.6 and 3.2.3, over example-ledger.bean, before the page was built. Save that file into an empty folder to try them.

The reference below is also built into Query in the app. For the language's full definition, beancount's own documentation is the place to start.

On this page:

What Query shows

Beanie Budgets shows each answer as a table, and as a chart where one fits: a line for a balance over time, columns for totals by month, stacked bars for several accounts over time, and bars, a donut or a treemap for totals by name. The screens below use the app's demo ledger.

Examples

In Beanie Budgets, the dice button in Query picks one of these at random, using the accounts, payees and tags in your own ledger.

Every posting

SELECT date, account, position
LIMIT 20

Balances

BALANCES

One account's journal

JOURNAL 'Assets:Bank:Current'

Spending by category

SELECT account, sum(position)
WHERE account ~ '^Expenses'
GROUP BY account
ORDER BY account

The biggest spends

SELECT date, payee, narration, position
WHERE account ~ '^Expenses'
ORDER BY number DESC
LIMIT 10

Spending month by month

SELECT year, month, sum(position)
WHERE account ~ '^Expenses'
GROUP BY year, month
ORDER BY year, month

Everything at one payee

SELECT date, narration, account, position
WHERE payee ~ 'Vanguard'
ORDER BY date

Payees by how often

SELECT payee, count(*) AS visits, sum(number) AS spent
WHERE account ~ '^Expenses'
GROUP BY payee
ORDER BY visits DESC

One category through a year

SELECT month, sum(position)
WHERE account ~ 'Expenses:Food:Groceries' AND year = 2026
GROUP BY month
ORDER BY month

Spending by group

SELECT root(account, 2) AS category, sum(number) AS total
WHERE account ~ '^Expenses'
GROUP BY category
ORDER BY total DESC

Tagged transactions

SELECT date, narration, tags
WHERE length(tags) > 0
ORDER BY date

Every payee, once

SELECT DISTINCT payee
ORDER BY payee

A running balance

SELECT date, narration, position, balance
WHERE account ~ 'Assets:Bank:Current'

Income month by month

SELECT year, month, neg(sum(number)) AS income
WHERE account ~ '^Income'
GROUP BY year, month
ORDER BY year, month

Each account's first and last

SELECT account, first(date), last(date), count(*)
GROUP BY account
ORDER BY account

Since the start of the year

SELECT date, account, position
WHERE date >= 2026-01-01 AND account ~ '^Expenses'
ORDER BY date
LIMIT 20

Leaving something out

SELECT account, sum(number)
WHERE account ~ '^Expenses' AND NOT account ~ 'Groceries'
GROUP BY account
ORDER BY account

An average

SELECT account, sum(number) / count(*) AS average
WHERE account ~ '^Expenses'
GROUP BY account
ORDER BY average DESC

Postings by flag

SELECT flag, count(*)
GROUP BY flag
ORDER BY flag

Accounts by their last name

SELECT leaf(account) AS name, sum(number) AS total
WHERE account ~ '^Expenses'
GROUP BY name
ORDER BY name

Spending trend

SELECT date_trunc('month', date) AS month, sum(position)
WHERE account ~ '^Expenses'
GROUP BY month
ORDER BY month

Saved each month

SELECT year, month, neg(sum(position)) AS saved
WHERE account ~ '^(Income|Expenses)'
GROUP BY year, month
ORDER BY year, month

Net worth at each month's end

SELECT year, month, value(last(balance), last(date)) AS net_worth
WHERE account ~ '^(Assets|Liabilities)'
GROUP BY year, month
ORDER BY year, month

What a tag cost

SELECT account, sum(position)
WHERE 'lisbon-2026' IN tags
GROUP BY account
ORDER BY account

Spending by weekday

SELECT weekday(date) AS day, sum(position)
WHERE account ~ '^Expenses'
GROUP BY day
ORDER BY day

The biggest payees in a year

SELECT payee, sum(number) AS spent
WHERE account ~ '^Expenses' AND year = 2026
GROUP BY payee
ORDER BY spent DESC
LIMIT 10

Holdings by commodity

SELECT currency, sum(units(position)) AS units
WHERE account ~ '^Assets'
GROUP BY currency
ORDER BY currency

Spending by quarter

SELECT quarter(date) AS quarter, sum(position)
WHERE account ~ '^Expenses'
GROUP BY quarter
ORDER BY quarter

Each asset over time

SELECT yearmonth(date) AS month, root(account, 3) AS holding, sum(position)
WHERE account ~ '^Assets'
GROUP BY month, holding
ORDER BY month

Each category month by month

SELECT year, month, root(account, 2) AS category, sum(position)
WHERE account ~ '^Expenses'
GROUP BY year, month, category
ORDER BY year, month

Statements

WrittenWhat it does
SELECT …Columns or expressions, one row a posting unless grouped. SELECT DISTINCT leaves out repeated rows.
BALANCESEvery account's balance. BALANCES AT cost, or AT units, value or another function, changes what is summed.
JOURNAL 'Assets:Bank'One account's postings with a running balance. Takes AT as BALANCES does.
PRINTThe entries as beancount writes them. Takes FROM and WHERE.

Clauses

WrittenWhat it does
FROM #accountsAnother table than postings. See Tables below.
FROM year = 2026Only the entries the expression picks, before anything else.
OPEN ON 2026-01-01After FROM. Earlier entries summed into an opening balance on that date.
CLOSE ON 2027-01-01After FROM. Entries from that date on left out.
CLEARAfter FROM. Income and expenses moved to equity, as at a year's end.
WHEREWhich rows count, as account ~ '^Expenses' AND year = 2026.
GROUP BY accountOne row for each value, with sum, count and the other aggregates totalling the rest.
HAVING sum(number) > 100After GROUP BY. Which groups to keep.
ORDER BY date DESCSorted by these, ASC (the default) or DESC.
LIMIT 10The first rows only.
PIVOT BY account, yearThe second column's values across the top, as a table.
ASA name for a column, as sum(number) AS total, which ORDER BY and GROUP BY can use.

Comparisons

WrittenWhat it does
~Matches a pattern, ignoring case: account ~ 'Groceries'. ^ is the start, so '^Expenses:Food' is Food and everything under it.
!~Doesn't match a pattern.
?~Matches a pattern, minding case.
= != < <= > >=Equal, not equal, before or after, less or more.
INIn a set or list: 'holiday' IN tags, or account IN ('Assets:Bank', 'Assets:Cash').
NOT INNot in a set or list.
AND OR NOTBoth, either, the opposite.
IS NULLHas no value, as payee IS NULL. IS NOT NULL for one that has.
BETWEENdate BETWEEN 2026-01-01 AND 2026-03-31, both ends included.
+ - * / %Arithmetic, on numbers and amounts.
meta['key']One value of a set of metadata, as entry.meta['receipt'].

Aggregates

WrittenWhat it does
sum(position)The total of a group. sum(number) for a plain number, sum(position) to keep each currency.
count(*)How many rows a group has.
first(date)The first value in a group.
last(balance)The last value in a group.
min(number)The smallest value in a group.
max(number)The largest value in a group.

Postings Columns

WrittenWhat it does
typeThe entry's kind, transaction for a posting.
idAn identifier for the transaction.
dateThe transaction's date.
yearThe year of its date.
monthThe month of its date, 1 to 12.
dayThe day of its date.
filenameThe file it is written in.
linenoThe line it is on.
locationFile and line together.
flag* for complete, ! for pending, # for a forecast.
payeeWho was paid or paid you.
narrationWhat it was for.
descriptionPayee and narration together.
tagsIts tags, as a set: 'holiday' IN tags.
linksIts links, as a set.
posting_flagA flag on this posting alone.
accountThe posting's account.
other_accountsThe transaction's other accounts, as a set.
numberThe posting's amount, as a plain number.
currencyThe posting's currency or commodity.
cost_numberIts cost per unit, when held at cost.
cost_currencyIts cost's currency.
cost_dateIts cost's date.
cost_labelIts cost's label.
positionThe amount with its currency, and its cost when held at cost.
priceIts @ price.
weightWhat it counts as towards the transaction's balance: its cost or price where it has one.
balanceThe running balance of the postings so far.
metaThe posting's metadata. meta['key'] for one value.
entryThe whole transaction, as entry.payee or entry.meta['key'].
accountsEvery account in the transaction, as a set.

Tables

WrittenWhat it does
FROM #transactionsOne row a transaction. Columns: meta, date, flag, payee, narration, tags, links, accounts.
FROM #entriesOne row an entry of any kind: transactions, opens, balances, notes and the rest. Columns: id, type, filename, lineno, date, year, month, day, flag, payee, narration, description, tags, links, meta, accounts.
FROM #accountsOne row an account, with the dates it opened and closed. Columns: account, open, close.
FROM #commoditiesOne row a commodity line. Columns: meta, date, name.
FROM #pricesOne row a price line. Columns: meta, date, currency, amount.
FROM #balancesOne row a balance assertion. Columns: meta, date, account, amount, tolerance, discrepancy.
FROM #notesOne row a note. Columns: meta, date, account, comment.
FROM #eventsOne row an event. Columns: meta, date, type, description.
FROM #documentsOne row a document line. Columns: meta, date, account, filename, tags, links.

Functions

WrittenWhat it does
abs(number)A number, amount or balance without its sign.
account_sortkey(account)A key that sorts accounts as the ledger orders them: Assets, Liabilities, Equity, Income, Expenses.
any_meta('key')The posting's metadata value, or the transaction's when the posting has none.
bool(x)Whether a value counts as true.
close_date(account)The date an account was closed, or nothing.
commodity(position)The same as currency.
commodity_meta(currency, 'key')Metadata on a commodity's line.
convert(position, 'GBP', date)An amount converted to a currency at the ledger's prices, today's or on the date given.
cost(position)What was paid for a holding, from its cost.
currency(position)An amount's currency or commodity.
currency_meta(currency, 'key')The same as commodity_meta.
date(2026, 1, 31)A date from its year, month and day, or from text, as date('2026-01-31').
date_add(date, 30)A date that many days later, or earlier for a negative number.
date_bin('2 weeks', date, 2026-01-05)A date taken back to the start of its run of a set length, counted from the last date.
date_diff(a, b)The days from b to a.
date_part('quarter', date)One part of a date: year, month, quarter, week, weekday, isoyear, decade and more.
date_trunc('month', date)A date taken back to the start of its year, quarter, month or week.
day(date)A date's day of the month.
decimal('12.50')A number from text.
empty(balance)Whether a balance holds nothing.
entry_meta('key')The transaction's metadata value.
filter_currency(balance, 'GBP')A balance or amount kept only in one currency.
findfirst('^holiday', tags)The first in a set or list that matches a pattern.
getitem(meta, 'key', 'none')One value of a set of metadata, or the third when there is none.
getprice('VWRL', 'GBP', date)One commodity's price in another, from the ledger's price lines.
grep('[0-9]+', narration)The part of text matching a pattern.
grepn('Ref ([0-9]+)', narration, 1)One bracketed part of a pattern's match.
has_account('Expenses:Holiday')Whether any of the transaction's postings has an account matching the pattern.
int(number)A whole number, cutting off any fraction.
interval('3 months')A length of time, for date_bin.
joinstr(tags)A set or list as text, with commas between.
leaf(account)An account's last part: Groceries for Expenses:Food:Groceries.
length(tags)The length of text, or how many are in a set or list.
lower(payee)Text in lower case.
maxwidth(narration, 20)Text shortened to that many characters.
meta('key')The posting's metadata value.
month(date)A date's month, 1 to 12.
neg(sum(number))The opposite sign: income reads positive as neg(sum(position)).
number(position)An amount's number, without its currency.
only('GBP', balance)The part of a balance in one currency.
open_date(account)The date an account was opened.
open_meta(account, 'key')Metadata on an account's open line.
parent(account)The account above: Expenses:Food for Expenses:Food:Groceries.
parse_date('31/01/2026', '%d/%m/%Y')A date from text, in the format given, or as 2026-01-31 without one.
possign(number, account)A number with liabilities, equity and income turned positive, as their balances usually read.
quarter(date)A date's quarter, as 2026-Q1.
repr(payee)A value as beanquery writes it, quotes included.
root(account, 2)An account's first parts: Expenses:Food for Expenses:Food:Groceries.
round(number, 2)A number rounded to that many decimal places, or a whole number.
safediv(a, b)a divided by b, or 0 where b is 0.
splitcomp(account, ':', 1)One part of text split at a separator, counting from 0.
str(number)Any value as text.
subst('Ltd', '', payee)Text with every match of a pattern replaced.
substr(narration, 0, 10)The part of text from the first position up to the second, counting from 0.
today()Today's date.
units(position)The units held, without their cost.
upper(payee)Text in upper case.
value(position, date)A holding at market value, from the ledger's price lines, today's or on the date given.
weekday(date)A date's day of the week, Mon to Sun.
year(date)A date's year.
yearmonth(date)The first of a date's month.