Try the reset: push and hold the button straight upward and it will attempt to reset it. Do not let go until it all stops.
BMW Sunroof Initialization Procedure Panoramic Roof - YouTube
Other than that, the sunroof cassette can be a problem. They tried to fix the wind deflector in front getting stuck up by replacing a piece in the cassette but eventually replaced the whole cassette after I brought it in 3 times for the same issue.