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

Days between based on slicer

Greetings!

I have table that has several lines of startdate and enddate. I can easily calculate a new colun that shows the number of days between startdate and enddate, but in the next step I'm bit lost.

I have added a date table, and made a slicer based on that. I want to count the days between startdate and enddate so that it is dependent on slicer. Here's an example 

startdate = 1.1.2018
enddate = 1.12.2018
slicer start = 1.2.2018
slicer end = 31.12.2018

Based on those dates, I would like to have a new column/measure, that would give me number of days between 1.2.2018 and 1.12.2018. 

 

I have tried to do this for example with following:

 

DayCount2 = 
VAR start = person[startDate]
VAR end = person[endDate]
RETURN
CALCULATE(
    DATEDIFF(
        IF(start < MIN('Date'[Date]);MIN('Date'[Date]);start);
        IF(end > MAX('Date'[Date]);MAX('Date'[Date]);end);
        DAY)
)

However, with this, MIN() and MAX() always gives the first and last date from the date table. I tried to do a simple measure with them, and with them I get the right result from slicer. 

 

Thank you in advance!

1 ACCEPTED SOLUTION
v-yuezhe-msft
Employee
Employee

@BilboBaggins,

You can create the following measure. For more details, please check attached PBIX file.

Datediff = var maxslicer=MAX('Date'[Date]) var minslicer=MIN('Date'[Date]) return CALCULATE(
    DATEDIFF(
        IF(MAX(person[startDate]) < minslicer;minslicer;MAX(person[startDate]));
        IF(MAX(person[endDate]) > maxslicer;maxslicer;MAX(person[endDate]) );
        DAY)
)

1.PNG

Regards,
Lydia

Community Support Team _ Lydia Zhang
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

2 REPLIES 2
v-yuezhe-msft
Employee
Employee

@BilboBaggins,

You can create the following measure. For more details, please check attached PBIX file.

Datediff = var maxslicer=MAX('Date'[Date]) var minslicer=MIN('Date'[Date]) return CALCULATE(
    DATEDIFF(
        IF(MAX(person[startDate]) < minslicer;minslicer;MAX(person[startDate]));
        IF(MAX(person[endDate]) > maxslicer;maxslicer;MAX(person[endDate]) );
        DAY)
)

1.PNG

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks Lydia, you made my day! Smiley Very Happy

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.