powerbimicrosoft-graph-apimicrosoft-teamspowerappsmicrosoft-graph-teams

How to create chart in power apps which shows number of post done by team member in teams channel?


I want to create one chart which shows number of post by each team member in the channel. I am using power apps in teams. Chart should have number of post on y-axis and name of person on x-axis.


Solution

    1. In the Data tab of Power Apps, add the Microsoft Teams connector
    2. In the OnVisible property of your Screen control which has the chart, paste the following code:
    // Get Team's groupId
    UpdateContext(
        {
            varGroupId: LookUp(
                MicrosoftTeams.GetAllTeams().value,
                displayName = "Your Team's display name"
            ).id
        }
    );
    
    // Get Team's channelId
    UpdateContext(
        {
            varChannelId: LookUp(
                MicrosoftTeams.GetChannelsForGroup(varGroupId).value,
                displayName = "Your channel's display name"
            ).id
        }
    );
    
    // Get all messages from channel
    ClearCollect(
        RawChannelMessages,
        MicrosoftTeams.GetMessagesFromChannel(
            varGroupId,
            varChannelId
        ).value
    );
    
    // Get all distinct users from channel messages, init MessageCount column
    ClearCollect(
        DistinctUsers,
        AddColumns(
            Filter(
                Distinct(
                    RawChannelMessages,
                    from.user.displayName
                ),
                !IsBlank(ThisRecord.Value)
            ),
            MessageCount,
            0
        )
    );
    
    // Update distinct user list with their message counts
    ForAll(
        RawChannelMessages,
        With(
            {messageUserDisplayName: ThisRecord.from.user.displayName},
            With(
                {
                    existingUser: LookUp(
                        DistinctUsers,
                        Value = messageUserDisplayName
                    ),
                    existingUserMessageCount: LookUp(
                        DistinctUsers,
                        Value = messageUserDisplayName
                    ).MessageCount
                },
                Patch(
                    DistinctUsers,
                    existingUser,
                    {MessageCount: existingUserMessageCount + 1}
                )
            )
        )
    );
    

    Note: Be sure to change Your Team's display name and Your channel's display name to their respective values.

    This will create a table called DistinctUsers which has two columns. Value, which is the name of all distinct users who have made posts in your channel. MessageCount, which is the number of posts each distinct user has posted.

    Here is an example of DistinctUsers:

    MessageCount | Value
    1            | Jon, Doe
    3            | Al, Dente
    2            | First, Last
    
    1. In your Chart control, set the chart's Items property to DistinctUsers
    2. You might need to save and refresh your app to see results