We can get the manager of a user by calling the Get manager (V2) action of the Office 365 Users connector in Flow.
Since the Manager field of the User entity in Dynamics 365 is a lookup (to the User entity) we need to get the userid of the manager.
Here is the overview of the flow:
First we get all active users and for each user we get their details and get their manager.
We check if the manager is not empty, and get the user record filtered on the user name of the manager.
Then we get the details of the manager’s user record, if available (I didn’t build in this check yet).
Finally we update the user record with the userid of the manager in case they’re not equal.
And here are the (test) results in Dynamics 365:
Disclaimer: when the manager is not part of the business unit hierarchy then the flow will presumably fail, so be sure to administer the business unit hierarchy in Dynamics 365 in case of organizational changes that have impact on the security structure.