The granularity principle [Gordy (2003)] allows for closed form expressions of the risk measures of a large portfolio at order 1/n, where n is the portfolio size. The granularity principle yields a decomposition of such risk measures that highlights the different effects of systematic and unsystematic risks. This paper derives the granularity adjustment of the Value-at-Risk (VaR), the Expected Shortfall and the other distortion risk measures for both static and dynamic risk factor models. The systematic factor can be multidimensional. The methodology is illustrated by several examples, such as the stochastic drift and volatility model, or the dynamic factor model for joint analysis of default and loss given default.