OK, it's taken me a little while to sort out a test environment.
I have set up a fresh AD domain. The Built-in Administrators group has the following:

And then I populated the hierarchy as follows:
Domain Admins Group
Security Administrators Group
As you can see, a fairly simple hierarchy.
I am trying to set up an audit screen that shows the members of a group, including any sub groups that are in the group. So if I were to filter by the group Administrators, I get the following expected result for users:
- lapsangadmin
- Siuan Sanche (admin)
- Lean Sharif (admin)
So now I enter Power BI and use the AD connector to Users. This gives me a rather difficult to deal with table:
Unedited Users Query
When I filter it a bit, and expand the "top" column I get:

Again, I expand those lists into separate rows, and expand the records, and I get...

The code I use to get there is:
let
Source = ActiveDirectory.Domains("lapsang.example"),
lapsang.example = Source{[Domain="lapsang.example"]}[#"Object Categories"],
user1 = lapsang.example{[Category="user"]}[Objects],
#"Expanded top" = Table.ExpandRecordColumn(user1, "top", {"cn", "memberOf"}, {"top.cn", "top.memberOf"}),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded top",{"top.cn", "top.memberOf", "distinguishedName"}),
#"Expanded top.memberOf" = Table.ExpandListColumn(#"Removed Other Columns", "top.memberOf"),
#"Expanded top.memberOf1" = Table.ExpandRecordColumn(#"Expanded top.memberOf", "top.memberOf", {"distinguishedName"}, {"top.memberOf.distinguishedName"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded top.memberOf1",{"top.cn", "distinguishedName", "top.memberOf.distinguishedName"})
in
#"Reordered Columns"
with this, I can tell that lapsangadmin is in Administrators and Domain Admins, that Siuan Sanche (admin) is in Domain Admins and that Leane Sharif (admin) is in Security Admins.
But I can't filter by Administrators and get all three.
So I start to look at the group object instead, which is an equally unappetising initial query:

In top, there is again a "memberOf" column, which has a list of other records:

And again, expanding that gives me:

The code I use to get here is:
let
Source = ActiveDirectory.Domains("lapsang.example"),
lapsang.example = Source{[Domain="lapsang.example"]}[#"Object Categories"],
user1 = lapsang.example{[Category="user"]}[Objects],
#"Expanded top" = Table.ExpandRecordColumn(user1, "top", {"cn", "memberOf"}, {"top.cn", "top.memberOf"}),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded top",{"top.cn", "top.memberOf", "distinguishedName"}),
#"Expanded top.memberOf" = Table.ExpandListColumn(#"Removed Other Columns", "top.memberOf"),
#"Expanded top.memberOf1" = Table.ExpandRecordColumn(#"Expanded top.memberOf", "top.memberOf", {"distinguishedName"}, {"top.memberOf.distinguishedName"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded top.memberOf1",{"top.cn", "distinguishedName", "top.memberOf.distinguishedName"})
in
#"Reordered Columns"
So this is where I'm stuck.
Eventually, I could just keep expanding memberOf until I run out of things, and then unpivot the resulting distinguishedName columns into one. The thing is, I'm trying to make this a generic solution that doesn't need to know how many times to expand, because whilst in this test case, I only have a simple hierarchy like this, in some of the Active Directories I deal with this hierarchy may go up to 6 or 8 groups deep.
So I suppose I'm asking: how do I do this sort of recursion in M, such that I can get a table of all the parents a group may have, withough needing to do it a specific number of times?
I suppose I'm trying to get a table like this:
Group | Contained in |
Administrators | Administrators |
Security Administrators | Security Administrators |
Security Administrators | Domain Admins |
Security Administrators | Administrators |
Domain Admins | Domain Admins |
Domain Admins | Administrators |
...and so on
From there, I can link my users to that Group Column, and then filter by a summary of the Contained In Column.
Any hints gratefully appreciated...