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
Fighting21
Frequent Visitor

Is it possible to find the period between months with negative values by using datediff?

I have measure _CurrentDateValueMPreviousDateValue and i want find the period between months with minus _CurrentDateValueMPreviousDateValue.

https://drive.google.com/file/d/1CgDLgj0NzLY384LBMkJr5cGmCnAkbgIQ/view?usp=sharing 

Example_dateif.png

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Fighting21 

 

The calculation of measures is dynamic and dependent on the current context, using measures to achieve your desired effect can be challenging.
Please consider using calculated columns to do it.

1. Create a column to calculate the difference between the current row and the previous row:

Difference = 
VAR PrevDate = 
    CALCULATE(
        MAX('DataTable'[Date]),
        FILTER(
            'DataTable',
            'DataTable'[Category] = EARLIER('DataTable'[Category]) &&
            'DataTable'[SubCategory] = EARLIER('DataTable'[SubCategory]) &&
            'DataTable'[Date] < EARLIER('DataTable'[Date])
        )
    )
VAR PrevValue = 
    CALCULATE(
        MAX('DataTable'[Value]),
        FILTER(
            'DataTable',
            'DataTable'[Category] = EARLIER('DataTable'[Category]) &&
            'DataTable'[SubCategory] = EARLIER('DataTable'[SubCategory]) &&
            'DataTable'[Date] = PrevDate
        )
    )
RETURN
IF(ISBLANK(PrevValue), 'DataTable'[Value], 'DataTable'[Value] - PrevValue)

2. Create a column to calculate the month interval between negative values:

MonthsBetweenNegativeValues = 
VAR CurrentDate = 'DataTable'[Date]
VAR CurrentCategory = 'DataTable'[Category]
VAR CurrentSubCategory = 'DataTable'[SubCategory]
VAR PreviousNegativeDate = 
    CALCULATE(
        MAX('DataTable'[Date]),
        FILTER(
            'DataTable',
            'DataTable'[Category] = CurrentCategory &&
            'DataTable'[SubCategory] = CurrentSubCategory &&
            'DataTable'[Date] < CurrentDate &&
            'DataTable'[Difference] < 0
        )
    )
RETURN
IF(
    'DataTable'[Difference] < 0,
    IF(
        ISBLANK(PreviousNegativeDate),
        BLANK(),
        DATEDIFF(PreviousNegativeDate, CurrentDate, MONTH)
    ),
    BLANK()
)

 

vxianjtanmsft_0-1730885502176.png

 

Best Regards,
Jarvis Tang
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

4 REPLIES 4
Anonymous
Not applicable

Hi @Fighting21 

 

The calculation of measures is dynamic and dependent on the current context, using measures to achieve your desired effect can be challenging.
Please consider using calculated columns to do it.

1. Create a column to calculate the difference between the current row and the previous row:

Difference = 
VAR PrevDate = 
    CALCULATE(
        MAX('DataTable'[Date]),
        FILTER(
            'DataTable',
            'DataTable'[Category] = EARLIER('DataTable'[Category]) &&
            'DataTable'[SubCategory] = EARLIER('DataTable'[SubCategory]) &&
            'DataTable'[Date] < EARLIER('DataTable'[Date])
        )
    )
VAR PrevValue = 
    CALCULATE(
        MAX('DataTable'[Value]),
        FILTER(
            'DataTable',
            'DataTable'[Category] = EARLIER('DataTable'[Category]) &&
            'DataTable'[SubCategory] = EARLIER('DataTable'[SubCategory]) &&
            'DataTable'[Date] = PrevDate
        )
    )
RETURN
IF(ISBLANK(PrevValue), 'DataTable'[Value], 'DataTable'[Value] - PrevValue)

2. Create a column to calculate the month interval between negative values:

MonthsBetweenNegativeValues = 
VAR CurrentDate = 'DataTable'[Date]
VAR CurrentCategory = 'DataTable'[Category]
VAR CurrentSubCategory = 'DataTable'[SubCategory]
VAR PreviousNegativeDate = 
    CALCULATE(
        MAX('DataTable'[Date]),
        FILTER(
            'DataTable',
            'DataTable'[Category] = CurrentCategory &&
            'DataTable'[SubCategory] = CurrentSubCategory &&
            'DataTable'[Date] < CurrentDate &&
            'DataTable'[Difference] < 0
        )
    )
RETURN
IF(
    'DataTable'[Difference] < 0,
    IF(
        ISBLANK(PreviousNegativeDate),
        BLANK(),
        DATEDIFF(PreviousNegativeDate, CurrentDate, MONTH)
    ),
    BLANK()
)

 

vxianjtanmsft_0-1730885502176.png

 

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

 

 

Thank you very much, sorry for accept it as the solution lately.

PhilipTreacy
Super User
Super User

Hi @Fighting21 

 

I don't know what you mean by 'month between'.  Please show an example of the result you want, including the sub-categories you've mentioned.

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Thank you for reply i upgraded my question.

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.