114

In an Emacs Org-mode table, when you have a column full of integers I know you can do C-c + followed by C-y to paste the sum of the values in the column. I want to know the formula to place in the last row to always sum the whole column.

I've tried everything. The docs show you how to sum two columns together but not one.

3
  • 8
    The best I've been able to get is to add a highline at the top and bottom of the table and do a column formula: =vsum(@I..@II) Commented Jul 14, 2011 at 6:01
  • As an FYI, I just posted a similar question about hline targeting here: stackoverflow.com/questions/6689424/… This question is already getting votes and the answer may be different, so I will leave it up in the meantime. Commented Jul 14, 2011 at 6:48
  • For those wondering, use C-c C-c when point is at #+TBLFM: line to execute the forumula given in the answers.
    – legends2k
    Commented Jul 13, 2022 at 7:48

6 Answers 6

79

The last row of a table is @> For example to get the sum for the third column in the last line, you can use the formula

@>$3=vsum(@2..@-1)

Maybe you have to adapt the @2, if you don't have a header line...

74

Assign a field-name using the ^ mark:

|---+---|
|   | 1 |
|   | 2 |
|   | 3 |
|---+---|
|   | 6 |
| ^ | x |
|---+---|
#+TBLFM: $x=vsum(@1..@-1)

See The Org Manual, Section-3.5.9 Advanced Features.

5
  • Thanks! This ends up working pretty good and I'm fine with it, however, I do now I have a "header" hline and "footer" hline with the same things. Is there a way to this using just the header hline. In the docs its the "-" marking, but then I don't know how to use vsum(@2..@[last item]). Commented Jul 14, 2011 at 18:20
  • 6
    You can also do #+TBLFM: @row$col=vsum(@1..@-1)
    – mgalgs
    Commented Aug 24, 2012 at 16:23
  • 11
    I get Unknown field: x Commented Aug 10, 2016 at 19:30
  • I also got unknown field. Read the manual, basically in the above example it's calling the row above the ^ x x, so that the 6 is calculated from the vsum of 1, 2, 3.
    – Japhir
    Commented Aug 29, 2020 at 10:42
  • 1
    Note that the ^ should be in the first column of the table. Not just the column preceding where you want the sum.
    – Dr. S
    Commented Jun 13, 2021 at 12:39
69

Yet another possibility makes use of horizontal lines (@I, @II, etc.) which are useful anyways to structure your table:

| What  |    $$ |
|-------+-------|
| Ice   |  3.00 |
| Soda  |  6.49 |
| Gin   |  4.99 |
|-------+-------|
| Total | 14.48 |
#+TBLFM: @>$2=vsum(@I..@II)

Without a header, just let the sum start at @0 as suggested by others already.

Edit: I just saw that you wrote this yourself already in a comment to your question.

4
  • 2
    It took me a minute to understand that @I and @II refer to the horizontal rules, but I think this is the simplest way to do it if you organize your table this way.
    – trey-jones
    Commented Sep 3, 2020 at 13:27
  • Which keybinding should I use to set sum's result?
    – alper
    Commented Nov 29, 2021 at 19:30
  • C-c C-c with point on the formula on the TBLFM line worked for me.
    – Winny
    Commented Feb 8, 2023 at 4:28
  • Can also use the greater-than symbol for the column, for example @>$>=vsum(@I..@II) puts the result on the last line of the last column. Brilliant!
    – alls0rts
    Commented Jan 6 at 15:13
15

You can try this:

$<col_num>=<func>(@2..@-1))

@2 is static. It refers to the 2nd row onwards. @-1 refers to the second to last row.

I think this was the easiest and non-intrusive way. It preserves your column names and does not clutter up the visual space. It does not require you to address the last row. It is addressed by default.

Rows can be added/removed. No other markers.

eg.
#+TBLFM: $3=vmean(@2..@-1)::$4=vsum(@2..@-1))

Sample table

   | Time                   | Input             | Test      | InQty |
   | <2018-03-13 Tue 06:15> | Water             |           |   200 |
   | <2018-03-13 Tue 07:03> |                   |           |       |
   |                        |                   |           |       |
   | <2018-03-13 Tue 07:31> | Water             |           |   180 |
   | <2018-03-13 Tue 09:00> | Chai              |           |   240 |
   | <2018-03-13 Tue 11:30> | Chai              |           |   240 |
   | <2018-03-13 Tue 16:01> | Water             |           |    60 |
   |                        |                   |           |       |
   |------------------------+-------------------+-----------+-------|
   |                        |                   |           |   920 |
   #+TBLFM: $4=vsum(@2..@-1)
4

Something which may not be apparent to a reader is the function is vsum() not sum()

The other thing, is that the @2..@-1 thing, is a reference to the row-specific label for the column being summed. The $A$1 thing in Excel is like @1$1 so a reference to vsum(@2..@-1) is saying "do a sum of the values from the column, using as the row index the @ value in the range 2, to the -1th (ie second last) row, but the column is a "given" in this so its vsum applied over [@2$col @3$col @4$col... @-1$col] if you try to map these concepts into Excel

0
|  3 |
|  2 |
|  5 |
| 10 |
#+TBLFM: @4$1=vsum(@1..@-1)

@1 refers to the 1st row, and @-1 to the row preceding the one holding the formula. That formula ignores hlines:

|  3 |
|  2 |
|  5 |
|----|
| 10 |
#+TBLFM: @4$1=vsum(@1..@-1)

Not the answer you're looking for? Browse other questions tagged or ask your own question.