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

DAX help... Count of distinct people except under a condition

I am hoping someone may know of a performant solution to my problem.

 

Sample data:

dim_user

user_id
1


fact_user_types

user_idtypestartdateenddatestatus
1A2/1/20221/1/2100On Hold
1B3/2/20223/5/2022Active
1C3/5/20221/1/2100Active

 

Filters:

  • Date Range
  • Type

 

We are looking for the following:

  1. Distinct count of user_ids that were Active at any point in the selected Date Range
  2. Discount count of user_ids that were On Hold at any point in the selected Date Range
  3. Table with distinct user_id and "true" status

 

Here's the kicker...

If a user_id has an On Hold record AND an Active record in the same date range, we need to exclude them from the On Hold count.  A user should never counted in both the Active and On Hold counts. Active will always trump On Hold.

 

Using the above example, these are the expected results for different scenarios:

 

Scenario 1:

Date Range 3/1/2022 - 3/31/2022, All Types

Active = 1   

On Hold = 0 * 

*even though the user has an on hold record, the fact that they also have an active record eliminates them from this count

 

Scenario 2:

Date Range 3/1/2022 - 3/31/2022, Type A Only

Active = 0

On Hold = 1

 

What I have so far are 2 measures for isActive and isOnHold. This gives me a value for each record. 

 

isActive =
VAR _MinDate = MIN(dim_date[full_date])
VAR _MaxDate = MAX(dim_date[full_date])
RETURN
   SWITCH (
      TRUE(),
              MIN(view_fact_user_membership_state[effective_from_date]) <= _MaxDate
              && MAX(view_fact_user_membership_state[effective_to_date]) >= _MinDate
              && MIN(view_fact_user_membership_state[current_membership_state]) = "Active"
      , 1, 0)

 

isOnHold is the same but with "On Hold"

 

So, when the Date Range is 3/1/2022 - 3/3/2022, I see this:

 

idtypestartdateenddatestatusisActiveisOnHold
1A2/1/20221/1/2100On Hold01
1B3/2/20223/5/2022Active10
1C3/5/20221/1/2100Active00

 

What I'd like is 1 record per user_id:

idstatus
1Active

 

and 2 cards:

joyhackett_0-1647308298642.png

 

Thank you in advance for any assistance! 🙂 Joy

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

This is great! Thank you so much. I see how you added a  column = {Number.From([startdate])..Number.From([enddate])} in the fact table. I'm a little concerned that it will make for a very very large sized table, but maybe it's better to store that data in memory rather than calculate the to/from on the report. I will experiment with this approach. Thanks again!!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
joyhackett
Helper II
Helper II

Oops - I c/p the actual dax for isActive instead of the simplified version with table/columns from my sample: 

 

isActive =
VAR _MinDate = MIN(dim_date[full_date])
VAR _MaxDate = MAX(dim_date[full_date])
RETURN
SWITCH (
TRUE(),
MIN(fact_user_types[startdate]) <= _MaxDate
&& MAX(fact_user_types[enddate]) >= _MinDate
&& MIN(fact_user_types[status]) = "Active"
, 1, 0)

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.