google sheets – Linear regression with 2 standard deviations?


I have a series of data, and I would like to determine the line of best fit (linear regression) over various numbers of datapoints, along with 2 standard deviations above and below that line.

I’m using Google Sheets, and it seems like the built-in Chart feature doesn’t have any standard deviation built in, although it can do the regression portion. But it is limited (AFAIK) to a hardcoded dataset, and I want to be able to for example determine the regression for the last N datapoints.

Here is an example of what I get currently with some sample data:

example of line of best fit

So that’s fine, but how can I get it to display 2 standard deviations on either side of the best fit line, and for a variable portion of the total dataset?