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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
amirsohani
New Member

Date Difference of Latest Two Values for Each Order

Hi there,

I have two tables:

1. Orders

Order IDPriceOrder Duration
1 $       2.00?
2 $       5.00?
3 $       3.00?

 

2. Orders Status

IDOrder IDStatusCreated at
11107/10/18 12:00 AM
21207/10/18 12:18 AM
31307/10/18 12:21 AM

 

I want to calculate date difference (minute) of latest two dates (ID:2 and ID:3 in order status table) for each order.

what is the DAX syntax for it?

1 ACCEPTED SOLUTION
v-jiascu-msft
Employee
Employee

Hi @amirsohani,

 

Try the demo out in the attachment. The two tables should have relationship. I have added more data to test.

Column =
VAR lastestTwo =
    CALCULATETABLE (
        TOPN ( 2, 'Orders Status', 'Orders Status'[Created at], DESC )
    )
RETURN
    DATEDIFF (
        MINX ( lastestTwo, [Created at] ),
        MAXX ( lastestTwo, [Created at] ),
        MINUTE
    )

date_difference_of_latest_two_values

 

 

Best Regards,

Dale

Community Support Team _ Dale
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

5 REPLIES 5
v-jiascu-msft
Employee
Employee

Hi @amirsohani,

 

Try the demo out in the attachment. The two tables should have relationship. I have added more data to test.

Column =
VAR lastestTwo =
    CALCULATETABLE (
        TOPN ( 2, 'Orders Status', 'Orders Status'[Created at], DESC )
    )
RETURN
    DATEDIFF (
        MINX ( lastestTwo, [Created at] ),
        MAXX ( lastestTwo, [Created at] ),
        MINUTE
    )

date_difference_of_latest_two_values

 

 

Best Regards,

Dale

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

Thank you Dale!

It works. Smiley Wink

tex628
Community Champion
Community Champion

Alright, due to the format of your order status table you will need to do something like this:

 

image.png

 

This is your order status table. A little simplified.

 

image.png

 

In the Query editor, use the "Transpose" feature. You should get something like this:

 

image.png

 

 

I would then use the "Use first row as headers" feature. See the previous picture.

 

image.png

 

Finally what you do is create a custom column, still in the query editor. Use the simple code [3] - [2].

 

Hope this helps! 🙂

 

 


Connect on LinkedIn

This is a sample table,
I just put 3 rows in order status table but it may have dozens of records for each order.
So your solution doesn’t help me in this case.

I realised that... In general, referring between two rows in the same column is alot harder than referring to two columns in the same row. 

 

You might be able to solve it using EARLIER()

If you filter the table by ID you might be able to collect both values that way. 

 

Eitherway, that format is more trouble than it's worth but good luck! 


Connect on LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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