Forum Discussion

RWrightHvs's avatar
RWrightHvs
Regular Visitor
1 year ago
Solved

Power BI DAX Count Winners from a subset

Hi,

 

I have a challenge which has ended up substantially harder than I expected.

 

What I would like to do, for any 2 selected players is count the number of wins. IE the number of days where Player X had a lower [position] than Player Y.

 

My data is a set 'Gamedata' of daily competions in the format below. Ties are possible but the position is absolute and predetermined externally.

 

DateUser IDNameScorePosition
01/11/2024  1Player 1  2  4
01/11/2024  2Player 2  3  3
01/11/2024  3Player 3  5  2
01/11/2024  4Player 4  7  1
02/11/2024  1Player 1  4  2
02/11/2024  2Player 2  2  4
02/11/2024  3Player 3  3  3
02/11/2024  4Player 4  8  1
03/11/2024  1Player 1  4  3
03/11/2024  2Player 2  6  1
03/11/2024  3Player 3  2  4
03/11/2024  4Player 4  5  2
04/11/2024  1Player 1  3  4
04/11/2024  2Player 2  5  3
04/11/2024  3Player 3  7  1
04/11/2024  4Player 4  6  2

 

The dashboard I have created compares two players performance exclusive of all others. These are filtered from two unique, non-related tables in a distinct list of User IDs 'Player 1' && 'Player 2'. For the majority of calculations, the measure below restricts the calculations.

Show H2H = IF(SELECTEDVALUE('Gamedata'[user id]) = SELECTEDVALUE('Player 1'[user id])
|| SELECTEDVALUE('Gamedata'[user id]) = SELECTEDVALUE('Player 2'[user id]), 1, 0)
  • RWrightHvs ,

     

     

    To calculate the number of days where Player X (selected from Player 1) had a lower position than Player Y (selected from Player 2), we can use a DAX measure. Here's the approach to achieve this:

    DAX Measure for Wins

    We will compare the positions of the two selected players for each day and count the days where Player X's position is lower than Player Y's.

    Steps:

    1. Filter the data to only include the two selected players.
    2. Compare their positions for each day.
    3. Count the days where Player X's position is lower than Player Y.

    Here's the measure:

    Count of Wins = 
    VAR Player1ID = SELECTEDVALUE('Player 1'[User ID])
    VAR Player2ID = SELECTEDVALUE('Player 2'[User ID])
    
    -- Filter game data to only include the selected players
    VAR FilteredData = 
        FILTER(
            'Gamedata', 
            'Gamedata'[User ID] = Player1ID || 'Gamedata'[User ID] = Player2ID
        )
    
    -- Group by Date and compare positions
    VAR Wins =
        SUMX(
            SUMMARIZE(
                FilteredData,
                'Gamedata'[Date],
                "Player1Position", 
                    MAXX(FILTER(FilteredData, 'Gamedata'[User ID] = Player1ID), 'Gamedata'[Position]),
                "Player2Position", 
                    MAXX(FILTER(FilteredData, 'Gamedata'[User ID] = Player2ID), 'Gamedata'[Position])
            ),
            IF([Player1Position] < [Player2Position], 1, 0)
        )
    
    RETURN Wins
    
    • This measure assumes that both players participate on the same days. If there are dates where only one player competes, ensure your data handling accounts for these cases by adding appropriate filters or conditions.
    • The measure is dynamic and updates based on the players selected in your slicers.

    Result

    You can use this measure in a card visual or any other visualization to display the count of days where Player X had a lower position than Player Y.

     

    Best regards,

3 Replies

  • RWrightHvs ,

     

     

    To calculate the number of days where Player X (selected from Player 1) had a lower position than Player Y (selected from Player 2), we can use a DAX measure. Here's the approach to achieve this:

    DAX Measure for Wins

    We will compare the positions of the two selected players for each day and count the days where Player X's position is lower than Player Y's.

    Steps:

    1. Filter the data to only include the two selected players.
    2. Compare their positions for each day.
    3. Count the days where Player X's position is lower than Player Y.

    Here's the measure:

    Count of Wins = 
    VAR Player1ID = SELECTEDVALUE('Player 1'[User ID])
    VAR Player2ID = SELECTEDVALUE('Player 2'[User ID])
    
    -- Filter game data to only include the selected players
    VAR FilteredData = 
        FILTER(
            'Gamedata', 
            'Gamedata'[User ID] = Player1ID || 'Gamedata'[User ID] = Player2ID
        )
    
    -- Group by Date and compare positions
    VAR Wins =
        SUMX(
            SUMMARIZE(
                FilteredData,
                'Gamedata'[Date],
                "Player1Position", 
                    MAXX(FILTER(FilteredData, 'Gamedata'[User ID] = Player1ID), 'Gamedata'[Position]),
                "Player2Position", 
                    MAXX(FILTER(FilteredData, 'Gamedata'[User ID] = Player2ID), 'Gamedata'[Position])
            ),
            IF([Player1Position] < [Player2Position], 1, 0)
        )
    
    RETURN Wins
    
    • This measure assumes that both players participate on the same days. If there are dates where only one player competes, ensure your data handling accounts for these cases by adding appropriate filters or conditions.
    • The measure is dynamic and updates based on the players selected in your slicers.

    Result

    You can use this measure in a card visual or any other visualization to display the count of days where Player X had a lower position than Player Y.

     

    Best regards,

  • RWrightHvs's avatar
    RWrightHvs
    Regular Visitor

    Apologies for the formatting. The first row of the data should appear:

    Date = 2024-11-01

    User ID = 1

    Name = Player 1

    Score = 2

    Position = 4

  • what's the expected output based on the sample data you provided?