Thank you for posting your query on Q&A.
I understand that you are looking to create dynamic group in Azure AD with users from specific departments in the EMEA region, excluding a specific country (abc), but including a specific user (John) from that excluded country.
The query you have written is may does not work, because it uses the -startsWith operator for department, which will match any department that begins with 123 or 124, also you have used the -contains operator for extensionAttribute1, which will match any value that contains EMEA.
In your case to achieve your ask I would suggest you try with the below expression.
(user.accountEnabled -eq true) and (user.extensionAttribute1 -eq "EMEA") and ( (user.department -eq "123" or user.department -eq "124") and (user.extensionAttribute2 -eq "A1" or user.extensionAttribute2 -eq "A2") and (user.country -ne "abc" or (user.country -eq "abc" and user.employeeId -eq "98765")) )
The above expression pull all the users from department 123 or 124, with job codes A1 or A2, from EMEA region, excluding the country abc, except for the user John who has the employee ID 98765.
I hope this answer helps! If you have any further questions, please feel free to ask.
Thanks,
Akhilesh.
Please "Accept the answer" if the information helped you. This will help us and others in the community as well.