Let X be a ball Banach function space on $${\mathbb R}^n$$ . In this article, under some mild assumptions about both X and the boundedness of the Hardy–Littlewood maximal operator on the associate space of the convexification of X, the authors prove that, for any locally integrable function f with $$\Vert \,|\nabla f|\,\Vert _{X}<\infty $$ , $$\begin{aligned} \sup _{\lambda \in (0,\infty )}\lambda \left\| \left| \left\{ y\in {{\mathbb {R}}}^n:\ |f(\cdot )-f(y)| >\lambda |\cdot -y|^{\frac{n}{q}+1}\right\} \right| ^{\frac{1}{q}} \right\| _X\sim \Vert \,|\nabla f|\,\Vert _X \end{aligned}$$ with the positive equivalence constants independent of f, where the index $$q\in (0,\infty )$$ is related to X and $$|\{y\in {\mathbb R}^n:\ |f(\cdot )-f(y)| >\lambda |\cdot -y|^{\frac{n}{q}+1}\}|$$ is the Lebesgue measure of the set under consideration. In particular, when $$X:=L^p({\mathbb R}^n)$$ with $$p\in [1,\infty )$$ , the above formulae hold true for any given $$q\in (0,\infty )$$ with $$n(\frac{1}{p}-\frac{1}{q})<1$$ , which when $$q=p$$ are exactly the recent surprising formulae of H. Brezis, J. Van Schaftingen, and P.-L. Yung, and which in other cases are new. This generalization has a wide range of applications and, particularly, enables the authors to establish new fractional Sobolev and new Gagliardo–Nirenberg inequalities in various function spaces, including Morrey spaces, mixed-norm Lebesgue spaces, variable Lebesgue spaces, weighted Lebesgue spaces, Orlicz spaces, Orlicz-slice (generalized amalgam) spaces, and weak Morrey spaces, and, even in all these special cases, the obtained results are new. The proofs of these results strongly depend on the Poincaré inequality, the extrapolation, the exact operator norm on $$X'$$ of the Hardy–Littlewood maximal operator, and the exquisite geometry of $${\mathbb {R}}^n.$$