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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

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
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.