Forum Discussion

jay13's avatar
jay13
Regular Visitor
1 year ago
Solved

Countrows with filters

I am trying to count the rows for an ID where they only have a specific code.

 

Example: If I am looking to count the IDs that only have code D how would I count just that row. In this case the count would be 1 because only ID 3 has code D only.

IDcode
1M
1D
1L
2D
2M
3D
  • Hello jay13 

     

    this is your measure

    ContaSoloD =
    CALCULATE (
        DISTINCTCOUNT ( Tabella[ID] ),
        FILTER (
            VALUES ( Tabella[ID] ),
            CALCULATE ( COUNTROWS ( FILTER ( Tabella, Tabella[code] <> "D" ) ) ) = 0
        )
    )

    let me know if it works

9 Replies

  • Gabry's avatar
    Gabry
    Super User

    Hello jay13 

     

    this is your measure

    ContaSoloD =
    CALCULATE (
        DISTINCTCOUNT ( Tabella[ID] ),
        FILTER (
            VALUES ( Tabella[ID] ),
            CALCULATE ( COUNTROWS ( FILTER ( Tabella, Tabella[code] <> "D" ) ) ) = 0
        )
    )

    let me know if it works
    • jay13's avatar
      jay13
      Regular Visitor

      thank you for the reply. This seemed to work at first but if I add additional rows it is still only counting one?

       

      Example: It should count 3 since ID's 3,4 and 5 have code D only. 

      IDcode
      1M
      1D
      1L
      2D
      2M
      3D
      4D
      5D
      • Gabry's avatar
        Gabry
        Super User

        You're right. I changed the previous post with the updated formula. Let me know

  • SachinNandanwar's avatar
    SachinNandanwar
    Impactful Individual

    Just incase if you want to return ID's that only have one instance of "D"

    Table_1 = 
    VAR _Table = 
        SUMMARIZE(
            'Table',
            'Table'[ID],
            "Codes", DISTINCTCOUNT( ( 'Table'[code] )),
            "Codes_D",COUNTROWS( FILTER( 'Table', 'Table'[code] = "D" ))
        )
    
    RETURN  FILTER(_Table,[Codes]=[Codes_D])