Custom script that iterates postings with `open on` and `close on` dates

46 views
Skip to first unread message

Aaron Stacy

unread,
Jan 16, 2021, 4:24:47 PM1/16/21
to Beancount
HI all! I'd like to make a customized income statement where some accounts are added together. For example, I'd like to know all income for the past month, whether it was Income:Hooli:Salary or Income:Hooli:RSU's. I can get pretty close with the query:

Ā  Ā  select account, sum(position) from open on 2020-12-01 close on 2020-12-31 where account ~ "Income" group by account order by account;

But then I need to go through and sum the specific accounts I happen to care about. I'm comfortable importing beancount, using it to parse my ledger, and iterating through expenses, but can someone point me to what I'd do to get the open on/close on part?

Thanks!

Ben Blount

unread,
Jan 16, 2021, 4:51:30 PM1/16/21
to Beancount
Here are some examples showing modifying a journal based on a query: Delete entry | Rename account.
It takes a full beancount SQL query and executes it on a journal. You could use that part and strip out the parts that stage changes to the journal.
You could run the query you've got there and then do some postprocessing to sum up the accounts accordingly.

--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/26067fa0-0a9a-41ec-9b5d-2f62c9714ff0n%40googlegroups.com.

Martin Blais

unread,
Jan 17, 2021, 12:49:41 AM1/17/21
to Beancount
If you're only interested in income just filter the date interval you need.

The "close" operation is essentially there to ensure that the balances of the assets/liabilities accounts are correct when you filter out transactions from the past, i.e., it just inserts a transfer from all of the past's net income to the balance sheet accounts at the head of the filtered transactions.


--

Aaron Stacy

unread,
Jan 17, 2021, 12:18:48 PM1/17/21
to bean...@googlegroups.com
Thanks Martin and Ben!

The following gives me precisely what I need:

Ā  Ā Ā select number(only("USD", sum(position)))
Ā  Ā  from open on 2020-10-01 close on 2020-11-01
Ā  Ā  where
Ā  Ā  Ā  Ā  account ~ "My:Checking" and
Ā  Ā  Ā  Ā  number(units(position)) > 0 and
Ā  Ā  Ā  Ā  findfirst("Income:Hooli", other_accounts) != NULL

EXCEPT I'd like a number in Python instead of text output in the terminal. Cribbing from Ben's examples, I came up with the following to run it from Python:

Ā  Ā  from beancount.query.query_compile import compile
Ā  Ā  from beancount.query.query_env import FilterPostingsEnvironment, FilterEntriesEnvironment, TargetsEnvironment
Ā  Ā  from beancount.query.query_execute import execute_query
Ā  Ā  from beancount.query.query_parser import Parser
Ā  Ā  from beancount.loader import load_file

Ā  Ā  entries, errors, options = load_file('my.beancount')
Ā  Ā  text = '... the query from above ...'
Ā  Ā  ast = Parser().parse(text)
Ā  Ā  compiled = compile(ast, TargetsEnvironment(), FilterPostingsEnvironment(), FilterEntriesEnvironment())
Ā  Ā  columns, results = execute_query(compiled, entries, options)

Thanks again for taking the time to help!

You received this message because you are subscribed to a topic in the Google Groups "Beancount" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beancount/_XEernfER5w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/CAK21%2BhNCseoKOVRPFUgpmKg3UM1VmcKonEDZA_Ck%2BCTrc%2BbhRA%40mail.gmail.com.

Martin Blais

unread,
Jan 17, 2021, 12:25:16 PM1/17/21
to Beancount

Aaron Stacy

unread,
Jan 17, 2021, 12:29:13 PM1/17/21
to bean...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages