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

View all the Fabric Data Days sessions on demand. View schedule

Reply
talitanieps
Advocate I
Advocate I

How to calculate difference of values for rows of the same column?

Hello PBI Community! Hope you are doing all right! 🙂

I need some assistance to calculate the difference between values of a same column; this is for a social media database, and the measure I had working just fine it's simply not anymore.

The data is appended everyday for different accounts, which here are represented by A, B and C, and the dates are from the column date.

The problem is that I need to calculate and show the amount of new followers acquired between two dates (usually a full month, but it needs to be flexible), and the data is not always organized by date and/or product:

For example, I'd need to calculate the difference  between may 17th and 18th for name A, that is 
88961-88889, but the values are separated by other values:

datenametotal_followers
18/05/2023A88961
18/05/2023B2882
18/05/2023C1935
17/05/2023A88889
17/05/2023B2868
17/05/2023C1935
16/05/2023A88793
16/05/2023B2858
16/05/2023C1937
15/05/2023A88710
15/05/2023B2848
15/05/2023C1936
14/05/2023A88674
14/05/2023B2847
14/05/2023C1937
13/05/2023A88674
13/05/2023B2847
13/05/2023C1937
12/05/2023A88580
12/05/2023B2832
12/05/2023C1935
11/05/2023A88514
11/05/2023B2794
11/05/2023C1929
10/05/2023A88459
10/05/2023B2784
10/05/2023C1924
09/05/2023A88373
09/05/2023B2766

 

Also, I'm currently using this Dax Formula to do the math, but it's not working (showing 0):

this firstdate() and lastdate() are supposed to get the dates from a date slicer, using a calendar table:

 

Measure = 

VAR _FollowersOnLastDate =
CALCULATE(
    MAX( Table[total_followers] ) ,
    FILTER( Table ,
            AND( Table[name] = SELECTEDVALUE( 'Table _ Media'[produto] ) ,
                  DATEADD ( FILTER ( DATESMTD ( d_Calendar[Date]), d_Calendar[Date] < TODAY()), -1, MONTH)) +0
            )
    )
)

VAR _FollowersOnFirstDate =
CALCULATE(
    MAX( Table[total_followers] ) ,
    FILTER( Table ,
            AND( Table[name] = SELECTEDVALUE( 'Table _ Media'[produto] ) ,
                  DATEADD ( FILTER ( DATESMTD ( d_Calendar[Date]), d_Calendar[Date] < TODAY()), -1, MONTH)) +0
            )
    )
)

VAR _MinFollowers =
CALCULATE(
    MIN( Table[total_followers] ) ,
    FILTER( Table ,
            Table[name] = SELECTEDVALUE( 'Table _ Media'[produto] )
    )
)

-------------------------------------------------------
RETURN _FollowersOnLastDate - _FollowersOnFirstDate

 SWITCH(
     TRUE() ,
     LASTDATE( d_Calendar[Date] ) <= MIN( Table[date] ) , 0 ,
     FIRSTDATE( d_Calendar[Date] ) < MIN( Table[date] ) , _FollowersOnLastDate - _MinFollowers ,
     _FollowersOnLastDate - _FollowersOnFirstDate
     ) +0

 


Any suggestions on how to improve this code, or to do the calculation as per I explained above?

Thanks once again for the assistance, in advance! 🙂

Best Regards,

 

 

2 REPLIES 2
Greg_Deckler
Community Champion
Community Champion

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



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hello @Greg_Deckler , 

Thanks a lot for your reply!
I'll review that and let you know of the results, thanks again! 🙂

Best regards, 

 

Talita.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors