Forum Discussion

TcT85's avatar
TcT85
Helper III
1 year ago
Solved

I need help with Distinct Count with excluding filter

Hi,

 

I have two tables, and I need to find the yield for each type.

The tables are fictional, the real table contains over 25 different names in the Type Column.

I only know how to use with including with filters.

Distinct count = CALCULATE(DISTINCTCOUNT('Defect table'[Serialnr] ), 'Defect table'[Type] = "Defect" )
Yield  = 1-  DIVIDE([Distinct count] ,SUM('Produced Table'[Quantity]))
 
How can i make a similar DAX formula with excluding filter?
I would like to exclude warning from this formula.
 
Defect table   
ProductSerialnrPartType
A0001CornDefect
A0001CornWarning
A0002CarrotWarning
A0002CornDefect
A0002CornDefect
A0002CornScratch
A0003CarrotDefect
A0004CabbageDefect
A0004CabbageWarning
B0006CarrotDefect
B0007CornWarning
B0008CarrotWarning
B0009CornDefect
B0010CarrotDefect
B0011CarrotDefect

 

Produced Table   
ProductQuantityDefectWarningScratch
A4431
B6420

 

This is the result I want.

 

Yield Table 
ProductYield
A0%
B33%
  • Hi TcT85 

    I included blank value in my table and now I get the following result

     

    To fix that, I have to add the modify my formula with:

    Yield =
    VAR TotalProduced =
        SUM('Produced Table'[Quantity])
    VAR DistinctDefects =
        CALCULATE(
            DISTINCTCOUNT('Defect table'[Serialnr]),
            FILTER(
                'Defect table',
                'Defect table'[Type] <> "Warning"
                && 'Defect Table'[Type] <> ""
            )
        )
    RETURN
        IF(
            TotalProduced > 0,
            1 - DIVIDE(DistinctDefects, TotalProduced),
            BLANK()
        )
    Because due to my test, the value was empty and not blank
    If it is not working, you have to replace
    && 'Defect Table'[Type] <> ""
    By
    && 'Defect Table'[Type] <>BLANK()
     

9 Replies

  • TcT85 , Create two measures

    DAX
    DistinctCountExcludingWarning =
    CALCULATE(
    DISTINCTCOUNT('Defect table'[Serialnr]),
    'Defect table'[Type] <> "Warning"
    )

     

    Yield =
    1 - DIVIDE(
    [DistinctCountExcludingWarning],
    SUM('Produced Table'[Quantity])
    )

  • Hi TcT85 

     

    The following measure will return the right result

     

    Yield =
    VAR TotalProduced =
        SUM('Produced Table'[Quantity])
    VAR DistinctDefects =
        CALCULATE(
            DISTINCTCOUNT('Defect table'[Serialnr]),
            FILTER(
                'Defect table',
                'Defect table'[Type] <> "Warning"
            )
        )
    RETURN
        IF(
            TotalProduced > 0,
            1 - DIVIDE(DistinctDefects, TotalProduced),
            BLANK()
        )

     

  • Hi TcT85 

    Please follow the below steps

    1. Create measures using below DAX.

        

    Distinct Defect Count (Excl Warning) =
    CALCULATE(
        DISTINCTCOUNT('Defect table'[Serialnr]),
        FILTER(
            'Defect table',
            'Defect table'[Type] <> "Warning"
        )
    )

    2. Create second measures using below DAX.

        
    Distinct Defect Count (Excl Warning) =
    CALCULATE(
        DISTINCTCOUNT('Defect table'[Serialnr]),
        FILTER(
            'Defect table',
            'Defect table'[Type] <> "Warning"
        )
    )
     
     
     
    3. Here is result.

     


    If this answers your question, kindly accept it as a solution and give kudos.
  • TcT85's avatar
    TcT85
    Helper III

    Hi bhanu_gautam , mdaatifraza5556  and mdaatifraza5556 

     

    Thanks for the quick response all of your solution works, but I have an issue that even blank cells from Type column are included in the calculation.

     

    Can anyone of you help me with a dax formula that also exclude the blanks?

    • bhanu_gautam's avatar
      bhanu_gautam
      Super User

      TcT85 , Try this

       

      DAX
      DistinctCountExcludingWarningAndBlanks =
      CALCULATE(
          DISTINCTCOUNT('Defect table'[Serialnr]),
          'Defect table'[Type] <> "Warning",
          'Defect table'[Type] <> BLANK()
      )
       
      And then 
      DAX
      Yield =
      1 - DIVIDE(
          [DistinctCountExcludingWarningAndBlanks],
          SUM('Produced Table'[Quantity])
      )
    • Cookistador's avatar
      Cookistador
      Super User

      Hi TcT85 

      I included blank value in my table and now I get the following result

       

      To fix that, I have to add the modify my formula with:

      Yield =
      VAR TotalProduced =
          SUM('Produced Table'[Quantity])
      VAR DistinctDefects =
          CALCULATE(
              DISTINCTCOUNT('Defect table'[Serialnr]),
              FILTER(
                  'Defect table',
                  'Defect table'[Type] <> "Warning"
                  && 'Defect Table'[Type] <> ""
              )
          )
      RETURN
          IF(
              TotalProduced > 0,
              1 - DIVIDE(DistinctDefects, TotalProduced),
              BLANK()
          )
      Because due to my test, the value was empty and not blank
      If it is not working, you have to replace
      && 'Defect Table'[Type] <> ""
      By
      && 'Defect Table'[Type] <>BLANK()
       
    • mdaatifraza5556's avatar
      mdaatifraza5556
      Super User

      Hi TcT85 

      Include one more filtering part in the filter.

      'Defect table'[Type] <> blank()

      If this answers your questions, kindly accept it as a solution.

  • v-achippa's avatar
    v-achippa
    Community Support

    Hi TcT85,

     

    Thank you for reaching out to Microsoft Fabric Community.

     

    Thank you bhanu_gautammdaatifraza5556Cookistador for the prompt response.

     

    As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided by the super user's resolved your issue?
    If our super user's response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

     

    Thanks and regards,

    Anjan Kumar Chippa

    • v-achippa's avatar
      v-achippa
      Community Support

      Hi @TcT85,

       

      We wanted to kindly follow up to check if the solution provided by the super user's resolved your issue?

      If our super user's response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

       

      Thanks and regards,

      Anjan Kumar Chippa