is there away I can calculate the slope only if a criteria in a third column is met.

{=AVERAGE(IF($G$162:$G$1217=G162,$Q$162:$Q$1217))}

Essentially doing what the above does except calculate the slope and intercept. The third column is the problem for me i guess (x's, y's, and criteria column a)

any help would be appreciated