# Counting trip expenses

**URL:** <https://community.silverbullet.md/t/counting-trip-expenses/414>\
**Category:** Tricks & Techniques\
**Created:** [April 30, 2024, 10:10pm UTC](https://community.silverbullet.md/t/counting-trip-expenses/414 "2024-04-30T22:10:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maarrk](https://community.silverbullet.md/user_avatar/community.silverbullet.md/maarrk/32/56_2.png) [@Maarrk](https://community.silverbullet.md/u/Maarrk)\
**Post date:** [April 30, 2024, 10:10pm UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/1 "2024-04-30T22:10:25Z")

</div>

Feels a bit silly, but on the other hand Excel or programming are overkill for the task. And this way it’s editable on mobile and kept together with other notes about the trip 🙂

 ![screenshot of the example page](https://community.silverbullet.md/uploads/default/original/1X/c51cf585ffc19e90b767eea41b9d25c0121281ad.png)

The page can have one or more tables, and then I pass a query to a custom function:

````auto
```template
**Total cost: {{sum("cost", {table where page = @page.name})}}€**
transport: {{sum("cost", {table where page = @page.name and tags = 'transport'})}}€
```

|Description|Cost|
|-----------|----|
|train ticket #transport|6.25|
|supermarket|15.12|

````

The `sum` function is defined in this Space Script

```js
silverbullet.registerFunction("sum", (key, items) => {
  return items
    .map(i => parseFloat(i[key]))
    .reduce((acc, n) => acc + (isNaN(n) ? 0 : n), 0)
    .toFixed(2);
});

```

---

<div class="post-metadata">

**Author:** ![zef](https://community.silverbullet.md/user_avatar/community.silverbullet.md/zef/32/7_2.png) [@zef](https://community.silverbullet.md/u/zef)\
**Post date:** [May 1, 2024, 6:24am UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/2 "2024-05-01T06:24:22Z")

</div>

Ah nice use case for the table indexing. Bye bye spreadsheets! 😂

---

<div class="post-metadata">

**Author:** ![paletochen](https://community.silverbullet.md/letter_avatar_proxy/v4/letter/p/cdc98d/32.png) [@paletochen](https://community.silverbullet.md/u/paletochen)\
**Post date:** [May 1, 2024, 10:49am UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/3 "2024-05-01T10:49:05Z")

</div>

I love this. Thanks for sharing!  
Perfect timing as I am reading this feature while being on vacation and sharing costs.

I have been using for 3 trips so far SilverBullet for cost tracking, though through a simple bullet list. No automatic calculations.

This will save me time.  
It would be amazing to enhance it to provide cost-sharing, as I normally create the cost tracking during vacation because I need to share the expenses with other people and at the end of the trip we make numbers.

[UPDATE] I guess I can use tags for it for now 🙂

---

<div class="post-metadata">

**Author:** ![paletochen](https://community.silverbullet.md/letter_avatar_proxy/v4/letter/p/cdc98d/32.png) [@paletochen](https://community.silverbullet.md/u/paletochen)\
**Post date:** [May 1, 2024, 1:18pm UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/4 "2024-05-01T13:18:57Z")

</div>

Well, thanks again for doing this.  
I just added some more formulas and now I have what I need.

Example with splitting expenses between 2 families (1 of 3 people and the other of 2, so we should pay different amounts at the end of the trip)

 ![image](https://community.silverbullet.md/uploads/default/original/1X/f7be7fab93e990d9bc67be7fbf0296458be55a59.png)

---

<div class="post-metadata">

**Author:** ![molnarti](https://community.silverbullet.md/user_avatar/community.silverbullet.md/molnarti/32/645_2.png) [@molnarti](https://community.silverbullet.md/u/molnarti)\
**Post date:** [September 29, 2024, 6:25pm UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/5 "2024-09-29T18:25:42Z")

</div>

the script gave me wrong sums, then after some troubleshooting i realized its not counting lines with empty colums.

as you can see in the image, only the items where link is present are summed. any idea how to fix this?

![image](https://community.silverbullet.md/uploads/default/original/1X/5b2de3fc461f344e4b65c7ccea5c16553c53c7a4.png)

---

<div class="post-metadata">

**Author:** ![Maarrk](https://community.silverbullet.md/user_avatar/community.silverbullet.md/maarrk/32/56_2.png) [@Maarrk](https://community.silverbullet.md/u/Maarrk)\
**Post date:** [September 30, 2024, 7:58pm UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/6 "2024-09-30T19:58:54Z")

</div>

I’m afraid that this is not my fault, but a bug in how SilverBullet handles empty cells, that seems to be previously unknown. After a bit of my troubleshooting I described it in the general case on GitHub:

> <https://github.com/silverbulletmd/silverbullet/issues/1104>
>
> The columns in a given row are assigned in order of their definition, skipping e…mpty columns.
> 
> Minimal example:
> ~~~
> |Description|Foo|Bar|
> |-----------|---|---|
> |Row 1 |A1 |B1 |
> |Row 2 | |B2 |
> 
> \`\`\`query
> table where page = @page.name
> \`\`\`
> ~~~
> 
> Produces the following output (notice that second row has "B2" assigned to "foo"):
> 
> |Description|Foo|Bar|
> |-----------|---|---|
> |Row 1 |A1 |B1 |
> |Row 2 | |B2 |
> 
> |tableref|ref|tag|tags|page|pos|description|foo|bar|
> |--|--|--|--|--|--|--|--|--|
> |Indexing issue@1|Indexing issue@45|table||Indexing issue|45|Row 1|A1|B1|
> |Indexing issue@1|Indexing issue@67|table||Indexing issue|67|Row 2|B2||

> [@molnarti](#):
>
> as you can see in the image, only the items where link is present are summed. any idea how to fix this?

To work around the bug with empty cells in your specific table, you can either:

- put any text in each cell of the “Link” column, like N/A or TODO or just a `.` (dot)
- reorder the columns so the “Price” column isn’t after any column that may be missing items; in your example that order would be “Item”, “Price”, “Link”

---

<div class="post-metadata">

**Author:** ![dhendiii](https://community.silverbullet.md/user_avatar/community.silverbullet.md/dhendiii/32/2544_2.png) [@dhendiii](https://community.silverbullet.md/u/dhendiii)\
**Post date:** [December 29, 2025, 7:54am UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/7 "2025-12-29T07:54:05Z")

</div>

Hi,  
Im trying to add this functionality on my note, but turns out it only for V1.  
Could you help me how to use this functionality on V2?

Thanks!

---

<div class="post-metadata">

**Author:** ![malys](https://community.silverbullet.md/user_avatar/community.silverbullet.md/malys/32/840_2.png) [@malys](https://community.silverbullet.md/u/malys)\
**Post date:** [December 30, 2025, 9:30am UTC](https://community.silverbullet.md/t/counting-trip-expenses/414/8 "2025-12-30T09:30:09Z")

</div>

[[FEATURE] Markdown Table formula with formulajs](https://community.silverbullet.md/t/feature-markdown-table-formula-with-formulajs/3208) do more and work in V2
