So many players are so easily confused by how a granary works. This confusion is caused by the fact that the effect isn't shown in the city screen when the granary isn't full, i.e. when the food storage box is less than half full.
It does not matter when you build it. Your city will benefit from the effect of the granary the first time it grows after the granary is built. You get the same benefit when growing to size 7 or 13 as you do at any other point. It may appear that you don't get the full effect, but in fact you do. A granary allows a city to retain one half of its food storage when it increases in size. For a town, size 1 to 6, 20 food are required for growth. With a granary, 10 food are retained, and this is always evident if you zoom to the city after growth. If your city has just reached size 7, you can zoom in and see that the 10 food is there. You won't see the granary in the bottom half of the food storage box because you now need 40 food for growth, so you need 20 food to fill the granary. Similarly, when your city becomes a metro at size 13, 20 food are retained, and the effect of the granary will be seen in the city screen as soon as you have 30 food stored. If the game had been designed to fill the food and shield banks from the bottom up, all of this confusion could be avoided.