# Creating "Formula" Columns

**URL:** https://talk.observablehq.com/t/creating-formula-columns/2558
**Category:** Help
**Created:** [November 16, 2019, 9:22am UTC](https://talk.observablehq.com/t/creating-formula-columns/2558 "2019-11-16T09:22:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![LukasGrebe](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lukasgrebe/32/2368_2.png) [@LukasGrebe](https://talk.observablehq.com/u/LukasGrebe)
#### Post date: [November 16, 2019, 9:22am UTC](https://talk.observablehq.com/t/creating-formula-columns/2558/1 "2019-11-16T09:22:55Z")

</div>

hi,  
with a data Array, how do i add “calculated”/“Formula” columns as i would in Excel?

in [https://observablehq.com/@lukasgrebe/a4-avant](https://observablehq.com/@lukasgrebe/a4-avant) i’ve inlined a TSV of Expenses, Date and Odometer of my car. I’d like to, for example, calculate the difference in “odometer” between two of the same types of expenses. for example the “fuel” expense to calcualte an mpg column.

- Lukas

---

<div class="post-metadata">

### Author: ![mcmcclur](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mcmcclur/32/2364_2.png) [@mcmcclur](https://talk.observablehq.com/u/mcmcclur)
#### Post date: [November 16, 2019, 11:37am UTC](https://talk.observablehq.com/t/creating-formula-columns/2558/2 "2019-11-16T11:37:09Z")

</div>

The `d3.tsvParse` command creates an array of objects and you can act on that array using standard Javascript techniques. To compute mpg for each entry, you might do something like

```
a4.map(d => parseFloat(d.odometer)/parseFloat(d.price))

```

If you want to add this as a column to `a4` for ease of use with Vega Lite, you could do something like:

```
a4 = {
  let a4 = d3.tsvParse(...),
  a4.forEach(function(d) {
    d.mpg = parseFloat(d.odometer)/parseFloat(d.price))
  }
  return a4
}

```

I [forked your code](https://observablehq.com/@mcmcclur/a4-avant) to illustrate. It doesn’t look particularly nice so I’m not sure I’ve done the desired calculation but I think it at least illustrates _how_ to do these types of manipulations.

---

<div class="post-metadata">

### Author: ![LukasGrebe](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lukasgrebe/32/2368_2.png) [@LukasGrebe](https://talk.observablehq.com/u/LukasGrebe)
#### Post date: [November 16, 2019, 3:16pm UTC](https://talk.observablehq.com/t/creating-formula-columns/2558/3 "2019-11-16T15:16:06Z")

</div>

Thanks for your reply! I had looked into the Map function, however for a correct calculation, I’d need to reference previous and next array elements.

So if `i` is the current array index, of `mpg[i]=(odometer[i+1]-odometer[i])/amount[i]`  
Wich i think isn’t possible with a map function.

And this gets more complicated since index `i+1` Might not be the „next“ entity. I suppose this could be solved by sorting the array.

---

<div class="post-metadata">

### Author: ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)
#### Post date: [November 16, 2019, 8:04pm UTC](https://talk.observablehq.com/t/creating-formula-columns/2558/4 "2019-11-16T20:04:46Z")

</div>

I’d probably use [d3.pairs](https://observablehq.com/@d3/d3-pairs) for this, like so:

```auto
d3.pairs(
  a4.filter(d => d.type === "fuel")
    .sort((a, b) => d3.ascending(a.date, b.date)),
  (a, b) => ({
    ...b,
    odometer_delta: b.odometer - a.odometer
  })
)

```

Here’s a suggestion (also switching to a file attachment):

> **[Comparing ‘A4 Avant’ by Lukas Grebe to ‘A4 Avant’ by Mike Bostock](https://observablehq.com/compare/30db33bb35a88034@43...2f5f9c8fe9924f70@60)**
>
> A comparison of two Observable notebooks.

---

<div class="post-metadata">

### Author: ![LukasGrebe](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/lukasgrebe/32/2368_2.png) [@LukasGrebe](https://talk.observablehq.com/u/LukasGrebe)
#### Post date: [November 17, 2019, 6:39am UTC](https://talk.observablehq.com/t/creating-formula-columns/2558/5 "2019-11-17T06:39:33Z")

</div>

thank you!  
incidentally i had stumbled upon pairs last night but not published my changes in the meantime. It seems to cover exactly this use case.  
Adding a filter for fuel is an elegant way to handling the different types of expenses. I had been messing with group and mapping the grouped types with pairs.

---

<div class="post-metadata">

### Author: ![nico-dsq](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@nico-dsq](https://talk.observablehq.com/u/nico-dsq)
#### Post date: [September 11, 2023, 11:01am UTC](https://talk.observablehq.com/t/creating-formula-columns/2558/6 "2023-09-11T11:01:02Z")

</div>

Hi both.

I am pretty new to Observable and JavaScript. I am trying to do something similar to this problem, and a search brought me here. I’m hoping you could help.

I have a data table and I want to add four columns that do calculations with two sliders’ values as input based on what is in the other columns (like formulas in Excel, but with dynamic inputs). I.e. I have true\_positive, false\_positive, false\_negative columns and I want to calculate the costs associated with these as follows:

fp\_cost = false\_positive \* replace(slider value)

fn\_cost = false\_negative \* repair(slider value)

tp\_cost = true\_positive \* replace(slider value)

total\_savings = (true\_positive \* repair(slider value)) - tp\_cost - fp\_cost

Is that achievable using the index, as above? Should I use a different approach? Any help or suggestions would be welcomed.

Here is a notebook with the data and graphs I’m using: [Test / Nico's Workspace | Observable](https://observablehq.com/d/8e3da4cc8c6c6609)
