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
PBI_newuser
Post Prodigy
Post Prodigy

Count items in table A if filter date from table B falls between start & end date from table B

How to count the distinct serial number in table A if filter date from table B falls between start & end date from table B?

I want to calculate the total count of serial number of Table B + count of serial number of Table A (fall between the selected slicer Created Date) excluding serial number count of Table B.

Please help!!!! Thanks.

For example:
If I select slicer "Created Date" = 10/1/2019 to 4/1/2020.

Count of Serial Number in Table B= 5

Count of Serial Number in Table A

= 3 (Start & End Date within "Created Date") - 2 (appear in Table 2) = 1

 

Table A:

TypeSerial NumberStart DateEnd Date
AA-1235/1/20194/30/2020
AB-1237/1/20196/30/2020
AC-1232/1/20181/31/2019
AD-1238/1/20197/31/2020
AE-1234/1/20183/31/2019

 

Table B:

TypeSerial NumberCreated Date
AB-1231/24/2020
AA-1238/13/2019
AH-1234/1/2020
BX-32112/23/2019
BZ-32110/12/2019

 

1 ACCEPTED SOLUTION

Hi @PBI_newuser ,

 

Please try this:

Serial Number in TA 1 =
VAR t =
    FILTER (
        TableA,
        TableA[Start Date] <= MIN ( 'Created date from table B'[Created Date] )
            && TableA[End Date] >= MAX ( 'Created date from table B'[Created Date] )
    )
RETURN
    COUNTROWS ( t )
Serial Number in TB 1 =
VAR t =
    FILTER (
        TableA,
        TableA[Start Date] <= MIN ( 'Created date from table B'[Created Date] )
            && TableA[End Date] >= MAX ( 'Created date from table B'[Created Date] )
    )
RETURN
    COUNTROWS ( FILTER ( t, [Serial Number] IN VALUES ( TableB[Serial Number] ) ) )

 

Best Regards,
Xue Ding
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
v-xuding-msft
Community Support
Community Support

Hi @PBI_newuser ,

 

Please try this:

Created date from table B = VALUES(TableB[Created Date])
selected date = SELECTEDVALUE('Created date from table B'[Created Date])
Serial Number in TA = 
var t = FILTER(TableA,TableA[Start Date]<= [selected date] && TableA[End Date] >= [selected date])
return
COUNTROWS(t)
Serial Number in TB = 
var t = FILTER(TableA,TableA[Start Date]<= [selected date] && TableA[End Date] >= [selected date])
return
COUNTROWS(FILTER(t,[Serial Number] in VALUES(TableB[Serial Number])))

v-xuding-msft_1-1594802880202.png

 

 

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

@v-xuding-msft ,thanks!
How about multiple selection of created date?
It only works for single selection now but i need date range selection for created date.

 

Thanks.

Hi @PBI_newuser ,

 

Please try this:

Serial Number in TA 1 =
VAR t =
    FILTER (
        TableA,
        TableA[Start Date] <= MIN ( 'Created date from table B'[Created Date] )
            && TableA[End Date] >= MAX ( 'Created date from table B'[Created Date] )
    )
RETURN
    COUNTROWS ( t )
Serial Number in TB 1 =
VAR t =
    FILTER (
        TableA,
        TableA[Start Date] <= MIN ( 'Created date from table B'[Created Date] )
            && TableA[End Date] >= MAX ( 'Created date from table B'[Created Date] )
    )
RETURN
    COUNTROWS ( FILTER ( t, [Serial Number] IN VALUES ( TableB[Serial Number] ) ) )

 

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

Hi @v-xuding-msft,
How do I calculate the distinct count of Serial number which their start and end date has touch the Created Date range I selected?
 

Below is the example, E and F should be excluded from the count.

 

Capture.PNG

Thanks @v-xuding-msft !! it works!

amitchandak
Super User
Super User

@PBI_newuser , create a date table join it with the start date of Table A and created Date of Table 2. Use the slicer from Date Table , then it should

 

count(TableA[Serial Number]) -count(TableB[Serial Number])

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-Y...


Appreciate your Kudos.

Hi @amitchandak, thanks for your reply.
How can I count the serial number in Table A if the selected date range in Table B falls between Start & End date of serial number in Table B?

If I create a Date table to join with start date in Table A, how if the start date is earlier than the created date (Table B) but end date is before the created date?

 

Thanks.

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.