Education is considered to be one of the main factors of development, economic growth and social progress. No country can achieve sustainable economic development without substantial investment in human capital. In Spain, school failure represents one of the main problems in the educational system, with potentially dramatic consequences for the basic competences required in the labour market and job instability, with the risk of economic and social exclusion. In this paper, we aim to identify the factors that define the risk of school failure in Madrid (Spain) by applying logit models. In this process we use a definition of school failure risk which relates to the probability of scoring below level 2 in the evaluation of competences (diagnostic assessment), and we use grade retention as a proxy of school failure. The variables included in the model cover several areas, such as personal, family and school characteristics. The results show that it is important that the policies to strengthen the educational system begin with early childhood education, as educational delay symptoms are detected, and it is necessary to intensify efforts towards personalized assistance to help identify potential learning problems, especially in those groups in the worst socioeconomic situations, which are most at risk of school failure.