Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
busarch
Frequent Visitor

CALCULATE with IN, NOT, OR, AND

Hello,

I'm trying to get a conditional count of items.

The scenario is: I have a data set with customer complaints about products. The dataset is simple 3 column spreadsheet

1) customerID 2) ProductName 3) ComplaintDate

 

There are 13 products A,B,C,D.....M

 

I'd like to calculate two things

1) Number of customers that call about product A or B or C but not X or Y or

2) Number of customers that call about product A and B and C but not X and Y and Z

 

If I could make up a syntax for this, it will look similar to: 

1) CALCULATE(DISTINCTCOUNT(CustomerID),AND(Product IN ANY ("A","B","C"),  NOT ANY ("X","Y","Z"))

2) CALCULATE(DISTINCTCOUNT(CustomerID),AND(Product IN ("A","B","C"),  NOT("X","Y","Z"))

 

What I have tried so far.

 
VAR Called = 'A,B,C'
VAR DidntCall= X,Y,Z
RETURN
CALCULATE(DISTINCTCOUNT([CustomerID]),AND([Product] IN {Called},NOT([Product] IN {DidntCall})))
 

 

Thank you!!!

4 REPLIES 4
v-lili6-msft
Community Support
Community Support

HI @busarch 

For your case, just try this logic:

1)

 

Measure 1 = 
CALCULATE (
    DISTINCTCOUNT ( 'Table'[customerID] ),
    FILTER (
        'Table',
         AND (
            'Table'[ProductName] = "A" || 'Table'[ProductName] = "B"  || 'Table'[ProductName] = "C",
            'Table'[ProductName] <> "X"  || 'Table'[ProductName] <> "Y" || 'Table'[ProductName] <> "Z"
         )))

 

2)

use this logic (this exclude customerid that not call "X""Y""Z" one or more of them)

 

Measure 2 = 
CALCULATE (
    DISTINCTCOUNT ( 'Table'[customerID] ),
    FILTER (
        'Table',
         AND(
            "A" in VALUES('Table'[ProductName]) &&  "B" in VALUES('Table'[ProductName]) &&  "C" in VALUES('Table'[ProductName]),
            NOT("X") in VALUES('Table'[ProductName])   &&   NOT("Y") in VALUES('Table'[ProductName])   &&  NOT("Z") in VALUES('Table'[ProductName])  
           
      )))

 

or use this logic (this only exclude customerid that not call "X""Y""Z" together)

 

Measure 2 = 
CALCULATE (
    DISTINCTCOUNT ( 'Table'[customerID] ),
    FILTER (
        'Table',
         AND(
            "A" in VALUES('Table'[ProductName]) &&  "B" in VALUES('Table'[ProductName]) &&  "C" in VALUES('Table'[ProductName]),
            NOT("X") in VALUES('Table'[ProductName])   ||   NOT("Y") in VALUES('Table'[ProductName])   ||  NOT("Z") in VALUES('Table'[ProductName])  
           
      )))

 

 

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

This is awesome.

Is it possible to incorporate variables into this? That way the "product they called about" and "Product they DID NOT call about" can be variable and controled by a user via a slicer. (I've created these slicers and set the VAR = slicer result (comma delimited)

 

I've set up a a comma delimited variable tied to a slicer for both the 

 
"CALCULATE(DISTINCTCOUNT([CustomerID]),AND([Product] IN {Called},NOT([Product] IN {DidntCall})))"
 

hi @busarch 

Yes, you could try this way:

First, you must have two separate dim productname tables for slicer: In ProductName / NOT IN ProductName

1)

New Measure 1 = 
VAR _customerID=CALCULATETABLE(VALUES('Table'[customerID]),'Table'[ProductName] IN VALUES('NOT In ProductName'[ProductName])) RETURN
CALCULATE (
    DISTINCTCOUNT ( 'Table'[customerID] ),
    FILTER (
        'Table',
         AND (
            'Table'[ProductName] in  VALUES('In ProductName'[ProductName]) ,
             NOT('Table'[customerID]) IN _customerID
         )))

2)

New Measure 2 = 
VAR __customerIDIN=CALCULATETABLE(VALUES('Table'[customerID]),'Table'[ProductName] IN VALUES('In ProductName'[ProductName]))
VAR __customerIDNOTIN=CALCULATETABLE(VALUES('Table'[customerID]),'Table'[ProductName] IN VALUES('NOT In ProductName'[ProductName])) RETURN
CALCULATE (
    DISTINCTCOUNT ( 'Table'[customerID] ),
    FILTER (
        'Table',
        AND(
         NOT('Table'[customerID]) IN __customerIDIN,
         NOT('Table'[customerID]) IN __customerIDNOTIN
      )))

OR

New Measure 2 = 
VAR __customerIDIN=CALCULATETABLE(VALUES('Table'[customerID]),'Table'[ProductName] IN VALUES('In ProductName'[ProductName]))
VAR __customerIDNOTIN=CALCULATETABLE(VALUES('Table'[customerID]),'Table'[ProductName] IN VALUES('NOT In ProductName'[ProductName])) RETURN
CALCULATE (
    DISTINCTCOUNT ( 'Table'[customerID] ),
    FILTER (
        'Table',
        AND(
         NOT('Table'[customerID]) IN __customerIDIN,
         NOT('Table'[ProductName]) IN VALUES('NOT In ProductName'[ProductName])
      )))

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
TomMartens
Super User
Super User

Hey @busarch ,

 

please provide a pbix file that contains sample data, but still reflects your data model. Upload the pbix and/or xlsx file(s) to onedrive or dropbox and share the link.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.