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
rel2022
Frequent Visitor

Nearest date to slicer selection

Hi,

 

How can I modify this DAX to find the nearest date (can be before or after) to the minimum value the user selects on the date range slicer? This data set doesn't have every possible day in it, so if someone doesn't pick one of the exact dates, it will return a zero but I want it to return the count of employees based on the nearest date to what the user puts in the slicer. Thanks!

 

 

MinValue =
VAR Min1 = CALCULATE( COUNT('Sheet1'[Employee ID]), FILTER('Sheet1', 'Sheet1'[Date Effective] = MIN('Dates'[Date]) ))
return
IF(ISBLANK(Min1), 0, Min1)
1 ACCEPTED SOLUTION
v-binbinyu-msft
Community Support
Community Support

Hi @rel2022 ,

Since you did not give a specific table, I had to create my own table for testing according to your description, please point out if there are any problems.

Please try below steps:

1. below is my test table

Table:

vbinbinyumsft_0-1663048106037.png

Dates:

Dates =
CALENDAR (
    FIRSTDATE ( 'Table'[Date Effective] ),
    LASTDATE ( 'Table'[Date Effective] )
)

vbinbinyumsft_1-1663048166797.png

2. create a measure and add it to card visual, add a slicer visual with "Dates[Date]" column

Measure =
VAR min_date =
    MIN ( Dates[Date] )
VAR tmp =
    FILTER ( ALL ( 'Table' ), 'Table'[Date Effective] >= min_date )
VAR ctn =
    COUNTROWS ( tmp )
RETURN
    IF ( ctn = 0, 0, ctn )

Animation13.gif

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_ Binbin Yu
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

3 REPLIES 3
v-binbinyu-msft
Community Support
Community Support

Hi @rel2022 ,

Since you did not give a specific table, I had to create my own table for testing according to your description, please point out if there are any problems.

Please try below steps:

1. below is my test table

Table:

vbinbinyumsft_0-1663048106037.png

Dates:

Dates =
CALENDAR (
    FIRSTDATE ( 'Table'[Date Effective] ),
    LASTDATE ( 'Table'[Date Effective] )
)

vbinbinyumsft_1-1663048166797.png

2. create a measure and add it to card visual, add a slicer visual with "Dates[Date]" column

Measure =
VAR min_date =
    MIN ( Dates[Date] )
VAR tmp =
    FILTER ( ALL ( 'Table' ), 'Table'[Date Effective] >= min_date )
VAR ctn =
    COUNTROWS ( tmp )
RETURN
    IF ( ctn = 0, 0, ctn )

Animation13.gif

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_ Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-binbinyu-msft 

Thank you for your response! I should have been more clear.

 

My data table is grouped by pay period dates (every 2 weeks there is a group of employees - mostly the same employees but some join and some leave the organization), so I want the measure to count the number of employees in the pay period group closest to the 2 dates selected by the user in the slicer, and then I'll divide that number by 2 which will be my estimate of the number of employees working there during the time range inputted by the user.

 

An example data table would be:

Employee IDPay Period Date
10019/12/2021
10029/12/2021
10039/12/2021
10049/12/2021
10059/12/2021
10069/12/2021
10079/12/2021
10019/26/2021
10029/26/2021
10039/26/2021
10049/26/2021
10059/26/2021
10069/26/2021
10109/26/2021
10119/26/2021
10129/26/2021

 

In the above example table, if someone selects any date near or exactly 9/12/2021 (9/11/2021, 9/12/2021, 9/13/2021, 9/14/2021, etc.), the count would be based on the data from 9/12/2021 only and the result would be 7 employees. If someone selects a date near 9/26/2021, then the count would be based on the 9/26/2021 data only and the result would be 9 employees. 

 

Thanks so much for any help you can provide!!

Hi @rel2022 ,

Please adjust my measure according to your demands, the "nearest date" i cannot confirm, so please according youself need to adjust the measure.

 

Best regards,
Community Support Team_ Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

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.