Why two formulas for a in a linear function are equal
They are not equal. Your two equations feature different variables, so clearly, they cannot be the same. However, they are somewhat related, as both are closely linked to linear functions.
Short answer:
The first formula is used to calculate $a$ if you do know $b$ and you know one point on the graph of the function.
The second formula is used to calculate $a$ if you don't know $b$ and you know two points on the graph of the function.
Longer answer:
Both equations can be seen to represent the same thing, which is a linear function of $x$.
Remember: By definition, a linear function is a function of $x$ equal to $$f(x)=ax+b$$
From this equation, we can see that, by moving $b$ to the left side of the equation and dividing by $x$, the equation is equivalent to the equation $$a=\frac{f(x)-b}{x}$$ so long as $x\neq 0$.
Therefore, the above expression is an expression that is true for every point $(x,f(x))$ on the graph of the linear function.
I can use this formula to calculate $a$, for example, if I already know the value of $b$ and one single point on the line, and want to calculate $a$.
On the other hand, let's we take two points, $(x_1,y_1)$ and $(x_2,y_2)$. Then, let's imagine that the two points are on the same line, then $y_1=ax_1+b$ and $y_2=ax_2+b$. Subtracting these two equations, and some division later, we get the equation $$a=\frac{y_2-y_1}{x_2-x_1}$$
which is an equation that is true for every pair of points on the graph of the linear function.
I can use this formula to calculate $a$ if I don't know what $b$ is, so long as I know two points on the graph.
Edit:
There is also a "third", special option for calculating $a$. Let's say you happen to know two points $(x_1, y_1)$ and $(x_2, y_2)$, and the $x$ value for one of them is $0$, that is, $x_1=0$. There are now two ways of calculating $a$.
Using the fact that $f(x)=ax+b$, you can plug in $x_1=0$ and get $y_1=a\cdot 0 + b$ which means that $y_1=b$. You now know the value of $b$ and one point on the equation, which is $(x_2,y_2)$, and you can use the first formula to calculate $a$ to get $$a=\frac{f(x_2)-b}{x_2}\\ a=\frac{y_2-y_1}{x_2}\\ a=\frac{y_2-y_1}{x_2-0}\\ a=\frac{y_2-y_1}{x_2-x_1}$$
You can see that the last formula in this case is exactly the same as the second formula for calculating $a$ in your question.
What is meant by all this is that if You consider an affine-linear function ( actually just the first part $x\mapsto ax$ is linear but by abuse of language one calls the affine function $f$ linear too): $$f(x)=ax+b$$ You can compute $a$ by computing for any $x_0\neq 0$: $$a=\frac{f(x_0)-b}{x_0}$$. On the other hand You know that given two points $A(x_1|y_1)$ and $B(x_2|y_2)$ and $x_1\neq x_2$ there is exactly one affine-linear function $f$ thats graph passes through these points and since the slope of an affine-linear function is everywhere the same You can compute it by the difference-quotient: $$a=\frac{y_2-y_1}{x_2-x_1}.$$ And if the above function $f$ is this one affine-linear function that has a graph passing through $A$ and $B$ then of course You have: $$\frac{y_2-y_1}{x_2-x_1}=\frac{f(x_0)-b}{x_0}$$ for any $x_0\neq 0.$