Reducing repetition/duplication of amount value in entries

76 views
Skip to first unread message

Viraj Alankar

unread,
Jun 8, 2023, 3:14:39 AM6/8/23
to Ledger
Hello,

I was wondering if there is a way to reduce the duplication of the amount value in an entry such as the following:

2023/06/06 McDonalds
    Expenses:Food:Restaurants                           $23.98
    Liabilities:Apple Card                             -$23.98
    Income:Credit Card Rewards  (-$23.98 * 0.02)
    Assets:Apple Cash

i.e. can I replace 23.98 with some string? I see there is some way to do this with automated transactions, but perhaps not with regular ones?

I considered using automated transactions for this, but in that case I would like to override the 0.02 multipler in certain cases (some rewards are different percentages). I'm not sure how to do that or whether it is possible.

Thanks,

Viraj.

Alexis

unread,
Jun 8, 2023, 7:53:03 AM6/8/23
to ledge...@googlegroups.com
Hello Viraj,

I know of two options to reduce the duplication of the amount value:

1. Using transaction typed metadata

2023/06/06 McDonalds
; amount_spent:: $23.98
Expenses:Food:Restaurants (tag("amount_spent"))
Liabilities:Apple Card (-tag("amount_spent"))
Income:Credit Card Rewards (-(tag("amount_spent")) * 0.02)
Assets:Apple Cash

See https://ledger-cli.org/doc/ledger3.html#Typed-metadata


2. Using a define

define spent_amount=$23.98
2023/06/06 McDonalds
Expenses:Food:Restaurants (spent_amount)
Liabilities:Apple Card (-spent_amount)
Income:Credit Card Rewards (-(spent_amount) * 0.02)
Assets:Apple Cash

See https://ledger-cli.org/doc/ledger3.html#index-define


Hope this helps,
Alexis

Viraj Alankar

unread,
Jun 8, 2023, 8:03:45 AM6/8/23
to ledge...@googlegroups.com
Ah, these are great. Thanks!

--

---
You received this message because you are subscribed to a topic in the Google Groups "Ledger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ledger-cli/tqXZGjJbZSo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ledger-cli+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ledger-cli/ty4szlg6plx7c4ty4ixuf6gohf4hyx4yjqnpct4xeetpkj5rcs%40fgfvyi2zrlm5.

Yuri Khan

unread,
Jun 8, 2023, 9:19:09 AM6/8/23
to ledge...@googlegroups.com
On Thu, 8 Jun 2023 at 18:53, Alexis <surr...@gmail.com> wrote:

> I know of two options to reduce the duplication of the amount value:>
> 1. Using transaction typed metadata
>
> 2023/06/06 McDonalds
> ; amount_spent:: $23.98
> Expenses:Food:Restaurants (tag("amount_spent"))
> Liabilities:Apple Card (-tag("amount_spent"))
> Income:Credit Card Rewards (-(tag("amount_spent")) * 0.02)
> Assets:Apple Cash
>
> See https://ledger-cli.org/doc/ledger3.html#Typed-metadata

One might try to combine typed metadata with automated transactions:

= Liabilities:Apple Card
Assets:Apple Cash (-tag("cashback"))
Income:Credit Card Rewards (tag("cashback"))

2023-06-06 McDonalds
; cashback:: 0.02
Expenses:Food:Restaurants $23.98
Liabilities:Apple Card

$ ledger bal -f test_cashback.ledger
$0.48 Assets:Apple Cash
$23.98 Expenses:Food:Restaurants
$-0.48 Income:Credit Card Rewards
$-23.98 Liabilities:Apple Card
--------------------
0

I do not know off the bat how to apply a default 0.02 value if the tag
is not present, but that is probably possible.

Viraj Alankar

unread,
Jun 8, 2023, 10:28:33 AM6/8/23
to ledge...@googlegroups.com
Nice, that is even cleaner. One thing however is if I then later add a transaction to pay the card, the automatic transaction still applies.

What would be right way to do apply the automatic transaction only when there is also an Expenses entry? I could not figure out the proper syntax. I tried something like:

= expr "any(account =~ /^Liabilities:Apple Card/) and any(account =~ /^Expenses/)"

but this does not seem to work. I get some errors if I try removing the quotes, but I'm likely doing it wrong.

Thanks,

Viraj.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Ledger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ledger-cli/tqXZGjJbZSo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ledger-cli+...@googlegroups.com.

Yuri Khan

unread,
Jun 8, 2023, 10:55:19 AM6/8/23
to ledge...@googlegroups.com
On Thu, 8 Jun 2023 at 21:28, Viraj Alankar <vala...@gmail.com> wrote:

> What would be right way to do apply the automatic transaction only when there is also an Expenses entry? I could not figure out the proper syntax. I tried something like:
>
> = expr "any(account =~ /^Liabilities:Apple Card/) and any(account =~ /^Expenses/)"
>
> but this does not seem to work. I get some errors if I try removing the quotes, but I'm likely doing it wrong.

You might instead apply cashback only where the amount is negative.
But I’m struggling with complex condition syntax too, so maybe someone
else will chime in.

Viraj Alankar

unread,
Jun 9, 2023, 9:27:43 AM6/9/23
to ledge...@googlegroups.com
I found one way that I think should work:

= "Liabilities:Apple Card" and %cashback

    Assets:Apple Cash           (-tag("cashback"))
    Income:Credit Card Rewards   (tag("cashback"))

2023/06/06 McDonalds

    ; cashback:: 0.02
    Expenses:Food:Restaurants                           $23.98
    Liabilities:Apple Card
2023/06/07 Transfer
    Liabilities:Apple Card                              $23.98
    Assets:Some bank


Mainly found by searching bugs. The manual would really benefit with some more realistic examples in the Automated Transactions section.

Thanks,

Viraj.


--

---
You received this message because you are subscribed to a topic in the Google Groups "Ledger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ledger-cli/tqXZGjJbZSo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ledger-cli+...@googlegroups.com.

John Wiegley

unread,
Jun 9, 2023, 3:14:24 PM6/9/23
to Viraj Alankar, ledge...@googlegroups.com
>>>>> "VA" == Viraj Alankar <vala...@gmail.com> writes:

VA> I found one way that I think should work:
VA> = "Liabilities:Apple Card" and %cashback
VA> Assets:Apple Cash (-tag("cashback"))
VA> Income:Credit Card Rewards (tag("cashback"))

VA> 2023/06/06 McDonalds
VA> ; cashback:: 0.02
VA> Expenses:Food:Restaurants $23.98
VA> Liabilities:Apple Card
VA> 2023/06/07 Transfer
VA> Liabilities:Apple Card $23.98
VA> Assets:Some bank

VA> Mainly found by searching bugs. The manual would really benefit with some
VA> more realistic examples in the Automated Transactions section.

Interesting example! Please feel free to submit it as an addition to the docs,
if you’d be willing.

John

Viraj Alankar

unread,
Jun 10, 2023, 4:10:49 AM6/10/23
to ledge...@googlegroups.com
Sure, I'll make a pull request.

Something I was a bit confused about. If I change the automated transaction to:

= "Liabilities:Apple Card" and has_tag("cashback")

It doesn't seem to work. There is no error, it just doesn't apply the transaction. Do you happen to know the right way to use has_tag() here?

Thanks,

Viraj.




John Wiegley

unread,
Jun 10, 2023, 3:36:07 PM6/10/23
to Viraj Alankar, ledge...@googlegroups.com
>>>>> "VA" == Viraj Alankar <vala...@gmail.com> writes:

VA> Something I was a bit confused about. If I change the automated
VA> transaction to:

VA> = "Liabilities:Apple Card" and has_tag("cashback")

This is a bug in the query parser, which you can see by running:

ledger query '"Liabilities:Apple Card" and has_tag("cashback")'

It seems that %cashback also has a bug, but it’s harmless and so you don’t
notice the problem.

The query parser needs an overhaul.

John

Viraj Alankar

unread,
Jun 18, 2023, 3:13:48 AM6/18/23
to ledge...@googlegroups.com
Just a followup, there is now an example in the manual:


Thanks to my pull request reviewer Alexis for helping clean it up.
Reply all
Reply to author
Forward
0 new messages