Forum Discussion

pbiuser12345's avatar
pbiuser12345
Helper I
3 years ago
Solved

Measure Behaviour in Matrix Visual

Hello,

 

I am creating a measure (expression at the very bottom of the post) to tell me the percentage of items that completed on time.  To that end, I have a calculated column that is basically [# yes] / ([# yes] + [# no].  However when I put this intro a matrix visual to validate the answer, this is what I ended up with:

 

This confuses me because I thought that the measure would be calculated independently for each row, so I would end up with 100% on the 'Yes' row, and 0% on the 'No' and 'N/A' rows.

 

I am a relative newbie to Power BI and DAX, so I assume I'm just missing something here.  Why does this not behave the way I think it should?  What changes would need to be made to make it work that way?

 

Thanks for your help!

% Programs Completed On Time =
    CALCULATE(
        DISTINCTCOUNT('Table - PROGRAMS'[ID]),
        && 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
        && 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] = "Yes"
    )
    /
    CALCULATE(
        DISTINCTCOUNT('Table - PROGRAMS'[ID]),
        && 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
        && 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] IN {"Yes", "No"}
    )

 

  • pbiuser12345 It's because you are trying to use CALCULATE with a single table. That tends not to work. Ditch CALCULATE can go with COUNTROWS( DISTINCT( SELECTCOLUMNS( FILTER(...), ... ) ) )

3 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    pbiuser12345 It's because you are trying to use CALCULATE with a single table. That tends not to work. Ditch CALCULATE can go with COUNTROWS( DISTINCT( SELECTCOLUMNS( FILTER(...), ... ) ) )

    • pbiuser12345's avatar
      pbiuser12345
      Helper I

      Thanks Greg, that put me on the right track.  The below expression is working the way I would have expected.

       

      I'm not sure I understand the 'why' of this though.  Are you able to elaborate or point me to something that explains why CALCULATE is bad on a single table?  Should I consider it best practice to reserve CALCULATE for measures that span multiple tables, and stick to more basic/fundamental functions for single table applications?

       

      % Programs Completed On Time = 
          COUNTROWS(
              FILTER(
                  'Table - PROGRAMS',
                  && 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
                  && 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] = "Yes"
              )
          )
          /
          COUNTROWS(
              FILTER(
                  'Table - PROGRAMS',
                  && 'Table - PROGRAMS'[PROGRAM_STATUS] = "Completed"
                  && 'Table - PROGRAMS'[PROGRAM_COMPLETED_ON_TIME] IN {"Yes", "No"}
              )
          )