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

How to create a DAX measure to find time between order dates

Hi, I'm looking to create a DAX measure to solve the same problem as this:

https://community.powerbi.com/t5/Desktop/Date-difference-between-values-in-same-column/m-p/454941#M2...

 

The solution to this was creating a calculated column, however I am limited to using only DAX measures in my current model.

 

Basically, given a list of companies and order dates, I need a field that tells me how long ago the last order was for every row, like:

 

moore_02_1-1634252200157.png

Thanks in advance for the help!

3 REPLIES 3
v-chenwuz-msft
Community Support
Community Support

Hi @Anonymous 

 

Try this measure:

Measure =
VAR _top2 =
    TOPN(
        2,
        FILTER(
            ALL( 'Table' ),
            [Company] = SELECTEDVALUE( 'Table'[Company] )
                && [Order Date] < SELECTEDVALUE( 'Table'[Order Date] )
        ),
        [Order Date], DESC
    )
VAR _pdate =
    MAXX( _top2, [Order Date] )
RETURN
    DATEDIFF( _pdate, SELECTEDVALUE( 'Table'[Order Date] ), DAY )

 

And the pbix file I put in the attachment you can reference.

 

Best Regards

Community Support Team _ chenwu zhu

 

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

CNENFRNL
Community Champion
Community Champion

Screenshot 2021-10-15 013423.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Thanks for the reply! PBI won't let me use my company column in this measure. Both the company column and the order date column are just regular columns in my model, not measures, which I believe is the problem. Is there a workaround to this that you know of?

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.

Top Solution Authors