Forum Discussion

am-person's avatar
am-person
Regular Visitor
2 years ago
Solved

Subtracting Averages from a table across columns

I'm wrecking my brain trying to figure out something that seems simple.  I have a data set that has a group of teams from which I have generated a measure for the team Avg that I can display in the following table:

TeamOpponentTeam AvgAvg Diff
AX1-9
BY5-6
CZ131
XA109
YB116
ZC12-1

However, I am having extreme difficulty subtracting the Team average across the columns and generating values for "Avg Diff".  For Example, Team A Avg = 1 subtract Team X Avg = 10 for the Avg Diff = -9.  Any help would be greatly appreciated!

  • am-person I could be wrong but this is what I think the result should be:

     

    Avg = AVERAGE ( 'Team'[pts] )
    
    Team Avg = 
    CALCULATE (
        [Avg],
        ALLSELECTED ( 'Team'[opponent] )
    )
    
    Avg Diff = 
    VAR vTeamAvg =
        [Team Avg]
    VAR vOpponentAvg =
        CALCULATE (
            [Team Avg],
            ALL ( 'Team'  ),
            TREATAS ( VALUES ( 'Team'[Opponent] ), 'Team'[Team] )
        )
    VAR vResult = vTeamAvg - vOpponentAvg
    RETURN
        vResult

7 Replies

  • am-person could you share what your raw data looks like and also what measures you are using for average?

    • am-person's avatar
      am-person
      Regular Visitor

      Raw data file is rather large but here's the gist of it:

      gameteamopponentpts
      1AX1
      1BY2
      1CZ3
      1XA4
      1YB5
      1ZC6
      2AY7
      2BZ8
      2CX9
      2XC10
      2YA1
      2ZB2
      3AZ3
      3BX4
      3CY5
      3XB6
      3YC7
      3ZA8

      I am using a measure:

      Team Avg = AVERAGE('Table'[pts])

  • am-person,

     

    Try this measure. It uses TREATAS to change the lineage of Opponent to Team.

     

    Avg Diff = 
    VAR vTeamAvg =
        MAX ( 'Table'[Team Avg] )
    VAR vOpponentAvg =
        CALCULATE (
            MAX ( 'Table'[Team Avg] ),
            ALL ( 'Table' ),
            TREATAS ( VALUES ( 'Table'[Opponent] ), 'Table'[Team] )
        )
    VAR vResult = vTeamAvg - vOpponentAvg
    RETURN
        vResult

     

     

    • am-person's avatar
      am-person
      Regular Visitor

      This doesn't work as my Team average is calculated as a measure and the MAX function only accepts column references

      • DataInsights's avatar
        DataInsights
        Icon for Super User rankSuper User

        am-person,

         

        Replace the column references with the measure Team Avg:

         

        Avg Diff = 
        VAR vTeamAvg =
            [Team Avg]
        VAR vOpponentAvg =
            CALCULATE (
                [Team Avg],
                ALL ( 'Table' ),
                TREATAS ( VALUES ( 'Table'[Opponent] ), 'Table'[Team] )
            )
        VAR vResult = vTeamAvg - vOpponentAvg
        RETURN
            vResult
  • am-person I could be wrong but this is what I think the result should be:

     

    Avg = AVERAGE ( 'Team'[pts] )
    
    Team Avg = 
    CALCULATE (
        [Avg],
        ALLSELECTED ( 'Team'[opponent] )
    )
    
    Avg Diff = 
    VAR vTeamAvg =
        [Team Avg]
    VAR vOpponentAvg =
        CALCULATE (
            [Team Avg],
            ALL ( 'Team'  ),
            TREATAS ( VALUES ( 'Team'[Opponent] ), 'Team'[Team] )
        )
    VAR vResult = vTeamAvg - vOpponentAvg
    RETURN
        vResult