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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

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
v-xianjtan-msft
Community Support
Community Support

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
v-xianjtan-msft
Community Support
Community Support

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

October NL Carousel

Fabric Community Update - October 2024

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

Top Solution Authors