This expression comes from the nineteenth century play “The Broken Sword.” One of the characters is continually telling the same jokes. He does, however, alter them very, very slightly each time. At one point in the play he tells another character a joke involving a cork tree, the other character tells him it was a chestnut tree, he continues by saying that he has heard the joke twenty seven times and knows it’s a chestnut.