The honest rule of thumb is four to eight weeks before departure for the ideal consultation, because some protection takes time to build. Certain vaccines need a couple of weeks to reach full effect, some require multiple doses spaced weeks apart, rabies pre-exposure being the classic multi-visit series, and yellow fever certificates only become valid a set number of days after the jab. Accelerated schedules exist for several vaccines when time is short.
Malaria prophylaxis has its own timing: depending on the drug, you start days to weeks before entering the risk area, continue throughout, and keep taking it for a period after leaving, which travelers often forget.
That said, the biggest mistake is skipping the clinic because you've left it late. Even a week before departure, a travel health professional can prioritize what still helps, use rapid schedules, and prescribe standby medications. Last-minute is worse than early, but it beats nothing.