cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
leone1857
Frequent Visitor

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

@leone1857 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/3395....
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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

1 REPLY 1
Greg_Deckler
Super User
Super User

@leone1857 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/3395....
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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
November 2022 Update

Check it Out!

Click here to read more about the November 2022 updates!

Microsoft 365 Conference â__ December 6-8, 2022

Microsoft 365 Conference - 06-08 December

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Power BI Dev Camp Session 27

Ted's Dev Camp

This session walks through creating a new Azure AD B2C tenant and configuring it with user flows and custom policies.