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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

DateDiff Measure

Im having trouble showing the correct calculation of days between stages.

Shown below measure / formula by DAX code.

 

Firstly I need to create a calculate column [Index By Q No]  to set index for every group using DAX below:

Index By Q No = RANKX(FILTER(Table1, Table1[Q No] = EARLIER(Table1[Q No])), RANKX(ALL(Table1), Table1[Last Modified]), , DESC, Dense)

Then create a calcualte column to calculate datediff which is your expected result:

DateDiff = 
VAR Previous_Date = CALCULATE(MAX(Table1[Last Modified]), FILTER(Table1, Table1[Index By Q No] = EARLIER(Table1[Index By Q No]) - 1 && Table1[Q No] = EARLIER(Table1[Q No])))
RETURN 
DATEDIFF(Previous_Date, Table1[Last Modified], DAY)

 

The output is as below.

 

The index By Q No is not in sequence and the date diff measure shows wrong calculation.

 

image.png

May I know which steps I did wrong ? How do I get the accurate Index By Q No in sequence ?

12 REPLIES 12
v-frfei-msft
Community Support
Community Support

Hi @Anonymous,

 

Based on my test, we can update the formulas as below to meet your requirement.

 

Index By Q No =
RANKX (
    FILTER ( Table1, Table1[Q No] = EARLIER ( Table1[Q No] ) ),
    RANKX ( ALL ( Table1 ), Table1[Last Modified] ),
    ,
    DESC,
    DENSE
)
Datenew =
VAR new =
    RANKX (
        FILTER ( Table1, Table1[Q No] = EARLIER ( Table1[Q No] ) ),
        RANKX ( ALL ( Table1 ), Table1[Last Modified] ),
        ,
        DESC,
        DENSE
    )
RETURN
    DATEDIFF (
        CALCULATE (
            MAX ( Table1[Last Modified] ),
            FILTER (
                Table1,
                Table1[Q No] = EARLIER ( Table1[Q No] )
                    && Table1[Index By Q No]
                    = new - 1
            )
        ),
        Table1[Last Modified],
        DAY
    )

Then we can get the result as we excepted.

Capture.PNG

 

For more details, please check the pbix as attached.

 

https://www.dropbox.com/s/lpreyfp6opekf5t/DateDiff%20Measure.pbix?dl=0

 

Regards,

Frank

 

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

@v-frfei-msft

Hi Frank,

 

Thank you for helping out. However, my source is from Salesforce Report database, therefore the formula for rankX and Datenew below will need to be revised.

 

Can you let me know how to work this formula out for my case as I am seeing grey out on some part. 

Shown below.

 

image.png

Hi @Anonymous,

 

What is Opp in your formula? And the EARLIER function could not be used in measure, it only could only be used in calculated column. Could you please share your pbix or sample data to me?

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

Hi @v-frfei-msft

 

I have message you the source file and the PBIX. Its the simplified version with propietary information removed.

Please refer and checked.

Feel free to advise how the formula will work for my case.

 

Thank you

 

Looking forward to your reply.

Anonymous
Not applicable

Hi @v-frfei-msft

 

Let  me know if any of your revised formula works.

I came to try the formula you provided and it does not work.

 

Thank You

Hi @Anonymous,

 

Based on your file that you shared. I create two calculated column using the formulas.

 

Index By Q No = 
RANKX (
    FILTER ( ALL(Stages), Stages[Q No] = EARLIER ( Stages[Q No] ) ),
    RANKX ( ALL ( Stages ), Stages[Last Modified] ),
    ,
    DESC,
    DENSE
)
Datenew = 
VAR new =
    RANKX (
        FILTER ( Stages, Stages[Q No] = EARLIER ( Stages[Q No] ) ),
        RANKX ( ALL ( Stages ), Stages[Last Modified] ),
        ,
        DESC,
        DENSE
    )
RETURN
    DATEDIFF (
        CALCULATE (
            MAX ( Stages[Last Modified] ),
            FILTER (
                Stages,
                Stages[Q No] = EARLIER ( Stages[Q No] )
                    && Stages[Index By Q No]
                    = new - 1
            )
        ),
        Stages[Last Modified],
        DAY
    )

Capture.PNG

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

Hi @v-frfei-msft,

 

I appreciate your reply. The results that I have when I used the latest formula still show the same error.

(refer below screenshot)

 

Refer below screenshot. The sequence should go by Stage 0, 1, 2, 3 ,4 ,5 ,6, 7 and not by Quote Number. 

Will changes index by stage helps ? let me know the revised formula.

 

 

image.png

 

Anonymous
Not applicable

@v-frfei-msft

 

Can you let me know if you are able to solve this issue?

How should we go about sequencing with the stage of the same quote numbers?

 

Only then can I have the accurate Datenew.

 

Please advise.

 

Thank You

Hi @Anonymous,

 

1,Create a calculated column

 

rank by Q no1 = RANKX(FILTER(ALL(Stages),Stages[Q No]=EARLIER(Stages[Q No])),Stages[Last Modified],,ASC,Dense)

2. Create a measure.

 

datediff = 
var dateto = MAX(Stages[Last Modified])
var datee = CALCULATE(MAX(Stages[Last Modified]),FILTER(ALLEXCEPT(Stages,Stages[Q No]),Stages[rank by Q no1]=MAX(Stages[rank by Q no1])-1))
return 
DATEDIFF(datee,dateto,DAY)

2.PNG

 

Also please find the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

  Hi @v-frfei-msft

 

Yet again the formula still produce an error results.

Refer below screenshot. Can we do a screen sharing and connect via skype session ?

This would speed up the process, and getting the right formula accurate soonest.

 

 

image.png

Anonymous
Not applicable

@v-frfei-msft 

 

May I have your answer and corrected solution soonest?

 

I will need the accurate measure soonest.

 

Thank You

Anonymous
Not applicable

Hi @v-frfei-msft,

 

Let me know when you have the solutions.

Appreciate your soonest reply.

 

Thank You

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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