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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.