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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
meghansh
Frequent Visitor

Calculating Date Difference on condition

Hello,
I have this table.

ID   Phase     Amount  PhaseSequence  HasPhase2

1       Red      500          1                        No

1       Green   750         3                        No

2       Red       800         1                        Yes

2       Yellow   900         2                        Yes 

2       Green   1500        3                        Yes

 

I want to create another column with condition as:

If hasphase2 = "Yes"

then calculate Amount of Phase 2 - Amount of Phase 1 for ID = 2
and Amount of Phase 3 - Amount of Phase 2

else Amount of Phase 3 - Amount of Phase 1

Result shoult be like

ID   Phase  Amount  Difference

1       1        500         

1       3        750         250

2       1        800         

2       2        900         100  

2       3        1500        600

 

Thanks

2 ACCEPTED SOLUTIONS
quantumudit
Super User
Super User

Hello @meghansh 
To achieve the desired result, you can utilize the following DAX formula:

Difference = 
VAR _id = PhaseTbl[ID]
VAR _phaseNum = PhaseTbl[PhaseSequence]
VAR _conditionalPhaseNum = IF(PhaseTbl[HasPhase2] = "No", 1, _phaseNum-1)
VAR _deductionAmount = 
CALCULATE(
    SUM(PhaseTbl[Amount]),
    FILTER(
        ALL(PhaseTbl),
        PhaseTbl[ID] = _id && PhaseTbl[PhaseSequence] = _conditionalPhaseNum
))

RETURN
IF(PhaseTbl[PhaseSequence] <> 1, PhaseTbl[Amount] -_deductionAmount)

Here is the screenshot of the solution for your reference:

quantumudit_0-1731091958052.png

Best Regards,
Udit

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
Visit My Linktree: LinkTree

View solution in original post

Anonymous
Not applicable

Hi @meghansh 

It seems that you want to calculate the difference row by row for different IDs. Besides the method provided by quantumudit , you can also consider the following method.

First create an index column in power query:

vjialongymsft_0-1731296803358.png

 

Then you can try the following measure:

Measure = 
var _ID = SELECTEDVALUE('Table'[ID])
VAR _index = SELECTEDVALUE('Table'[Index])
var _current = SELECTEDVALUE('Table'[Amount])
VAR _previous = CALCULATE(SELECTEDVALUE('Table'[Amount]),FILTER(ALL('Table'),'Table'[Index] = _index -1 && 'Table'[ID] = _ID))

RETURN
IF(_previous <> BLANK(),_current - _previous,BLANK())

 

 

Select "show items with no data"

vjialongymsft_1-1731297734618.png



Result:

vjialongymsft_2-1731297790291.png

 

 

 

 

 

 

Best Regards,

Jayleny

 

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

Hi @meghansh 

It seems that you want to calculate the difference row by row for different IDs. Besides the method provided by quantumudit , you can also consider the following method.

First create an index column in power query:

vjialongymsft_0-1731296803358.png

 

Then you can try the following measure:

Measure = 
var _ID = SELECTEDVALUE('Table'[ID])
VAR _index = SELECTEDVALUE('Table'[Index])
var _current = SELECTEDVALUE('Table'[Amount])
VAR _previous = CALCULATE(SELECTEDVALUE('Table'[Amount]),FILTER(ALL('Table'),'Table'[Index] = _index -1 && 'Table'[ID] = _ID))

RETURN
IF(_previous <> BLANK(),_current - _previous,BLANK())

 

 

Select "show items with no data"

vjialongymsft_1-1731297734618.png



Result:

vjialongymsft_2-1731297790291.png

 

 

 

 

 

 

Best Regards,

Jayleny

 

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

quantumudit
Super User
Super User

Hello @meghansh 
To achieve the desired result, you can utilize the following DAX formula:

Difference = 
VAR _id = PhaseTbl[ID]
VAR _phaseNum = PhaseTbl[PhaseSequence]
VAR _conditionalPhaseNum = IF(PhaseTbl[HasPhase2] = "No", 1, _phaseNum-1)
VAR _deductionAmount = 
CALCULATE(
    SUM(PhaseTbl[Amount]),
    FILTER(
        ALL(PhaseTbl),
        PhaseTbl[ID] = _id && PhaseTbl[PhaseSequence] = _conditionalPhaseNum
))

RETURN
IF(PhaseTbl[PhaseSequence] <> 1, PhaseTbl[Amount] -_deductionAmount)

Here is the screenshot of the solution for your reference:

quantumudit_0-1731091958052.png

Best Regards,
Udit

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
Visit My Linktree: LinkTree

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors