# Calculate the difference between rows

This tutorial shows how to reference a previous row in a spreadsheet formula.

In this example, we have daily stock prices for a bunch of different stocks. We want to calculate the daily change in stock price for each stock.

To follow along, download the data here.

DailyStockPrices.csv

110KB

Text

Import data into Mito

We're going to use a spreadsheet formula to calculate the daily change in stock price. Create a new column for the formula.

Add a new column

To calculate the daily change in stock price, compare each row to the previous row. If they have the same stock symbol, calculate the difference in stock price. Otherwise, set the daily price change to zero.

If we write the formula in row 1 of the Daily Price Change column, then the formula should look like this:

IF(Symbol1==Symbol0, Close1-Close0,0)

Calculate the difference between daily stock prices

Once the formula is applied, check out the results. Notice that the first row has a daily price change of zero because there was no previous stock price to compare.

View the results