The nature of the flow below the cloud level on Jupiter and Saturn is still unknown. Relating the flow on these planets to perturbations in their density field is key to the analysis of the gravity measurements expected from both the Juno (Jupiter) and Cassini (Saturn) spacecrafts during 2016–2018. Both missions will provide latitude-dependent gravity fields, which in principle could be inverted to calculate the vertical structure of the observed cloud-level zonal flow on these planets. Theories to date connecting the gravity field and the flow structure have been limited to potential theories under a barotropic assumption, or estimates based on thermal wind balance that allow baroclinic wind structures to be analysed, but have made simplifying assumptions that neglected several physical effects. These include the effects of the deviations from spherical symmetry, the centrifugal force due to density perturbations and self-gravitational effects of the density perturbations. Recent studies attempted to include some of these neglected terms, but lacked an overall approach that is able to include all effects in a self-consistent manner. The present study introduces such a self-consistent perturbation approach to the thermal wind balance that incorporates all physical effects, and applies it to several example wind structures, both barotropic and baroclinic. The contribution of each term is analysed, and the results are compared in the barotropic limit with those of potential theory. It is found that the dominant balance involves the original simplified thermal wind approach. This balance produces a good order-of-magnitude estimate of the gravitational moments, and is able, therefore, to address the order one question of how deep the flows are given measurements of gravitational moments. The additional terms are significantly smaller yet can affect the gravitational moments to some degree. However, none of these terms is dominant so any approximation attempting to improve over the simplified thermal wind approach needs to include all other terms.