What is written above concerning this matter (in Genesis 40:23): "Yet the chief cupbearer did not remember Joseph." Rabbi Berekhyah said: This "did not remember" — he had been remembered, and he had been lifted up. But (it says) "he forgot him." He had said outside: I will go and tell Pharaoh, "Bring him up." Immediately, "he forgot him." Until his appointed end to go out had arrived, the Holy One, blessed be He, brought about the occasions concerning him in order to bring him out, as it is said (in Genesis 41:1): "Now it came to pass at the end of two full years."
Why the Cupbearer Forgot Joseph Until His Appointed End
Midrash Tanchuma Buber, Miketz 3:1
מה כתיב למעלה מן הענין, ולא זכר שר המשקים את יוסף (בראשית מ כג). אמר ר' ברכיה ההן ולא זכר, זכורהיה והיה נשואי אלא וישכחהו, היה אומר בחוץ אלך ואומר א פרעה עלו, מיד וישכחהו, עד שהגיע קצו לצאת הביא הקב"ה עלילות עליו כדי להוציאו, שנאמר ויהי מקץ שנתים ימים.