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
Yamabushi
Helper I
Helper I

Dynamic DATEDIFF problem

Hello,

 

I am trying to calculate the date difference between various rows in minutes as follows:

  • I need to know the date difference between transactions for each person individually.
  • If two of the transactions from the same person happened simultaneously (down to the second), I want the difference between them to be 0.

Below is the sample data and how far along I managed to get (I filtered out a single person for convenience):

image.png

 

Difference from previous = DATEDIFF(CALCULATE(MAX('Sample'[Date and time of transaction]);FILTER('Sample';EARLIER('Sample'[Name])='Sample'[Name]&&EARLIER('Sample'[Date and time of transaction])>'Sample'[Date and time of transaction]));'Sample'[Date and time of transaction];MINUTE)

Now as you can see, the problem is that two transactions happened at the same moment, but DAX ignores that and instead just uses the previous transaction that was done at a different time. How can I solve this? I've tried || and >= but then I just get zeros everywhere.

 

 

After this has been done, I would like to be able to filter the data based on transaction type and get just the differences between the currently shown data. How can this be done?

 

Thank you very much!

 

1 ACCEPTED SOLUTION

Hi @Yamabushi ,

 

To use this formula to create a calcualted column.

 

Column 2 = 
VAR ind = Table1[Index] - 1
VAR pre =
    CALCULATE (
        MAX ( 'Table1'[Date and time of transaction] ),
        FILTER ( Table1, Table1[Index] = ind )
    )
RETURN
    DATEDIFF ( pre, 'Table1'[Date and time of transaction], MINUTE )

Capture.PNG

 

Regards,

Frank

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

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

I think you are going to need to use an Index to solve this. So you will need that and will want to check how many rows you have of the same sample and date/time. If you have multiple, then check your Index. If your current row is the MIN index, then >, otherwise, >=. If you do not have multiple, then >.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler,

 

Thank you for you reply! Would you care to elaborate a bit more in depth? I'm not sure I quite understand, I'm not really an expert in this stuff.

 

Thank you!

Hi @Yamabushi,

 

Firstly, I insert an index column in Power query. after that, I create a calculated column as below. If I misunderstood your requirement, kindly share your excepted result to me.

 

 

Column 2 =
DATEDIFF (
    CALCULATE (
        MAX ( 'Table1'[Date and time of transaction] ),
        FILTER (
            Table1,
            'Table1'[Transaction type] = EARLIER ( Table1[Transaction type] )
                && Table1[Index] < EARLIER ( Table1[Index] )
        )
    ),
    'Table1'[Date and time of transaction],
    MINUTE
)

Capture.PNG

 

Regards,

Frank

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

Hi @v-frfei-msft,

 

If I have 2 transactions at the same time, I would like the first transaction to show the difference from the previous transaction (as it was at a different time). The next transaction (which is at the same time as the previous transaction), should show 0.

 

So the solution is not quite what I'm looking for yet. I want the second row to show 1535 and the third row to show 0.

 

Hope this is not too complicated.

 

Best regards

Hi @Yamabushi ,

 

To use this formula to create a calcualted column.

 

Column 2 = 
VAR ind = Table1[Index] - 1
VAR pre =
    CALCULATE (
        MAX ( 'Table1'[Date and time of transaction] ),
        FILTER ( Table1, Table1[Index] = ind )
    )
RETURN
    DATEDIFF ( pre, 'Table1'[Date and time of transaction], MINUTE )

Capture.PNG

 

Regards,

Frank

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

Hi @v-frfei-msft ,

 

Great, this seems to work ok!

 

Thank you!

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.