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
Anonymous
Not applicable

count unique values in two tables with date slicer

I have two identical tables (PP1 and PP2) - there is a relationship on PP1 on 'start date' so that it can be used with the date slicer and a relationship on PP2 with the 'end date' so that it can be used with the date slicer.

My issue is that I need a distinct count of 'App ref' where the dates fall between the slicer dates - to include all of those with a 'start date' between those dates and all of those with an 'end date' between those dates but so that it does not count duplicates.

I've thought about adding both counts together and then creating a measure for 'App ref' where 'start date' and 'end date' are only within the slicer boundaries (i.e: this would count the duplicates) and then minus this from the original count of all, but I can't even seem to get my head around how that would work because the date range is dictated by a slicer not a set range.

There's probably a simple explanation to this, but I can't seem to see the wood for the trees. Please help 

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

vchenwuzmsft_0-1649997370004.png

A table named slicer should be created via Slicer = CALENDAR(min('PP1'[Creation Date]),MAX('PP2'[Completion Date])). And no relationship created between these table. This step is uesd to remove filter on table pp1 and pp2. We use the slicer to send parameters to the final measure rather than the filter which comes from relationship.

 

 

final Measure =
VAR _pp1 =
    CALCULATETABLE (
        VALUES ( PP1[App Ref] ),
        FILTER ( 'PP1', [Creation Date] IN VALUES ( Slicer[Date] ) )
    )
VAR _pp2 =
    CALCULATETABLE (
        VALUES ( PP2[App Ref] ),
        FILTER ( 'PP2', [Completion Date] IN VALUES ( Slicer[Date] ) )
    )
VAR _duplicates =
    COUNTROWS ( EXCEPT ( _pp1, _pp2 ) )
RETURN
    COUNTROWS ( _pp2 ) + _duplicates

 

 

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

8 REPLIES 8
Anonymous
Not applicable

sample data.png

Hi @Anonymous ,

 

vchenwuzmsft_0-1649997370004.png

A table named slicer should be created via Slicer = CALENDAR(min('PP1'[Creation Date]),MAX('PP2'[Completion Date])). And no relationship created between these table. This step is uesd to remove filter on table pp1 and pp2. We use the slicer to send parameters to the final measure rather than the filter which comes from relationship.

 

 

final Measure =
VAR _pp1 =
    CALCULATETABLE (
        VALUES ( PP1[App Ref] ),
        FILTER ( 'PP1', [Creation Date] IN VALUES ( Slicer[Date] ) )
    )
VAR _pp2 =
    CALCULATETABLE (
        VALUES ( PP2[App Ref] ),
        FILTER ( 'PP2', [Completion Date] IN VALUES ( Slicer[Date] ) )
    )
VAR _duplicates =
    COUNTROWS ( EXCEPT ( _pp1, _pp2 ) )
RETURN
    COUNTROWS ( _pp2 ) + _duplicates

 

 

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

Anonymous
Not applicable

Amazing! Thank-you so much.

I had to do some additional tweaks to make this bespoke to my dashboard, but this works perfectly. 

AllisonKennedy
Super User
Super User

@Anonymous  Why do you have two fact tables? (the PP1 and PP2)? Usually you would have two date dimension tables instead or even one date table with NO relationship to PP table and use a DAX approximate lookup: 

https://excelwithallison.blogspot.com/2020/06/dax-approximate-lookup.html 


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

vojtechsima
Memorable Member
Memorable Member

Hi, @Anonymous
Something like this?

vojtechsima_0-1649752273892.png

Measure = 

var PP1_ = DISTINCTCOUNT(PP1[StartDate])
var PP2_ = DISTINCTCOUNT(PP2[EndDate])
return PP1_+PP2_
Anonymous
Not applicable

so my problem is that I need to count the distinct number of App Refs in these tables.

PP1 has 100 App Refs with a start date between the slicer dates, PP2 has 150 App Refs with an end date between the slicer dates, but 75 App Refs are counted in both tables. Adding PP1 and PP2 together gives me 250, but really my answer should be 175.

Hi, @Anonymous 
Could you please share a sample dataset and desired Count, please?
(Copyable table)

Anonymous
Not applicable

shared above - not sure why it didn't let me reply directly.

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.