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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
ArtoKukkonen
New Member

Date difference

Hi,

could someone have a solution to this how to calculate the difference between dates in the dax according to the example below?

 

CustomerIDTransactionDateStatusOnStatusOffDiff 
1008.4.2009 12.4.2021  
10026.3.201512.10.2021   
1001.11.2015 22.3.2022161(22.3.2022-12.10.2021)
1003.9.201913.4.2022   
10012.10.2020 7.2.2023300(7.2.2023-13.4.2022)
1 ACCEPTED SOLUTION
_elbpower
Resolver III
Resolver III

To calculate the duration in days between consecutive "Status On" and "Status Off" dates for the given dataset, you can follow these steps:

  1. Create a new calculated column to combine "Status On" and "Status Off" dates into one column, and another column to indicate whether it's a "Status On" or "Status Off" date.

 

CombinedDate = 
IF(ISBLANK([StatusOn]), [StatusOff], [StatusOn])

EventType = 
IF(ISBLANK([StatusOn]), "Status Off", "Status On")
​

 

  1. Sort the table by "TransactionDate" and "CombinedDate" in ascending order.

  2. Create a new calculated column to calculate the duration between consecutive "Status On" and "Status Off" events.

 

DurationInDays =
VAR CurrentRowDate = [CombinedDate]
VAR CurrentRowEventType = [EventType]
VAR PreviousRowDate =
    CALCULATE(
        MAX([CombinedDate]),
        FILTER(
            ALL('YourTable'),
            'YourTable'[TransactionDate] < EARLIER('YourTable'[TransactionDate])
            && 'YourTable'[CustomerID] = EARLIER('YourTable'[CustomerID])
        )
    )
RETURN
    IF(
        CurrentRowEventType = "Status Off" && PreviousRowDate <> BLANK(),
        CurrentRowDate - PreviousRowDate,
        BLANK()
    )

 

  • Replace YourTable with the actual name of your table.

    This calculated column calculates the duration in days between consecutive "Status On" and "Status Off" events for each customer based on the sorted "TransactionDate."

     

  • Now, you have a new column, "DurationInDays," that represents the duration in days between "Status On" and "Status Off" events for each customer.

View solution in original post

1 REPLY 1
_elbpower
Resolver III
Resolver III

To calculate the duration in days between consecutive "Status On" and "Status Off" dates for the given dataset, you can follow these steps:

  1. Create a new calculated column to combine "Status On" and "Status Off" dates into one column, and another column to indicate whether it's a "Status On" or "Status Off" date.

 

CombinedDate = 
IF(ISBLANK([StatusOn]), [StatusOff], [StatusOn])

EventType = 
IF(ISBLANK([StatusOn]), "Status Off", "Status On")
​

 

  1. Sort the table by "TransactionDate" and "CombinedDate" in ascending order.

  2. Create a new calculated column to calculate the duration between consecutive "Status On" and "Status Off" events.

 

DurationInDays =
VAR CurrentRowDate = [CombinedDate]
VAR CurrentRowEventType = [EventType]
VAR PreviousRowDate =
    CALCULATE(
        MAX([CombinedDate]),
        FILTER(
            ALL('YourTable'),
            'YourTable'[TransactionDate] < EARLIER('YourTable'[TransactionDate])
            && 'YourTable'[CustomerID] = EARLIER('YourTable'[CustomerID])
        )
    )
RETURN
    IF(
        CurrentRowEventType = "Status Off" && PreviousRowDate <> BLANK(),
        CurrentRowDate - PreviousRowDate,
        BLANK()
    )

 

  • Replace YourTable with the actual name of your table.

    This calculated column calculates the duration in days between consecutive "Status On" and "Status Off" events for each customer based on the sorted "TransactionDate."

     

  • Now, you have a new column, "DurationInDays," that represents the duration in days between "Status On" and "Status Off" events for each customer.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.