Search code examples
powerbidaxpowerbi-desktoppowerbi-custom-visuals

Power Bi Matrix not calculating "Total" properly when creating DAX measure with "if" statement


I have a data set that has revenue and base salary by month. I am attempting to put this in a Power Bi Matrix with a DAX measure that calculates by month if the revenue is less than the salary then it is $0 earned and not negative. The issue I'm running into is the "Total" section on the Matrix is not accounting for the logic by month and instead taking total revenue less total salary.

The DAX expression I wrote is:

Bonus Earned Calculation = 
  IF(
    SUM(Example[Revenue])-SUM(Example[Base Salary]) < 0,
    0,
    SUM(Example[Revenue])-SUM(Example[Base Salary])
  )

Is there a way to write DAX so the Matrix Total (that calculates automatically) is correct?

enter image description here

I tried tweaking the

Bonus Earned Calculation = 
  IF(
    SUM(Example[Revenue])-SUM(Example[Base Salary]) < 0,
    0,
    SUM(Example[Revenue])-SUM(Example[Base Salary])
  )

formula but I am not quite adept enough to write something that affects the Matrix Total. Any help or ideas would be greatly appreciated! I can also send the example dashboard.


Solution

  • You will need to wrap it in a SUMX.

    Bonus Earned Calculation = 
      SUMX(
        YourDateTable,
        MAX( 0, SUM(Example[Revenue]) - SUM(Example[Base Salary]) )
      )
    

    Tip:
    Your IF is calculating twice and this is a waste. Instead use variables to help:

      var x = SUM(Example[Revenue]) - SUM(Example[Base Salary])
      return IF(x < 0, 0, x)
    

    Or as above, you can use MAX(0, xxx).