Search code examples
powerbipowerquerym

PowerQuery - Count number of unique instances of data in aggregated / grouped data


I have the following data of events by players in a game. Using this data I would like to know how many times each player played for their team across the data set. When I group the data, I count the number of instances of the player column but that can be too high ie they have 3 events in a game but that should only count as them having played once. I can't find a way to get if they played or not per match and not all their events when grouping.

Desired result would be :

Team Player No of Times Played
AAA P1 3
AAA P2 2

As P1 played on 1/1, 1/2 and 1/4) As P2 only played on 1/1 and 1/2 and not 1/4)

Here is the source data:

Team Date Player Event
AAA 1/1/23 P1 Shoot
AAA 1/1/23 P2 Miss
AAA 1/1/23 P1 Pass
AAA 1/1/23 P3 Score
AAA 1/1/23 P5 Miss
AAA 1/1/23 P1 Shoot
AAA 1/2/23 P6 Shoot
AAA 1/2/23 P1 Miss
AAA 1/2/23 P3 Pass
AAA 1/2/23 P4 Miss
AAA 1/2/23 P7 Miss
AAA 1/2/23 P1 Shoot
AAA 1/4/23 P1 Score
AAA 1/4/23 P2 Shoot
AAA 1/4/23 P4 Miss
BBB 1/1/23 P1 Miss
BBB 1/1/23 P3 Miss
BBB 1/1/23 P1 Pass
BBB 1/1/23 P6 Score
BBB 1/3/23 P5 Miss
BBB 1/3/23 P3 Shoot
BBB 1/3/23 P2 Shoot
BBB 1/4/23 P1 Score
BBB 1/4/23 P3 Pass

Group by but counts the number of rows, not unique instances


Solution

  • Do a group and replace the code as follows:

    = Table.Group(#"Changed Type", {"Team", "Player"}, {{"Count", each List.Count( List.Distinct( _[Date])) , Int64.Type}})
    

    enter image description here