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

DISTINCTCOUNT of ID WHERE a previous condition is TRUE

I have a Table (Table)

DateTimeIDStatus_AEntryExit
25/10/2020 14:251001A  
29/10/2020 14:251001 10
1/12/2020 15:301002 10
5/02/2021 12:001001 01
15/04/2021 21:451002 01


The table records status changes of various types by DateTime.

I want to calculate the number (distinct count of clients) who have exited (exit = 1) where the client has previously had a Status_A (Status_A = 1)

Something like:

 

 

 

 

Measure = 
VAR select_id = SELECTEDVALUE('Table'[ID])
VAR temp_table = FILTER('Table', 'Table'[Status_A] = "A")
RETURN
IF(
    CONTAINS(temp_table, 'Table'[ID], select_id),
    CALCULATE(
        DISTINCTCOUNT('Table'[ID]), 
        'Table'[Exit] = 1),
    BLANK()
)

 

 

 

 


There is 1 client (ID=1001) who has exited (Exit = 1) and who previously had a Status A (Status_A = "A") so I expect Measure = 1.
However, Measure = BLANK

Oddly, when I create a table with ID and Measure the data looks correct, except for the Total

moosepng_1-1621398104659.png

 


Why is the total not 1? My guess is the data type.
Is there a correct way to structure the IF statement in my measure to return a value that can be totaled/averaged etc.?

I've looked at the Microsoft documentation, but I can't find a way to strucutre the IF statement to return a number that can be calculated into a total.

1 ACCEPTED SOLUTION

Hi @moosepng ,

 

Try the following formula:

 

Measure = 
var exit_value = 
CALCULATE(
    MAX('Table'[Exit]),
    FILTER(
        ALL('Table'),
        'Table'[ID] = MAX('Table'[ID])
    )
)
return 
CALCULATE(
    DISTINCTCOUNT('Table'[ID]),
    FILTER(
        'Table',
        exit_value = 1
        && 'Table'[Status_A] = "A"
    )
)

image.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

View solution in original post

7 REPLIES 7
moosepng
Helper II
Helper II

That works :), but it's not ideal.
I have to create a number of measures with this logic and this would double the number of measures in my report.
Do you know what the difference is?

If I include your solution in the orignal measure it has the same problem.. is it a bug?

Measure test fix 2 = 
VAR select_id = SELECTEDVALUE('Table'[ID])
VAR temp_table = FILTER('Table', 'Table'[Status_A] = "A")
RETURN
IF(
    CONTAINS(temp_table, 'Table'[ID], select_id),
    SUMX( VALUES('Table'[ID]), 
        CALCULATE(
            DISTINCTCOUNT('Table'[ID]), 
            'Table'[Exit] = 1)
    ),
    BLANK()
)

Hi @moosepng ,

 

Try the following formula:

 

Measure = 
var exit_value = 
CALCULATE(
    MAX('Table'[Exit]),
    FILTER(
        ALL('Table'),
        'Table'[ID] = MAX('Table'[ID])
    )
)
return 
CALCULATE(
    DISTINCTCOUNT('Table'[ID]),
    FILTER(
        'Table',
        exit_value = 1
        && 'Table'[Status_A] = "A"
    )
)

image.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

How would it work with text values in 'Table'[Exit] ?

DateTimeIDStatus_AEntryExit
25/10/2020 14:251001A  
29/10/2020 14:251001 YesNo
1/12/2020 15:301002 YesNo
5/02/2021 12:001001 NoYes
15/04/2021 21:451002 NoYes

Hi @moosepng ,

 

If there are only two texts: "Yes" and "No", then just change "1" to "Yes" in the formula.

Because when comparing "Yes" and "No", it will be based on the order of the first letter, that is, "Yes" > "No".

 

Measure = 
var exit_value = 
CALCULATE(
    MAX('Table'[Exit]),
    FILTER(
        ALL('Table'),
        'Table'[ID] = MAX('Table'[ID])
    )
)
return 
CALCULATE(
    DISTINCTCOUNT('Table'[ID]),
    FILTER(
        'Table',
        exit_value = "Yes"
        && 'Table'[Status_A] = "A"
    )
)

 

Best Regards,
Winniz

moosepng_0-1621584555503.png

 

Yep that looks like it gives the right result.
thanks

moosepng_0-1621578222392.png

 

Jihwan_Kim
Super User
Super User

Hi, @moosepng 

Please try the below.

 

Picture7.png

 

Measure Total Fix =
SUMX( VALUES('Table'[ID]), [Measure])
 
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


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.