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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
verolure
New Member

Issue with Consecutive Valid Date Calculation in DAX

The issue is that the Diff Dates 1 column currently calculates the day difference between consecutive dates for each person, instead of between consecutive valid dates as intended.

 

Problem:

The goal of the Diff Dates 1 column is to calculate the day difference between two consecutive valid dates for each person in the Data 01 table. However, it is actually calculating the day difference between consecutive dates regardless of validity, which produces incorrect results.

 

Current Workaround:

I created the Diff Dates 2 column as a workaround. This column calculates the day difference between consecutive dates for each person and only returns the result if the row is marked as valid. While it achieves the goal, this approach is very inefficient.

 

Can you help me identify the issue in Diff Dates 1? Do you have suggestions for more efficient solutions?

 

Diff Dates 1 code:

Diff Dates 1 =
VAR CurrentDate = 'Data 01'[Date]
VAR CurrentPersonId = 'Data 01'[PersonID]

VAR MaxPreviousDate =
    CALCULATE (
        MAX(  'Data 01'[Date] ),
        FILTER (
            ALL ( 'Data 01' ),
            'Data 01'[Valid] = "TRUE"
            && 'Data 01'[PersonID] = CurrentPersonId
            && 'Data 01'[Date] < CurrentDate
        )
    )
RETURN
DATEDIFF ( MaxPreviousDate, CurrentDate, DAY )
 
Diff Dates 2 code:
Diff Dates 2 =
VAR CurrentDate = 'Data 01'[Date]
VAR CurrentPersonId = 'Data 01'[PersonID]

VAR MaxPreviousDate =
    CALCULATE (
        MAX ( 'Data 01'[Date] ),
        FILTER (
            ALL ( 'Data 01' ),
            AND ('Data 01'[Date] < CurrentDate,
            'Data 01'[PersonID] = CurrentPersonId
            )
        )
    )
RETURN
IF('Data 01'[Valid] = "TRUE", DATEDIFF ( MaxPreviousDate, CurrentDate, DAY ))

 

Data from the table Data 01:

 

verolure_2-1731607045285.png

 

Data from Excel:

verolure_3-1731607064945.png

 

1 ACCEPTED SOLUTION

Hi @verolure ,

 

With your description you need to filter some data, you can create a slicer visual object to filter, if you need the results to change dynamically then you can change the measure to:

Measure = 
VAR CurrentDate = MAX('Data 01'[Date])
VAR CurrentPersonId = MAX('Data 01'[PersonID])

VAR MaxPreviousDate =
    CALCULATE (
        MAX ( 'Data 01'[Date] ),
        FILTER (
            ALLSELECTED(  'Data 01' ),
            AND ('Data 01'[Date] < CurrentDate,
            'Data 01'[PersonID] = CurrentPersonId
            )
        )
    )
RETURN
IF(MAX('Data 01'[Valid]) = "TRUE", DATEDIFF ( MaxPreviousDate, CurrentDate, DAY ))

vtangjiemsft_0-1731894717570.png

If you need the results to be static and unaffected by filtering, then the measures remain unchanged:

vtangjiemsft_1-1731894972123.png

If I have misunderstood your needs, please clarify in a follow-up reply.

 

Best Regards,

Neeko 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
SachinNandanwar
Impactful Individual
Impactful Individual

@verolure : Unsure what issue you are facing but your Diff Dates 2 code seem to get the desired output.

Anyways here is my approach.

[Valid Diff] =
    VAR _PersonId =
        MAX ( 'Data 01'[PersonID] )
    VAR _Date =
        CALCULATE ( MIN ( 'Data 01'[Date] ), 'Data 01'[Valid] = "TRUE" )
    VAR _tbl =
        FILTER (
            ALL ( 'Data 01' ),
            'Data 01'[PersonID] = _PersonId
                && 'Data 01'[Date] < _Date
        )
    RETURN
        ABS ( MAXX ( _tbl, ( DATEDIFF ( _Date, 'Data 01'[Date], DAY ) ) ) )

SachinNandanwar_0-1731782967706.png



Regards,
Sachin
Check out my Blog
verolure
New Member

Hi @v-tangjie-msft,

Thank you for the idea! However, the main issue is that the calculations are being performed even for the rows that are not valid, which I need to avoid. The filter was meant to prevent this, but since it didn't work as I expected, I implemented the validation in the Return instead.

Hi @verolure ,

 

With your description you need to filter some data, you can create a slicer visual object to filter, if you need the results to change dynamically then you can change the measure to:

Measure = 
VAR CurrentDate = MAX('Data 01'[Date])
VAR CurrentPersonId = MAX('Data 01'[PersonID])

VAR MaxPreviousDate =
    CALCULATE (
        MAX ( 'Data 01'[Date] ),
        FILTER (
            ALLSELECTED(  'Data 01' ),
            AND ('Data 01'[Date] < CurrentDate,
            'Data 01'[PersonID] = CurrentPersonId
            )
        )
    )
RETURN
IF(MAX('Data 01'[Valid]) = "TRUE", DATEDIFF ( MaxPreviousDate, CurrentDate, DAY ))

vtangjiemsft_0-1731894717570.png

If you need the results to be static and unaffected by filtering, then the measures remain unchanged:

vtangjiemsft_1-1731894972123.png

If I have misunderstood your needs, please clarify in a follow-up reply.

 

Best Regards,

Neeko Tang

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

v-tangjie-msft
Community Support
Community Support

Hi @verolure ,

 

Instead of calculated columns, you can create measures, which take up resources only at the time of calculation and will outperform calculated columns.

vtangjiemsft_0-1731640341045.png

 

Measure = 
VAR CurrentDate = MAX('Data 01'[Date])
VAR CurrentPersonId = MAX('Data 01'[PersonID])

VAR MaxPreviousDate =
    CALCULATE (
        MAX ( 'Data 01'[Date] ),
        FILTER (
            ALL ( 'Data 01' ),
            AND ('Data 01'[Date] < CurrentDate,
            'Data 01'[PersonID] = CurrentPersonId
            )
        )
    )
RETURN
IF(MAX('Data 01'[Valid]) = "TRUE", DATEDIFF ( MaxPreviousDate, CurrentDate, DAY ))

 

vtangjiemsft_2-1731640624351.png

If I have misunderstood your needs, please clarify in a follow-up reply.

 

Best Regards,

Neeko Tang

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

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!

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.