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
POSPOS
Helper IV
Helper IV

Days between dates of same column using DAX

Hi All,

I have a requirement to find the days between dates of the same column. 
The first value should be the difference between the date and the date of the next step and the last value should be zero.

POSPOS_0-1700513247156.png

I have attached a sample pbix file with date here
Could someone please suggest on how to achieve this using DAX.

 

1 ACCEPTED SOLUTION

Hi @POSPOS ,

Please try:

Column = 
VAR _cur_protocol = 'export'[Protocol Number]
VAR _cur_index = 'export'[Index]
VAR _next_index = CALCULATE(MIN('export'[Index]),FILTER(ALL('export'),'export'[Protocol Number]=_cur_protocol && 'export'[Index]>_cur_index))
VAR _next_date = CALCULATE(MIN('export'[Status Date]),ALLEXCEPT('export','export'[Index]),'export'[Index]=_next_index)
VAR _result = COALESCE(DATEDIFF('export'[Status Date],_next_date,DAY),0)
RETURN
_result

vcgaomsft_0-1700641281201.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

View solution in original post

10 REPLIES 10
Data-estDog
Resolver II
Resolver II

DaysTillNextSatus=
VAR CurrentStatusDate = Min('export'[Status Date])
VAR CurrentProtocol = SELECTEDVALUE('export'[Protocol Number])
var NextDate = CALCULATE(
    Min('export'[Status Date]),
    FILTER(
        ALL('export'),
        'export'[Status Date] > CurrentStatusDate && CurrentProtocol = 'export'[Protocol Number]
    )
)
VAR vDifference = DATEDIFF(CurrentStatusDate , NextDate, DAY)

RETURN if(ISBLANK(vDifference), 0, vDifference)

@Data-estDog  - thank for the response.

When the dates are same, the values are not showing as expected. In this example, "Staff" comes before "Assigning", hence staff should be 9 and Assigning should be 0.

But 9 is repeated for both the statuses.

POSPOS_0-1700516363709.png

 

You would need a more granular date/time stamp. Or a sequence number (order the status was changed in) in order to achieve that.

@Data-estDog  - Can we add a index col and then use this code ? as the statuses are not always in alphabetical order and can vary case by case.

Yes. I see that as the same as a sequence number. With the absence of time, its just something that gives indication of order.

 

Don't forget to mark this as the solution if and give me a thumbs up if I have helped you!

@Data-estDog  - 
Could you please suggest with the code using index. I have added index in the Power Query and tried reusing the code provided first, but does not seem to work fine.

Sure. I re-downloaded the pbix file... but no index column was there.

 

@Data-estDog  - Please find the file here.

Hi @POSPOS ,

Please try:

Column = 
VAR _cur_protocol = 'export'[Protocol Number]
VAR _cur_index = 'export'[Index]
VAR _next_index = CALCULATE(MIN('export'[Index]),FILTER(ALL('export'),'export'[Protocol Number]=_cur_protocol && 'export'[Index]>_cur_index))
VAR _next_date = CALCULATE(MIN('export'[Status Date]),ALLEXCEPT('export','export'[Index]),'export'[Index]=_next_index)
VAR _result = COALESCE(DATEDIFF('export'[Status Date],_next_date,DAY),0)
RETURN
_result

vcgaomsft_0-1700641281201.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

If you want to assume status always happens in alphabetical order: 

DaysTillNextStatus =
VAR CurrentStatusDate = Min('export'[Status Date])
VAR CurrentProtocol = SELECTEDVALUE('export'[Protocol Number])
VAR CurrentStatus = SELECTEDVALUE('export'[Status])
var NextDate = CALCULATE(
    Min('export'[Status Date]),
    FILTER(
        ALL('export'),
        ('export'[Status Date] > CurrentStatusDate && CurrentProtocol = 'export'[Protocol Number])
    )
)
var NextDate2 = CALCULATE(
    Min('export'[Status Date]),
    FILTER(
        ALL('export'),
        ( 'export'[Status Date] = CurrentStatusDate
            && CurrentProtocol = 'export'[Protocol Number]
            && 'export'[Status] > CurrentStatus)
    )
)
VAR vDifference = DATEDIFF(CurrentStatusDate , NextDate, DAY)
VAR vDifference2 = DATEDIFF(CurrentStatusDate , NextDate2, DAY)

RETURN IF(ISBLANK(COALESCE(vDifference2, vDifference)), 0, COALESCE(vDifference2, vDifference))

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.