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
adjohnson2
Helper II
Helper II

Having Clause or something close

I would like for my measure to only count the accounts that have more than n case. 

 

This is what I've tried so far.

Qualified Customer =
CALCULATE(COUNT('Customer Info'[ACCOUNT#]), FILTER('Customer Count', COUNT('Customer Info'[CASE]) > 6))

 

This pretty much ignores my FILTER() and does a normal Count() of the accounts.

 

I've also tried 

Qualified_Customers =
var Qualified_Customers = CALCULATETABLE(VALUES('Customer Info'[ACCOUNT_NBR]), FILTER('Customer Info', COUNT('Customer Info'[CASE_ID]) > 6))
return CALCULATE(COUNT('Customer Info'[ACCOUNT_NBR]), 'Customer Info'[ACCOUNT_NBR] in Qualified_Customers)

 

This gives me an error message saying it ran out of avaiable memory or does a normal Count() of the accounts.

 

Last I tried 

Qualified_Customer = 
var numberSelect = 6
VAR CEMIn_Customers =
    CALCULATETABLE (
        VALUES ( 'Customer Info'[ACCOUNT#] ),
        ALLSELECTED ( Outages ),
        VALUES ( 'Customer Info'[ACCOUNT#] ),
        FILTER( 'Customer Info', COUNT('Customer Info'[CASE_ID]) >6)
    )
var custTotal = CALCULATE (
        COUNT ( 'Customer Info'[ACCOUNT#] ),
        ALLSELECTED ( 'Customer Info' ),
        'Customer Info'[ACCOUNT#] IN CEMIn_Customers)
RETURN
       CALCULATE (
            CALCULATE(COUNT('Customer Info'[ACCOUNT#])),
            FILTER('Customer Info', custTotal >= numberSelect)
        )

 

This gives me an error message saying it ran out of avaiable memory.

 

I'm all out of ideas, any help would be greatly appreciated. 

1 ACCEPTED SOLUTION
d_gosbell
Super User
Super User

So you should be able to do this with a measure like the following

Qualified Customer = 
COUNTROWS(
    FILTER(
        VALUES('Customer Info'[Account_NBR]), -- gets a distinct list of account_nbr
        CALCULATE(                            -- forces a context transition so that Case_ID is
                                              -- filtered for just those under the current Account_nbr        
            COUNT('Customer Info'[CASE_ID])
        ) > 6
    )
)

 

If you had a measure that counted case_ids

Case Count = COUNT('Customer Info'[CASE_ID])

 

Then you could simplify this to remove the call to calculate (as measures are wrapped in an implied calculate )

Qualified Customer = 
COUNTROWS(
    FILTER(
        VALUES('Customer Info'[Account_NBR]),   -- gets a distinct list of account_nbr
        [Case Count] > 6      
    )
)

View solution in original post

10 REPLIES 10
d_gosbell
Super User
Super User

So you should be able to do this with a measure like the following

Qualified Customer = 
COUNTROWS(
    FILTER(
        VALUES('Customer Info'[Account_NBR]), -- gets a distinct list of account_nbr
        CALCULATE(                            -- forces a context transition so that Case_ID is
                                              -- filtered for just those under the current Account_nbr        
            COUNT('Customer Info'[CASE_ID])
        ) > 6
    )
)

 

If you had a measure that counted case_ids

Case Count = COUNT('Customer Info'[CASE_ID])

 

Then you could simplify this to remove the call to calculate (as measures are wrapped in an implied calculate )

Qualified Customer = 
COUNTROWS(
    FILTER(
        VALUES('Customer Info'[Account_NBR]),   -- gets a distinct list of account_nbr
        [Case Count] > 6      
    )
)

I do have one more question. I created the DAX below and got the same outcome as you. However, I like how much more simple yours is. Both of our DAX formulas are coming up with incorrect total rows.

 

Qualified Customer = 
var CEMIn = SELECTEDVALUE(CEMI_Threshold[CEMIn], 6)
var cust_table = 
GROUPBY(
FILTER (
    SUMMARIZE (
        'Customer Info',
        'Customer Info'[Account_NBR],
        "Count records", COUNTX('Customer Info', COUNT('Customer Info'[CASE_ID]))
    ),
    [Count records] >= CEMIn
), 'Customer Info'[ACCOUNT_NBR])
return CALCULATE(DISTINCTCOUNT('Customer Info'[ACCOUNT_NBR]), 'Customer Info'[ACCOUNT_NBR] in cust_table)

 

 

 

Incorrect Totals.png

Do you have any accounts that have cases with different legal entities? For example if an account had 4 cases with legal entity 1 and 4 with legal entity 2 they will fall under the threashold when split by legal entity, but at the total level they will have 8 cases so will qualify there.

No, there shouldn't be any accounts with multiple legal entities. 


@adjohnson2 wrote:

No, there shouldn't be any accounts with multiple legal entities. 


So shouldn't seems a bit vague - do you mean "in theory this should not happen" or "I've double checked and this is definitely not the case"?

 

Assuming this is not the case I'm not sure what else could cause the higher total count. I tried mocking up a simple model and I can't make it behave like your screenshot. I'm wondering if there is some bi-directional relationship in your model or some filter on the visual that is causing this. Can you create a test file with some dummy data and reproduce this issue? If so it would help if you could post such a file back to this thread. 

No need to wait until tomorrow, I was too eager to find out. So there are cases where the account number is in multiple legal entities. 


@adjohnson2 wrote:

No need to wait until tomorrow, I was too eager to find out. So there are cases where the account number is in multiple legal entities. 


So that sounds like it probably explains this issue then and it's either a data quality issue that needs cleaning up or maybe just a fact of life that users need to be trained on when using this metric.

I got the 'Total' row to calculate correctly. It was a user limited knowledge error. There are two account# fields, the one I didn't use in my measure changed as the legal entity changed. So everything is good now.

Please be on tomorrow, I'll have a more definite answer then.

Greg_Deckler
Super User
Super User

What does your source data look like? Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.