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
jobf
Helper I
Helper I

Difference between set of dates on the same column

Hello. I need to calculate the difference between the dates of different applications:

 

Field Application  Date
P1  1st01/01/2024
P2  1st01/05/2024
P1  2nd01/20/2024
P2  2nd01/22/2024
P1  3rd02/10/2024
P2  3rd02/11/2024
P1  1st02/15/2024
P2  1st02/15/2024
P1  2nd02/27/2024
P2  2nd02/28/2024
P1  3rd03/10/2024
P2  3rd03/11/2024
P1  1st03/14/2024
P2  1st03/16/2024
P1  2nd03/20/2024
P2  2nd03/24/2024
P1  3rd04/02/2024

P2

  3rd

04/05/2024


For example, I need to take the dates of the 2nd application of P1, select the oldest of them, and subtract the oldest date of the 1st application of P1. Therefore, I need to take the dates of the 3rd application of P1, select the oldest of them, and subtract by the oldest date of the 2nd application of P1. In the truth table, there will be more types of application, such as 4th and 5th. And so on with all Fields.

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Thanks for the reply from @vigneshba , please allow me to provide another insight: 

 

Hi  @jobf ,

 

Here are the steps you can follow:

1. Create calculated column.

Index =
VALUE(
LEFT(
    'Table'[Application],LEN('Table'[Application])-2))

vyangliumsft_0-1720163553787.png

2. Create measure.

Measure =
var _current=
MAXX(
    FILTER(ALL('Table'),
    'Table'[Field]=MAX('Table'[Field])&&'Table'[Index]=MAX('Table'[Index])),[Date])
var _last=
MAXX(
    FILTER(ALL('Table'),
    'Table'[Field]=MAX('Table'[Field])&&'Table'[Index]=MAX('Table'[Index])-1),[Date])
return
DATEDIFF(
    _last,_current,DAY)

3. Result:

vyangliumsft_1-1720163553789.png

 

Best Regards,

Liu Yang

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

5 REPLIES 5
v-yangliu-msft
Community Support
Community Support

Thanks for the reply from @vigneshba , please allow me to provide another insight: 

 

Hi  @jobf ,

 

Here are the steps you can follow:

1. Create calculated column.

Index =
VALUE(
LEFT(
    'Table'[Application],LEN('Table'[Application])-2))

vyangliumsft_0-1720163553787.png

2. Create measure.

Measure =
var _current=
MAXX(
    FILTER(ALL('Table'),
    'Table'[Field]=MAX('Table'[Field])&&'Table'[Index]=MAX('Table'[Index])),[Date])
var _last=
MAXX(
    FILTER(ALL('Table'),
    'Table'[Field]=MAX('Table'[Field])&&'Table'[Index]=MAX('Table'[Index])-1),[Date])
return
DATEDIFF(
    _last,_current,DAY)

3. Result:

vyangliumsft_1-1720163553789.png

 

Best Regards,

Liu Yang

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

 

Hello. Thanks, your solution worked. I just have one question. In the true table, for example, there are some moments where there is a 1st application and 3rd application, but there is no 2nd application. I would like to know if there is a possibility of changing the measure so that, if the previous application (-1) does not exist, it subtracts from the column that came before it (-2).

There was an error. DAX comparison operations do not support comparing values of type Text with values of type Number. Consider using the VALUE or FORMAT function to convert one of the values.

vigneshba
Frequent Visitor

Hi @jobf - I hope you are expecting below result

vigneshba_3-1720121599760.png

Please follow below steps to achieve this.

I have just loaded the same dataset in Power BI and created a reference in Power Query and grouped this table with Field and Application to get Oldest Date in Power BI. I have created reference here not to disturb the Original Data but we can apply group by directly on the Original Data

vigneshba_0-1720121291005.png

vigneshba_1-1720121388680.png

vigneshba_2-1720121529015.png

You have to create 1 Calculated Measurement which I have created as Prior Oldest Date here to get the prior date for every group (Field and Application) and first item it will be blank as there are no prior dates available

Prior Oldest Date = CALCULATE(MAX('Use Case'[Oldest Date]), FILTER(ALLEXCEPT('Use Case', 'Use Case'[Field]), 'Use Case'[Oldest Date]<EARLIER('Use Case'[Oldest Date])))
You have to create one more Calculated Dimension to find the Date Difference between these 2 dates now

Date Difference = DATEDIFF('Use Case'[Prior Oldest Date], 'Use Case'[Oldest Date], DAY)
 
Hope this helps you to do date difference between the dates in the same column


@jobf - Have you tried this approach?

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.