I've traveled to Machu Picchu just about every month of the year. Not to say that travel trends don't change, but I have a pretty good idea how to avoid the crowds at Machu Picchu. My focus here will be on the season, not the time of day, because no matter what season you travel, afternoons are always less crowded.  

I don't include Inca Trail permits because they have limit of 500 per day which includes porters, guides and cooks.  This leaves about 200 spots per day for hikers and this number is stable year round except in February when the Inca Trail is closed for maintenance.  

Since 2016 I have collected Machu Picchu general entry ticket statistics from the official Peruvian government website through December 31, 2018. Starting January 1, 2019 they changed the website and removed the statistical information.  It is no longer possible to see entry ticket data. 

Rank top 5 months by most number of tickets sold.  Here's a link to the page where I list all the data on Machu Picchu tickets sold in 2018.

August 149,185

July 141,680

September 127,156

October 124,345

May 117,545

Rank lowest tickets sold per month

December 58,954 (more tickets are sold during Christmas and New Year holidays than earlier in the month)

February 69,767

January 87,237

March 87,274

November  98,151

Based on a normal rainy season, typically from late November to early April, I prefer to travel during the early rainy season rather than the end of the rainy season. This is because the impact of early rains is less likely to cause disruptions from flooding and mudslides. After a season of rain, when everything is saturated, it's more likely that flooding, mudslides and road closures can occur. Perurail closes the Poroy station from January 1 - April 30 every year because the section of train tracks from Poroy to Ollantaytambo is most likely to experience disruption during this segment of the rainy season. For people who book train tickets from Poroy during this time, Perurail offers bimodal service beginning in Cusco. This is a hidden benefit only available during the rainy season and saves you the cost of a taxi or transfer by car to the station because the bimodal service begins in Cusco. Follow this link to see full details.

When I overlay the rainfall data with the ticket data (and based on my personal intel) the month of November and early December are the best months to travel to avoid crowds and the heaviest rainfall.