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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

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
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.