Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
luxpbi
Helper V
Helper V

DAX calculated number of days between dates in the same column

Hi, 

 

I'm struggling with a problem in DAX. 

I have a data that looks like this:

DateProductState
01/01/20181A
05/01/20181B
12/01/20181C
15/01/20181D
17/01/20181E
04/01/20182B
06/01/20182D
08/01/20182A
02/01/20183A

 

I need to calculate for the same product the number of days between 2 states.

And in case it's possible only the number of days that a product is in state let's say D. 

 

Thanks for your help, I'm not sure how to aboard the problem.

8 REPLIES 8
Stachu
Community Champion
Community Champion

try this code for calculated column

Column =
VAR CurrentProduct = 'Table'[Product]
VAR CurrentState = 'Table'[State]
VAR CurrentDate = 'Table'[Date]
VAR NextStateDate =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        FILTER (
            'Table',
            'Table'[Product] = CurrentProduct
                && 'Table'[State] <> CurrentState
                && 'Table'[Date] > CurrentDate
        )
    )
RETURN
    IF ( ISBLANK ( NextStateDate ), BLANK (), NextStateDate - CurrentDate )


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Hi @Stachu

 

Thank you for your reply, it's almost what I'm looking for.
It doesn't work when for one product there is 2 lines with the same date but diferent status. Is possible to fix it?

13/05/2018 0:00D6
13/05/2018 0:00A6
19/05/2018 0:00D1

 

For the first line here the value should be 1 and for the second is correct. I've noticed that this occur a lot of times.

Do you think it's also possible to do it only for one kind of status (for example for status = D)? 

Thanks for your help!

Stachu
Community Champion
Community Champion

do you mean something like this?

Column =
VAR CurrentProduct = 'Table'[Product]
VAR CurrentState = 'Table'[State]
VAR CurrentDate = 'Table'[Date]
VAR NextStateDate =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        FILTER (
            'Table',
            'Table'[Product] = CurrentProduct
                && 'Table'[State] <> CurrentState
                && 'Table'[Date] > CurrentDate
        )
    )
RETURN
    IF (
        CurrentState = "D",
        IF ( ISBLANK ( NextStateDate ), 1, NextStateDate - CurrentDate ),
        BLANK ()
    )


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Just one more step 🙂 

In my case finally I just don't need to count the number of days for a product for one type of status but for 2 of them. 

 

DateStatusColumn
11/01/2018826
11/01/20182 
11/01/2018926
05/02/20182 

 

My issue is that the first value should be 1 and the second 26. 

 

Any aditional great idea?

 

Thank you!

Stachu
Community Champion
Community Champion

can you share the data that shows that in the format of the table from your first post?
The Status column wasn't there, and it's difficult to make sense out of it



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Sorry I just did it manually. 

 

DateProductStateColumn
11/01/20181D26
11/01/20181A 
11/01/20181C26
05/02/20181A 

 And I need to filter by D and C, not just D. 

And the first value should be 1 and the second 26. 

 

 

Stachu
Community Champion
Community Champion

for C&D this will work

Column =
VAR CurrentProduct = 'Table'[Product]
VAR CurrentState = 'Table'[State]
VAR CurrentDate = 'Table'[Date]
VAR NextStateDate =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        FILTER (
            'Table',
            'Table'[Product] = CurrentProduct
                && 'Table'[State] <> CurrentState
                && 'Table'[Date] > CurrentDate
        )
    )
RETURN
    IF (
        CurrentState = "D"
            || CurrentState = "C",
        IF ( ISBLANK ( NextStateDate ), 1, NextStateDate - CurrentDate +1),
        BLANK ()
    )

by default it retuned 25, so I aded +1 (in red in the code, so ou can remove it if needed), it also makes sense as you consider same date to be 1 and not 0



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Yes that work perfect but I have this:

 

RowDateProductStateColumn
111/01/20181D26
211/01/20181A 
311/01/20181C26
405/02/20181A 

  And what I need is this: 

RowDateProductStateColumn
111/01/20181D1
211/01/20181A 
311/01/20181C26
405/02/20181A 

 

Row 1 should be 1 and not 26 because the product is in state D one day and then changes to state C.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (6,186)