simonmichael / hledger

Robust, fast, intuitive plain text accounting tool with CLI, TUI and web interfaces.
https://hledger.org
GNU General Public License v3.0
2.97k stars 317 forks source link

--average-ignore-last flag #1463

Open aragaer opened 3 years ago

aragaer commented 3 years ago

Sometimes I want to see an average of some regular expenses and expenses for the current period together. However if current period is not yet finished the result sometimes becomes incorrect.

Example:

2020-11-25
 expenses:monthly  60
 assets

2020-12-25
 expenses:monthly  40
 assets

2021-01-20
 expenses:one-time  10
 assets

I'd like to see that my "monthly" expenses are 50 on average, but 0 for current month. Instead I see the following:

$ hledger -f avg.journal bal expenses -A -M
Balance changes in 2020-11-01..2021-01-31:

                   || 2020-11  2020-12  2021-01  Average 
===================++====================================
 expenses:monthly  ||      60       40        0       33 
 expenses:one-time ||       0        0       10        3 
-------------------++------------------------------------
                   ||      60       40       10       37 

Without that one-time entry January would be eluded from the report and average would be shown correctly.

It would be nice to have a way to calculate average without last incomplete period.

simonmichael commented 3 years ago

I don't think this entirely makes sense yet. Is it assuming that subperiods can start on other boundaries, eg 15th to 15th ?

aragaer commented 3 years ago

We have all periods and we have the "report end" date. I am not saying that ignoring the last incomplete period should be the default behavior of -A flag, just some way of intentionally ignoring the last subperiod.

Xitian9 commented 3 years ago

I encounter this issue too, but I'm not sure a new flag is worth it. The same can be accomplished by manually setting the report end date to the end of the last complete period: in your case

$ hledger -f avg.journal bal expenses -A -M e 2021
aragaer commented 3 years ago

Yes, but in that case I'll need two reports for "history, average and current amount". I can do --transpose for both, keep only one line from the second, get it all into emacs org-mode, then "transpose back" combined table using elisp... Except there's no --transpose for incomestatement. Basically too much external processing for something that could be done internally.

Xitian9 commented 3 years ago

One problem is that it's tricky to distinguish between:

There might be some heuristics we could apply to distinguish these, e.g. having postings after the last day of the period, but this raises more questions that would need to be answered (do we count any posting at all? any posting in an account the report is considering? only cleared postings?).

Sometimes I put future postings in my journal which are scheduled, but have not yet occurred. I then fill in other postings as they actually happen. How would we tell which is the last incomplete period here?

aragaer commented 3 years ago

The report I'm having problems with looks like this:

hledger is -M -b $(date +%Y-%m -d "-12 month") -e nextmonth -NA

In this exact case I do have -e nextmonth specified so the last month is a finished period. If I had -e today then last month would be unfinished.

However --average-ignore-last might be something more straightforward -- just ignore the last period, finished or not.

Xitian9 commented 3 years ago

Maybe something like that would work, but I think even that is not guaranteed. My credit card can take up to two weeks for transactions to clear, so we could be well into the next month with the previous month still unfinished.

Perhaps the thing to say is that any way we would implement this would have to have a very big caveat utilitor attached.

simonmichael commented 3 years ago

I haven't understood the exact problem here or the exact proposed solution/functional specification. Could you make it clearer ?

aragaer commented 3 years ago

Problem: I want to see in a single table the following data:

Currently the last column includes current period in calculations which is not always "finished". Instead I want to see "average for all previous periods".

I can have manual processing of the output of --average -e <beginning-of-current-period> and "everything else". While this is possible I think that it would be easy enough to just add an option for "average of every period but ignoring the last one".

That is for file I included in my initial post I'm expecting the following output:

$ hledger -f avg.journal bal expenses -M --average
Balance changes in 2020-11-01..2021-01-31:

                   || 2020-11  2020-12  2021-01  Average 
===================++====================================
 expenses:monthly  ||      60       40        0       33 
 expenses:one-time ||       0        0       10        3 
-------------------++------------------------------------
                   ||      60       40       10       37 
$ hledger -f avg.journal bal expenses -M --average-ignore-last
Balance changes in 2020-11-01..2021-01-31:

                   || 2020-11  2020-12  2021-01  Average before 2021-01
===================++====================================
 expenses:monthly  ||      60       40        0       50 
 expenses:one-time ||       0        0       10        0 
-------------------++------------------------------------
                   ||      60       40       10       50 
simonmichael commented 3 years ago

Thanks @aragaer, that helped. I see your desire. It seems a little bit of a specialised report. Right now when I see the Average column I know that it's the average of the other columns. Even if the the last period is still in progress. And if I want the average with the partial period excluded, I need to rerun the report without it. That's easy to understand and explain and seems not too burdensome.

Am I underestimating how frequently people want this report you're describing ?

Re pivot, we should add it to these reports as well.

simonmichael commented 3 years ago

PS making progress on support for custom reports could be another way to solve this.

simonmichael commented 3 years ago

PS you're right that it would be "easy" to add (or so we always think, at least) but PTA and hledger are already complicated and overwhelming so everything we add has a cost..

aragaer commented 3 years ago

Just another somewhat crazy idea. Being an xmonad user I'm used to writing code instead of a configuration. Having something like "hledger reporting api" to declare my own reports would be enough for me (not sure if it will be used by anyone else though). And that might be a way to have some unified internal reporting code which might be more maintainable.

adept commented 3 years ago

It is not exactly xmonad, but I found that writing custom reports with hledger is relatively straightforward. Two more generic of those that I wrote are in the hledger repo, and you can see them in the bin/hledger-combine-balances and bin/hledger-balance-as-budget

On Wed, Jan 20, 2021 at 6:58 AM Ilya Konovalov notifications@github.com wrote:

Just another somewhat crazy idea. Being an xmonad user I'm used to writing code instead of a configuration. Having something like "hledger reporting api" to declare my own reports would be enough for me (not sure if it will be used by anyone else though). And that might be a way to have some unified internal reporting code which might be more maintainable.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/simonmichael/hledger/issues/1463#issuecomment-763383775, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA42KXAK5Z76S3JMTPWUGTS2Z5IZANCNFSM4WEEMR3Q .

-- D. Astapov

simonmichael commented 3 years ago

Agreed! This api has existed for a long time but it will get better only with more use. I usually look at the closest similar addon script or builtin command to see how they produce their reports.

I think it's pretty cool that a script can access the full power of hledger..