Identity.IsMemberOf

D

T

The M Code Behind the Power Query M function Identity.IsMemberOf

Understanding the Function Syntax

Before we dive into the M code, let’s examine the function syntax. The Identity.IsMemberOf function takes two parameters – the first is the user to be checked, and the second is the group to check against.


Identity.IsMemberOf(user as text, group as text) as logical


The function returns a logical value - either true or false - depending on whether the user is a member of the specified group.

The M Code Behind the Function

The M code behind the Identity.IsMemberOf function is relatively straightforward. The function first retrieves the current user's security groups using the following code:


groups = Binary.Decompress(Web.Contents(“https://graph.microsoft.com/v1.0/me/transitiveMemberOf/$/microsoft.graph.group?$count=true”), Compression.Deflate)


This code retrieves the current user's transitive security groups from the Microsoft Graph API and stores them in a variable called "groups."

Next, the function checks whether the specified group is included in the list of security groups. This is done using the following code:


isMember = List.Contains(groups, group)


This code uses the List.Contains function to check whether the specified group is included in the "groups" variable. If the group is found in the list, the function returns true; otherwise, it returns false.

Using the Function in Power BI

Now that we've explored the M code behind the Identity.IsMemberOf function, let's take a look at how it can be used in Power BI.

Suppose we have a report that contains sensitive information that we only want to share with members of a particular security group. We can use the Identity.IsMemberOf function to check whether the current user is a member of that group and show or hide the sensitive information accordingly.

To do this, we can create a measure that uses the Identity.IsMemberOf function to check whether the current user is a member of the desired security group. Here's an example measure:


IsMember = Identity.IsMemberOf(CURRENT_USER(), “Sensitive Data Group”)


This measure checks whether the current user is a member of the "Sensitive Data Group" security group and returns a true or false value accordingly.

We can then use this measure to control the visibility of our sensitive data. For example, we might create a visual that only displays the sensitive data if the user is a member of the "Sensitive Data Group" security group. Here's an example of how we might do this:

1. Select the visual that contains the sensitive data.

2. In the Visualizations pane, expand the "Format" section.

3. In the "Visual header" section, select "Conditional formatting."

4. Select "Background color" and choose a color to use when the user is not a member of the "Sensitive Data Group" security group.

5. Select "Add conditional formatting rule."

6. In the "Format by field value" section, choose the "IsMember" measure we created earlier.

7. In the "Format" section, select "Background color" and choose a color to use when the user is a member of the "Sensitive Data Group" security group.

8. Click "OK" to apply the conditional formatting.

Now, when a user views the report, the visual containing sensitive data will only be visible if they are a member of the "Sensitive Data Group" security group. If they are not a member of the group, the visual will be hidden.

The Identity.IsMemberOf function is a powerful tool for managing user access in Power BI reports. By checking whether a user is a member of a particular security group, we can control the visibility of sensitive data and ensure that only authorized users have access. By understanding the M code behind this function, we can gain a deeper insight into how it works and use it more effectively in our reports.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)