Running a regression on dates and then using the estimated parameters to produce a forecast presents two challenges: (1) you can’t do math on dates, and (2) it’s not straight forward to add future dates to a model.

Here’s how to do it in Looker:

**Step 1:**

Right-join a calendar view to the Explore.

```
- join: mr_calendar_dates
sql: RIGHT OUTER JOIN mr_calendar_dates ON calendar_dates.date_id = order.created_at
```

**Step 2**

Create two dimensions in the calendar view that transform dates into a series of numbers. One of them should go into the past and have NULL values for the future (for training the model), and the other should go into the past and the future (for making predictions). I did it like this:

Please note that this SQL will need to be modified according to your SQL dialect.

```
- dimension: is_past
type: number
sql: CASE WHEN ${date} > CURRENT_DATE THEN NULL ELSE 1 END
hidden: true
- dimension: days_from_today
type: number
sql: DATEDIFF(${date}, current_date)
- dimension: days_from_today_past
type: number
sql: DATEDIFF(${date}, current_date)*${is_past}
```

**Step 3**

We are now ready to go to the Explore. Select the two time dimensions we just made (**x**) and whatever measure you want to model (**y**). Take the **ln(y)** if you want to assume exponential growth. Filter on the date, selecting the amount of time in the past you want to train the model on, and the amount of time in the future you want to predict.

**Step 4**

Use these formulae to estimate intercept (**a**) and slope (**b**) using the time-series with NULL values for the future:

Or, in words, you need sums and sum^2 of the time axis, counts of numbers in one of your columns, and then the sum and sum_product of the predicting set. Then apply the estimated parameters to the time-series that has future values (and wrap the whole thing in **exp()** if you were regressing on **ln(y)**).

**Step 5**

Make the plot deselecting everything but **y** and predicted **y**. The x-axis label won’t be perfect because we have multiple dimensions, and if you want to clean it up a bit, you can turn Axis Value Labels off.