I had the same problem with a 27 inch 2013 model. Since I don’ t live in the usa I could not bring it to the Apple store. I’ ve tried everything. Changing sockets, buying UPS, disconnecting everything and connecting them back 1 by 1. Had a IT friend look at it and at his house everything worked perfect. Brought it back home and same problem again. Suddenly the battery of the keyboard was dying very quickly. I had to change batteries every 2 weeks. After the 2nd time I was so fed up with batteries that I just replaced the wireless Apple keyboard with a generic non Apple wired keyboard. I was thinking about buying a new MAC. So glad I did not do that. The MAC has been working fine since that day, now almost 3 months ago.