First Formula $$b_1=\dfrac{\displaystyle\sum_{i=1}^n(Y_iX_i-\bar Y \bar X)}{\displaystyle\sum_{i=1}^n(X_i^2-\bar X^2)}$$
Second Formula $$b_1=\dfrac{\displaystyle\sum_{i=1}^nY_i(X_i- \bar X)}{\displaystyle\sum_{i=1}^n(X_i-\bar X)^2}$$
I understand the derivation of the slope $b_1$ in the Ordinary Least Squares regression. However, I am having a hard time converting the initial formula to the second one using algebra.
Both the numerator and the denominator can be adjusted using the two tricks that $\sum_{i=1}^n C = nC$ where $C$ is a constant, and $n\bar{Y} = \sum_{i=1}^n Y_i$.
In the numerator, you just have to expand:
$\sum \bar{X} \bar{Y} = \bar{X} \left(\sum \bar{Y}\right) = \bar{X} \left(n \bar{Y}\right) = \bar{X} \sum Y_i$ and you can then combine that with the other term and factorise the common $\bar{X}$
In the denominator, you need to expand the square, and then use the tricks to make two of the terms partially cancel out.