The first version of it I ever saw was in a Baby Huey comic book in about 1967, a single page comic, and it struck my infant self as wise and unusually funny. I wonder how old the joke is.
Not sure if the real source can be traced. Birbal lived in the 1500's and I have heard another version atributed to another court jester called Tenali Raman who lived in the same century.