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
Anonymous
Not applicable

DAX: Calculation based on having the same column value

Hi community,

 

I am stuck at the following. I want to obtain the time difference in seconds between rows based on a rank and the same column value in DAX.

 

This is my data:

leone1857_0-1664637957742.png

 

I want to obtain the time difference in seconds between a rank and it's previous rank, whilst having the same club_id.

The result would look like this:

 

leone1857_1-1664637984343.png

 

I tried to do it kinda like this answer, but without success. It would be nice to understand the DAX behind getting data with the same column values.

 

Thanks in advance for any help, tips or tricks!

 

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@Anonymous Use this:

seconds = 
    VAR __clubID = [club_id]
    VAR __rank = [rank_team]
    VAR __previous = MAXX(FILTER('Table',[club_id] = __clubID && [rank_team] < __rank),[joined_at])
RETURN
    IF( __previous = BLANK(), 0, ROUND( ( [joined_at] - __previous ) * 1. * 24 * 60 * 60, 0) )

PBIX is attached below signature. For a full explanation: See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous


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

View solution in original post

1 REPLY 1
Greg_Deckler
Super User
Super User

@Anonymous Use this:

seconds = 
    VAR __clubID = [club_id]
    VAR __rank = [rank_team]
    VAR __previous = MAXX(FILTER('Table',[club_id] = __clubID && [rank_team] < __rank),[joined_at])
RETURN
    IF( __previous = BLANK(), 0, ROUND( ( [joined_at] - __previous ) * 1. * 24 * 60 * 60, 0) )

PBIX is attached below signature. For a full explanation: See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous


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

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.