cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
axk180022
Helper II
Helper II

Find difference between the 2 rows of column based on another column criteria

axk180022_0-1630569259585.png

I would want to find the difference in the GenHours for each serialid. Currently this is how my table looks on power BI. I have filtered the latest and 2nd latest date for each serialid.

 

Now i would want to find the difference between the GenHours that is being diplayed on Power BI after applying filters like removing blanks. 

 

Can someone please help.

 

@amitchandak @Greg_Deckler @parry2k 

1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @axk180022 

According to your statement, I know you want to show absolute Gen Difference. And you will filter your table to show first 2 date for each serialid, you just want show difference in 1st latest date and difference in 2nd latest date will show blank. Here I suggest you build a table visual and create a measure to achieve your goal. Measure could be dynamic.

My Sample:

1.png

Rank is a calculated column.

Rank = RANKX(FILTER('Table','Table'[serialid]=EARLIER('Table'[serialid])),'Table'[Dates],,DESC,Dense)

Build a table visual and filter visual by rank column to show items when value is less than or equal to 2.

Measure:

Gen Difference = 
VAR _RANK1GenHours =
    CALCULATE (
        SUM ( 'Table'[GenHours] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[serialid] = MAX ( 'Table'[serialid] )
                && 'Table'[Rank] = 1
        )
    )
VAR _RANK2GenHours =
    CALCULATE (
        SUM ( 'Table'[GenHours] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[serialid] = MAX ( 'Table'[serialid] )
                && 'Table'[Rank] = 2
        )
    )
RETURN
    IF (
        SUM ( 'Table'[Rank] ) = 1,
        ABS ( _RANK1GenHours - _RANK2GenHours ),
        BLANK ()
    )

Result is as below.

2.png

Best Regards,
Rico Zhou

 

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

2 REPLIES 2
v-rzhou-msft
Community Support
Community Support

Hi @axk180022 

According to your statement, I know you want to show absolute Gen Difference. And you will filter your table to show first 2 date for each serialid, you just want show difference in 1st latest date and difference in 2nd latest date will show blank. Here I suggest you build a table visual and create a measure to achieve your goal. Measure could be dynamic.

My Sample:

1.png

Rank is a calculated column.

Rank = RANKX(FILTER('Table','Table'[serialid]=EARLIER('Table'[serialid])),'Table'[Dates],,DESC,Dense)

Build a table visual and filter visual by rank column to show items when value is less than or equal to 2.

Measure:

Gen Difference = 
VAR _RANK1GenHours =
    CALCULATE (
        SUM ( 'Table'[GenHours] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[serialid] = MAX ( 'Table'[serialid] )
                && 'Table'[Rank] = 1
        )
    )
VAR _RANK2GenHours =
    CALCULATE (
        SUM ( 'Table'[GenHours] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[serialid] = MAX ( 'Table'[serialid] )
                && 'Table'[Rank] = 2
        )
    )
RETURN
    IF (
        SUM ( 'Table'[Rank] ) = 1,
        ABS ( _RANK1GenHours - _RANK2GenHours ),
        BLANK ()
    )

Result is as below.

2.png

Best Regards,
Rico Zhou

 

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

 

Greg_Deckler
Super User
Super User

@axk180022 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
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors