Let $\pi:X\to S$ be a separated flat morphism of finite type of Noetherian schemes. Does $\pi$ necessarily factor as an open immersion followed by a proper flat morphism? The analogue of this question with the word "flat" replaced by "smooth" has a negative answer (consider an elliptic curve over $\mathbb{Q}_p$ that has bad reduction).
This already fails if $S$ is regular of dimension $3$ and $\pi$ is quasifinite. Indeed, let $X$ be a normal affine variety over $\mathbf C$ of dimension $3$ with an isolated nonCohen–Macaulay singularity (e.g. an affine cone over a smooth projective surface $Y$ with $H^1(Y,\mathcal O_Y) \neq 0$). By Noether normalisation, there exists a finite surjection $$\pi \colon X \to S = \mathbf A^3,$$ without loss of generality taking the isolated singularity $x_0 \in X$ to the origin $0 \in \mathbf A^3$. Let $U = X \setminus x_0$, which is smooth by assumption, so $\pi_U$ is flat by miracle flatness. Now I claim that $\pi_U \colon U \to \mathbf A^3$ does not have a flat compactification.
Indeed, suppose $U \hookrightarrow X' \stackrel{\pi'}\to S$ is a factorisation into an open immersion and a proper flat morphism. Because $\pi'$ is flat and generically finite, it is quasifinite, hence finite since it is proper. Since $\pi'$ is finite flat and $S$ is regular, we conclude that $X'$ is Cohen–Macaulay.
Let $\bar U \subseteq X'$ be the schemetheoretic closure of $U$, and let $V = S \setminus 0$. Since $\pi^{1}(V) \subseteq U$, we conclude that $\bar U \setminus U$ is supported on $\pi'^{1}(0)$, in particular has dimension $0$. By Hartshorne's connectedness theorem, this implies that there are no other components in $X'$ (otherwise two components would meet only in a $0$dimensional set), i.e. $\bar U = X'$ settheoretically. Since $X'$ is generically reduced and Cohen–Macaulay, it is reduced, so $\bar U = X'$ schemetheoretically.
In particular, $X' \setminus U$ is $0$dimensional, so $X'$ is regular in codimension $1$ since the same holds for $U$. Since $X'$ is Cohen–Macaulay, this forces $X'$ normal, so it equals the normalisation of $S$ in $K(U)$, which is $X$. But $X$ is not Cohen–Macaulay, contradicting flatness of $\pi'$. $\square$

$\begingroup$ is it true is $S$ is regular of Krull dimension 2? $\endgroup$ – user158636 Jun 12 '20 at 8:03

$\begingroup$ Hmm, there is a positive result in the quasifinite case if $S$ is regular of dimension $2$ and $X$ is normal. Because then you can take the normalisation, which is Cohen–Macaulay (this uses dimension $2$), hence flat. I'm not sure what happens if $X$ is not normal, or if $\pi$ is not quasifinite. A thing to try is the projectivisation of a vector bundle (e.g. of rank $2$) that does not extend, but it's hard to get a grip on the possible compactifications (which is why I restricted to the quasifinite case). $\endgroup$ – R. van Dobben de Bruyn Jun 12 '20 at 19:35

$\begingroup$ Of course if $S$ is regular of dimension $1$ the result is positive (for arbitrary $\pi$): in this case flat just means torsion free, which can be arranged by taking the schemetheoretic closure of $X$ in an arbitrary compactification. $\endgroup$ – R. van Dobben de Bruyn Jun 12 '20 at 19:47