Preheat the oven to 350°F (175°C).
Grease a baking dish with butter.
Layer half of the sliced potatoes in the dish.
Sprinkle half of the diced ham over the potatoes.
In a saucepan, melt butter over medium heat, then add chopped onion and sauté until translucent.
Stir in flour and cook for 1-2 minutes.
Gradually whisk in milk and cream, bringing to a simmer.
Season with salt and pepper, then remove from heat.
Pour half of the cream mixture over the first layer of potatoes and ham.
Repeat layers with remaining potatoes, ham, and cream mixture, then top with shredded cheese.
Cover with foil and bake for 30 minutes, then remove foil and bake for another 15 minutes until golden.