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! Request now

Reply
Anonymous
Not applicable

Days between purchase and cancellation (days between two rows)

Hi all, 

 

I want to create a measure that calculates the number of days between a customer buys a product and returns it (if that happens). 

 

Our sales are stored in rows in a table like the one below:

NicoM96_3-1652725363072.png

 

 

Every bought product is stored in individual rows and has an unique purchase ID.  Can anyone help to count the number of days between the pruchase date and the return date?

 

 

Best,

 

NicoM96

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

According to your description, here's my solution.

Create a measure.

Measure =
IF (
    MAX ( 'Table'[Purchase/return] ) = "return",
    DATEDIFF (
        MAXX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[Product] = MAX ( 'Table'[Product] )
                    && 'Table'[Purchase ID] = MAX ( 'Table'[Purchase ID] )
                    && 'Table'[Date] <= MAX ( 'Table'[Date] )
                    && 'Table'[Purchase/return] = "Purchase"
            ),
            'Table'[Date]
        ),
        MAX ( 'Table'[Date] ),
        DAY
    ),
    BLANK ()
)

Get the expected result.

vkalyjmsft_0-1653027458403.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

3 REPLIES 3
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

According to your description, here's my solution.

Create a measure.

Measure =
IF (
    MAX ( 'Table'[Purchase/return] ) = "return",
    DATEDIFF (
        MAXX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[Product] = MAX ( 'Table'[Product] )
                    && 'Table'[Purchase ID] = MAX ( 'Table'[Purchase ID] )
                    && 'Table'[Date] <= MAX ( 'Table'[Date] )
                    && 'Table'[Purchase/return] = "Purchase"
            ),
            'Table'[Date]
        ),
        MAX ( 'Table'[Date] ),
        DAY
    ),
    BLANK ()
)

Get the expected result.

vkalyjmsft_0-1653027458403.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

Whitewater100
Solution Sage
Solution Sage

Hi:

If you want to see positive value for answer it would be:

Return Days =
var ifreturn =
CALCULATE(MAX(Purchases[Date]), ALLEXCEPT(Purchases,Purchases[Purch ID]))
return
IF(Purchases[Date] = ifreturn, BLANK(), INT(Purchases[Date] - ifreturn))*-1
Whitewater100
Solution Sage
Solution Sage

Hi Nico:

I was calling your table "Purchases.

I added Calc col

Return Days =
var ifreturn =
CALCULATE(MAX(Purchases[Date]), ALLEXCEPT(Purchases,Purchases[Purch ID]))
return
IF(Purchases[Date] = ifreturn, BLANK(), INT(Purchases[Date] - ifreturn))
Whitewater100_0-1652726779683.png

I hope you can mark as solved. Thanks..

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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.