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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Calculate difference between two values per ID

Hello everyone I currently have this type of data set and was wondering if there is a way to calculate the difference month over month, with January being the first month, per employee. 

Employee IDAmountDate
11001/1/2022
12002/1/2022
13003/1/2022
25001/1/2022
26002/1/2022
27003/1/2022
31501/1/2022
33002/1/2022
34503/1/2022

So that information would look something like this.

Employee IDAmountDifferenceDate
110001/1/2022
12001002/1/2022
13001003/1/2022
250001/1/2022
26001002/1/2022
27001003/1/2022
315001/1/2022
33001502/1/2022
34501503/1/2022
1 ACCEPTED SOLUTION

@Anonymous Whoops, was troubleshooting. Yes, that can be done:

Column = 
    VAR __Employee = [Employee ID]
    VAR __Date = [Date]
    VAR __Amount = [Amount]
    VAR __PrevDate = MAXX(FILTER(ALL('Table'),[Date]<__Date),[Date])
    VAR __PrevAmount = MAXX(FILTER(ALL('Table'),[Employee ID] = __Employee && [Date] = __PrevDate),[Amount])
    VAR __Diff = __Amount - __PrevAmount
    VAR __Result = IF(__PrevAmount = BLANK(), BLANK(), __Diff)
RETURN
    __Result


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

View solution in original post

3 REPLIES 3
Greg_Deckler
Community Champion
Community Champion

@Anonymous Try:

Column = 
    VAR __Employee = [Employee ID]
    VAR __Date = [Date]
    VAR __Amount = [Amount]
    VAR __PrevDate = MAXX(FILTER(ALL('Table'),[Date]<__Date),[Date])
    VAR __PrevAmount = MAXX(FILTER(ALL('Table'),[Employee ID] = __Employee && [Date] = __PrevDate),[Amount])
    VAR __Result = __Amount - __PrevAmount
RETURN
    __PrevAmount

Basically MTBF pattern. 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



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

Hello @Greg_Deckler,

 

The formula has worked! I just had to switch the last part as that was just returning the previous amount as opposed to the result. I did have a question though. Is there a way to have it ignore the January value as it will have no comparison? Right now in the data it is showing the chart below, instead of a zero. Thanks for all the help!

 

Employee IDAmountDifferenceDate
11001001/1/2022
12001002/1/2022
13001003/1/2022

@Anonymous Whoops, was troubleshooting. Yes, that can be done:

Column = 
    VAR __Employee = [Employee ID]
    VAR __Date = [Date]
    VAR __Amount = [Amount]
    VAR __PrevDate = MAXX(FILTER(ALL('Table'),[Date]<__Date),[Date])
    VAR __PrevAmount = MAXX(FILTER(ALL('Table'),[Employee ID] = __Employee && [Date] = __PrevDate),[Amount])
    VAR __Diff = __Amount - __PrevAmount
    VAR __Result = IF(__PrevAmount = BLANK(), BLANK(), __Diff)
RETURN
    __Result


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

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.