Introduction
Part 3 of the hledger tags tutorial shows one way to use tags to track expenses for trips in such a way that you can see:
· Total cost for each individual trip
· Cost for trips by the year in which the trip took place (even if, for example, you purchased airline tickets in December for a trip in January)
· Total yearly expenses for travel based on the year of the travel rather than when the expenses occurred (as in the example above)
· Total expenses for all trips to a specific location, either for one or multiple years
This portion of the tutorial is based on a need I had to track travel expenses, one of my budget items. First, I used the expense category travel for my travel expenses (gas, hotel, restaurant, airline tickets, etc.). That is, if I am on a trip and eat at a restaurant, I categorize it as a travel expense. On the other hand, if I am not travelling and eat at a restaurant (not as part of a trip, but because I decided to eat out), I categorize it as dinning out. This approach won’t necessarily work for all situations, but I have found that it works well for me.
Part 1 and part 2 of the hledger tags tutorials are posted in this forum, and you may find it useful to read them before tackling part 3.
Tags and Values for Trip Tracking
In the examples we will be working with we will have one tag, “trip,” and the values will be in the form of DESTINATION-YEAR[one letter trip identifier]. For example, if one of your trips is to Atlanta, you traveled in 2017, and it was the first trip to Atlanta in 2017, the value of the tag would be:
Atlanta-2017a
The year is the year of the trip, not necessarily when the year the money was spent.
Therefore, a sample expense for the Atlanta trip could look like the following:
2017/01/12 My Gas Station ; trip: Atlanta-2017a
Expenses:Travel $18.43
Liabilities:CreditCard
How Much Did It Cost to Go Where?
To see the total expenses sorted by each tag value, you use the --pivot option with the balance command. Note, though, the advice to limit the report to expenses, as follows:
hledger filename.hledger balance Expenses --pivot trip
If your tags are 100% limited to your expenses, that is, they all have the tag in the Expenses line as shown below:
2018/01/12 My Gas Station
Expenses:Travel $14.43 ; trip: Boise-2018a
Liabilities:CreditCard
Then with the above, you can shorten the command to:
hledger filename.hledger balance Expenses --pivot trip
However, if the tag is applied to the whole transaction:
2018/01/12 My Gas Station ; trip: Boise-2018a
Expenses:Travel $14.43
Liabilities:CreditCard
Then for the above you need to use the command limiting the balance report to Expenses. Otherwise, the liability will balance the expense to zero.
Some Examples
Let’s add some sample data to work with, which will be in a file named TagsPart3.hledger:
2013/01/12 My Gas Station ; trip: Boise-2013a
Expenses:Travel $14.43
Liabilities:CreditCard
2014/01/12 My Gas Station ; trip: Albany-2014a
Expenses:Travel $15.43
Liabilities:CreditCard
2014/09/15 My Gas Station ; trip: Albany-2014b
Expenses:Travel $16.43
Liabilities:CreditCard
2016/01/12 My Gas Station ; trip: Austin-2016a
Expenses:Travel $17.43
Liabilities:CreditCard
2016/05/18 My Gas Station ; trip: Fort Lauderdale-2016a
Expenses:Travel $18.43
Liabilities:CreditCard
2016/11/21 My Gas Station ; trip: Houston-2016a
Expenses:Travel $19.43
Liabilities:CreditCard
Your Report is My Command
If we run the command:
hledger -f TagsPart3.hledger balance Expenses --pivot trip
Then we get:
$15.43 Albany-2014a
$16.43 Albany-2014b
$17.43 Austin-2016a
$14.43 Boise-2013a
$18.43 Fort Lauderdale-2016a
$19.43 Houston-2016a
--------------------
$101.58
To see totals for all trips in 2014, we run the command:
hledger -f TagsPart3.hledger balance Expenses --pivot trip tag:trip=2014
Output:
$15.43 Albany-2014a
$16.43 Albany-2014b
--------------------
$31.86
Similarly, to see totals for all trips in 2016:
hledger -f TagsPart3.hledger balance Expenses --pivot trip tag:trip=2016
Output:
$17.43 Austin-2016a
$18.43 Fort Lauderdale-2016a
$19.43 Houston-2016a
--------------------
$55.29
Finally, how much did we spend on trips to Albany, no matter what year the trip(s) took place in?
hledger -f TagsPart3.hledger balance Expenses --pivot trip tag:trip=Albany
Output:
$15.43 Albany-2014a
$16.43 Albany-2014b
--------------------
$31.86
Let’s Get Real
The above examples were highly simplified, with just one expense per trip. Just to show that we can work with something more complicated, let’s create some new data, assumed to be in a file TagsPart3b.hledger:
2013/12/01 Ariline ; trip: Boise-2014a
Expenses:Travel $200.00 ;round trip tickets
Liabilities:CreditCard
2014/01/10 ACME Rideshare ; trip: Boise 2014a
Expenses:Travel $30.00 ; to airport
Liabilities:CreditCard
2014/01/12 Hotel ; trip: Boise-2014a
Expenses:Travel $300.00
Liabilities:CreditCard
2014/01/12 ACME Rideshare ; trip: Boise 2014a
Expenses:Travel $16.43 ; airport to home
Liabilities:CreditCard
2014/09/12 My Gas Station ; trip: Austin-2014a
Expenses:Travel $17.43
Liabilities:CreditCard
2014/10/18 My Gas Station ; trip: Austin-2014b
Expenses:Travel $18.43
Liabilities:CreditCard
2014/12/01 Airline ; trip:Boise-2015a
Expenses:Travel $212.00 ; round trip tickets
Liabilities:CreditCard
In the above, we purchase airline tickets in 2013 for travel to Boise in 2014. We also take two drives to Austin in 2014. Finally, in December of 2014 we buy tickets for travel in 2015.
How much did each trip cost?
hledger -f TagsPart3b.hledger balance Expenses --pivot trip
Output:
$17.43 Austin-2014a
$18.43 Austin-2014b
$546.43 Boise-2014a
$212.00 Boise-2015a
--------------------
$794.29
How much did the 2014 trips cost?
hledger -f TagsPart3b.hledger balance Expenses --pivot trip tag:trip=2014
Output:
$17.43 Austin-2014a
$18.43 Austin-2014b
$546.43 Boise-2014a
--------------------
$582.29
Conclusion
Tag, you’re it.
--
You received this message because you are subscribed to the Google Groups "hledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hledger+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.