You can do it. An example of this would be learning rust when you reach the 7th level, and at the same time, unlearn hot hands and learn dimension door.
The reason is the sentence:
which must be for a level for which you have spelling slots.
Because you are a level 7 wizard, you have 4 level spell locations, and so in separate instances of 1) learning a spell, and 2) replacing a spell, you can to do for a spell that is a level for which you have spelled the locations.
In addition, it is important to keep the "known spells" and the "spell locations" separate, since the spell locations are your power (often described as mana or energy in other games and systems) and spells are their versatility. Having 2 4th level spells can be useful, even if you have only one location for the 4th level, because the situations you use dimension door and rust are very different.