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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
learning_going
Frequent Visitor

date gap between multiple item for occurrence in different month

hi all

 

whats the formula to calculate the days gap between each of the entry?

 

line1: nov 4  to nov 11

line2: nov11  to nov 27

line3:  nov27 to Dec 1

2024-06-12_18h15_53.png

1 ACCEPTED SOLUTION
Ritaf1983
Super User
Super User

Hi @learning_going 
The first step is to add an index column from PQ (after sorting the table by the column of the wanted date)

Ritaf1983_0-1718202009179.png

after you have an index column you can use a formula like : (replace parameters to the yours)

Difference =
VAR CurrentRowIndex = 'Orders'[Index]
VAR CurrentOrderDate = 'Orders'[Order Date]
VAR PreviousOrderDate =
    CALCULATE(
        MAX('Orders'[Order Date]),
        FILTER(
            'Orders',
            'Orders'[Index] < CurrentRowIndex
        )
    )
RETURN
    IF(
        ISBLANK(PreviousOrderDate),
        0,
        DATEDIFF(PreviousOrderDate, CurrentOrderDate, DAY)
    )
Result :
Ritaf1983_1-1718203941961.png

PBIX with the example is attached

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

View solution in original post

4 REPLIES 4
Ritaf1983
Super User
Super User

Hi @learning_going 
The first step is to add an index column from PQ (after sorting the table by the column of the wanted date)

Ritaf1983_0-1718202009179.png

after you have an index column you can use a formula like : (replace parameters to the yours)

Difference =
VAR CurrentRowIndex = 'Orders'[Index]
VAR CurrentOrderDate = 'Orders'[Order Date]
VAR PreviousOrderDate =
    CALCULATE(
        MAX('Orders'[Order Date]),
        FILTER(
            'Orders',
            'Orders'[Index] < CurrentRowIndex
        )
    )
RETURN
    IF(
        ISBLANK(PreviousOrderDate),
        0,
        DATEDIFF(PreviousOrderDate, CurrentOrderDate, DAY)
    )
Result :
Ritaf1983_1-1718203941961.png

PBIX with the example is attached

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

what element do i need to add in, to allow filtering based on the text as criteria? 

i tried a few workout, to get the gap by the lot no; cant seem to get the result i wanted 

Hi @learning_going 

I apologize, but unfortunately I'm having trouble understanding what exactly you're trying to do. Please provide an example of the data and the desired result.

I also understand that this is a new topic, so I recommend asking it separately as an independent post.

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

tried altering to this

 

Lot Difference =
VAR CurrentRowIndex = Sheet1[Index]
VAR CurrentOrderDate = Sheet1[< InvDate]
VAR mix = Sheet1[LotNo]
VAR country = Sheet1[Country]
VAR PreviousOrderDate =
    CALCULATE(
        MAX(Sheet1[< InvDate]),
        FILTER(
            Sheet1,
            Sheet1[Index] < CurrentRowIndex && mix = Sheet1[LotNo] && Sheet1[Country] = country
        )
    )
RETURN
    IF(
        ISBLANK(PreviousOrderDate),
        0,
        DATEDIFF(PreviousOrderDate, CurrentOrderDate, DAY)

but i kept getting msg " There's not enough memory to complete this operation. Please try again later when there may be more memory available."

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.