beancount / fava

Fava - web interface for Beancount
https://beancount.github.io/fava/
MIT License
1.95k stars 286 forks source link

Accounts that should be filtered out still show up in graph and values #1423

Open jcornaz opened 2 years ago

jcornaz commented 2 years ago

Given the following beancount file:

option "operating_currency" "USD"

2000-01-01 open Assets:A:B:C
2000-01-01 open Assets:A:B:D
2000-01-01 open Assets:A:E
2000-01-01 open Equity:Opening-Balances

2000-01-01 * "1"
    Assets:A:B:C                        1 USD
    Assets:A:B:D                        1 USD
    Assets:A:E                          1 USD
    Equity:Opening-Balances

When filtering by the account Assets:A:B, fava also shows the account Assets:A:E that should be filtered out as it is not a sub-account of Assets:A:B.

Screenshot from 2022-05-16 14-31-08

Fava version: 1.21

rnd256 commented 2 years ago

This is something that was really confusing to me too. I would expect Fava to do one of the following:

  1. Generate the unfiltered report, then remove any results that are not about account(s) that you're filtering down to, or
  2. Filter out any posting/legs that don't include the account(s) you're filtering down to, then generate the report.

But instead, it does:

  1. Filter out any transactions that don't have any postings/legs that include the account(s) you're filtering down to, then generate the report.

It would be really helpful to have support for methods 1 or 2 above, since I think that's what most users would expect/need when filtering reports.

jcornaz commented 2 years ago

@yagebu, why the docs label?

We are asking for a change of behavior here. To me the current behavior is so surprising that I would qualify this issue as "Bug", though I can understand if it is qualified as an "enhancement" in case that behavior was intentional.

Rudd-O commented 2 years ago

I too think the current behavior is a bug. I want to see the accounts I selected. That would be my "this is logical" expectation.

yagebu commented 1 year ago

The current behaviour is intentional. Only allowing transactions that balance is one of the core invariants of Beancount. So filtering on complete transactions is the only way to obtain a subset of all entries that behaves consistently in all reports. In the Beancount query language, these two levels of filtering are represented by FROM (complete entries, which works like the filters in Fava) and WHERE (postings) (see also https://beancount.github.io/docs/beancount_query_language.html#transactions-and-postings). So when writing a custom BQL query, you can apply both levels of filters as you wish, but some of the shortcuts like balances or journal only support FROM expressions. (@rnd256 described a further filter level above - a filter over the generated report: 1.; the others correspond as follows: FROM: 3. WHERE: 2.)

Both levels of filtering can be useful but only the whole transaction filtering can work well across all reports. In particular for reports with a somewhat well-defined meaning like a balance sheet, IMHO core invariants like the balance sheet equation should not be violated and for that reason the "global" filters should behave like FROM.

I've personally found the FROM-like filters to be very useful, as it easily allows me to answer questions like "how much did I spend on food with which card" (filtering to Expenses:Food). So I've often found the "extra" accounts that are still there after the filtering to be exactly the information I want and was never really bothered by them.

We could add WHERE-like filters to Fava, but I believe these should not apply to the balance sheet and income statement and maybe be new separate reports. For the case of filtering to a single account (which seems to be the example given here) this already exists: The account report for that account.

rnd256 commented 1 year ago

Thanks for your response, @yagebu. After spending more time on this, I think report filtering is really what we're after here. ie. Generate an entire balance sheet with no filtering, then filter the results based on account. That would effectively give people a view similar to Fava's account report view, but they'd be able to include any number of accounts in the view.

That's useful for cases where the aggregation levels/combinations you're interested in can't be perfectly reflected in your account hierarchy. Eg. If your hierarchy goes AssetClass:Person:Account, where AssetClass can be "Investments" or "Cash", Person can be "rnd256" or "wife". I want to be able to see reports for all "Investments" and reports for all "rnd256".

rnd256 commented 1 year ago

FYI for folks wanting to accomplish this, I've been using the command line: bean-query main.beancount "SELECT date, narration, position, balance WHERE account = 'Assets:MyAccount'" | less