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
Joel_sony
Frequent Visitor

How to find the number of days between rows

Hey guys! Can someone help me out how to create a new column which calculates the difference of days between each row similiar to the days_difference int this table?image.png

4 ACCEPTED SOLUTIONS
DataNinja777
Super User
Super User

Hi @Joel_sony

You can produce your required output by writing a calculated column using dax as follows:

 

days_difference dax = 
VAR CurrentIndex = 'Table'[Index]
VAR CurrentDate = 'Table'[created at]

// Find the previous date for the same ID using the previous index
VAR PreviousDate = 
    CALCULATE(
        MAX('Table'[created at]),
        FILTER(
            'Table',
            'Table'[Index] = CurrentIndex - 1
        )
    )

RETURN
    IF(
        ISBLANK(PreviousDate), 
        0, 
        DATEDIFF(PreviousDate, CurrentDate, DAY)
    )

 

The above dax will produce the last column as shown below:

DataNinja777_0-1731159861563.png

 

 

To solve this in Power BI, start by adding an index column in Power Query. Power Pivot DAX doesn’t retain the inherent row order during calculations, so adding an index column helps establish a fixed order for accurate calculations.

 

I have attached an example pbix file for your reference.

 

Best regards,

 

View solution in original post

PhilipTreacy
Super User
Super User

@Joel_sony 

 

Use Power Query to subtract the dates from each other.  First you need to add an Index column so you can refer to the previous row but then you just need to add a Custom Column with this

 

= try Duration.Days(Date.From([created_at]) - Date.From(#"Added Index"[created_at]{[Index]-1})) otherwise 0

 

and delete the Index column as it's no longer needed to give this

 

PhilipTreacy_0-1731160035545.png

 

This is the entire query which you can also download from this example PBIX file

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc/BCgIhEIDhVwnPK8you9q+iixh7RBCWbhW9PbpJcpswYMwH+OvtQwl69ghkks055sANByBo9igGsGMSrKps0yIPFySixXT3wwBi7vtzz695ZaD5ChrWTa64E7PJe0i3T09VnnpvFKYfTiuOpWnkS6Zrr7f97pZmr+lamn+l/7yAYZGqeESKoe6VZrP58LpBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, status = _t, created_at = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"status", type text}, {"created_at", type datetime}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "days_difference", each try Duration.Days(Date.From([created_at]) - Date.From(#"Added Index"[created_at]{[Index]-1})) otherwise 0),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"

 

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!


View solution in original post

ThxAlot
Super User
Super User

Easy enough,

ThxAlot_0-1731174624233.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Column = if(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[id]<EARLIER(Data[id])))=0,0,int(Data[created_at]-CALCULATE(MAX(Data[created_at]),FILTER(Data,Data[id]<EARLIER(Data[id])&&Data[created_at]<=EARLIER(Data[created_at])))))

Hope this helps.

Ashish_Mathur_0-1731209259804.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

13 REPLIES 13
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Column = if(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[id]<EARLIER(Data[id])))=0,0,int(Data[created_at]-CALCULATE(MAX(Data[created_at]),FILTER(Data,Data[id]<EARLIER(Data[id])&&Data[created_at]<=EARLIER(Data[created_at])))))

Hope this helps.

Ashish_Mathur_0-1731209259804.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks alot! @Ashish_Mathur 

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
ThxAlot
Super User
Super User

Easy enough,

ThxAlot_0-1731174624233.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



Thankyou @ThxAlot 

PhilipTreacy
Super User
Super User

@Joel_sony 

 

Use Power Query to subtract the dates from each other.  First you need to add an Index column so you can refer to the previous row but then you just need to add a Custom Column with this

 

= try Duration.Days(Date.From([created_at]) - Date.From(#"Added Index"[created_at]{[Index]-1})) otherwise 0

 

and delete the Index column as it's no longer needed to give this

 

PhilipTreacy_0-1731160035545.png

 

This is the entire query which you can also download from this example PBIX file

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc/BCgIhEIDhVwnPK8you9q+iixh7RBCWbhW9PbpJcpswYMwH+OvtQwl69ghkks055sANByBo9igGsGMSrKps0yIPFySixXT3wwBi7vtzz695ZaD5ChrWTa64E7PJe0i3T09VnnpvFKYfTiuOpWnkS6Zrr7f97pZmr+lamn+l/7yAYZGqeESKoe6VZrP58LpBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, status = _t, created_at = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"id", Int64.Type}, {"status", type text}, {"created_at", type datetime}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "days_difference", each try Duration.Days(Date.From([created_at]) - Date.From(#"Added Index"[created_at]{[Index]-1})) otherwise 0),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"

 

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!


Thankyou @PhilipTreacy !

DataNinja777
Super User
Super User

Hi @Joel_sony

You can produce your required output by writing a calculated column using dax as follows:

 

days_difference dax = 
VAR CurrentIndex = 'Table'[Index]
VAR CurrentDate = 'Table'[created at]

// Find the previous date for the same ID using the previous index
VAR PreviousDate = 
    CALCULATE(
        MAX('Table'[created at]),
        FILTER(
            'Table',
            'Table'[Index] = CurrentIndex - 1
        )
    )

RETURN
    IF(
        ISBLANK(PreviousDate), 
        0, 
        DATEDIFF(PreviousDate, CurrentDate, DAY)
    )

 

The above dax will produce the last column as shown below:

DataNinja777_0-1731159861563.png

 

 

To solve this in Power BI, start by adding an index column in Power Query. Power Pivot DAX doesn’t retain the inherent row order during calculations, so adding an index column helps establish a fixed order for accurate calculations.

 

I have attached an example pbix file for your reference.

 

Best regards,

 

Thankyou @DataNinja777 

muhammad_786_1
Resolver V
Resolver V

Hi @Joel_sony 

You can try this DAX to get your desired outcome:

days_difference = 
VAR CurrentDate = 'Table'[created_at]

VAR PreviousDate = 
    CALCULATE(
        MAX('Table'[created_at]),
        FILTER(
            'Table',
            'Table'[created_at] <= EARLIER('Table'[created_at]) &&
            'Table'[id] < EARLIER('Table'[id])
        )
    )

RETURN
IF(
    ISBLANK(PreviousDate),
    0,
    IF(CurrentDate = PreviousDate, 
        0, 
        DATEDIFF(PreviousDate, CurrentDate, DAY)
    )
)

 

muhammad_786_1_0-1731160033920.png

 

Best Regards,
Muhammad Yousaf

 

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

 

LinkedIn

 

Thankyou @muhammad_786_1 ! Great help!

Hakuna_matata
Frequent Visitor

Hi @Joel_sony ,
We can achieve this by the below Dax creating a calculated column
Days_Difference =
VAR CurrentDate = 'Table'[Date]
VAR PreviousDate =
CALCULATE(
MAX('Table'[Date]),
FILTER(
'Table',
'Table'[Date] < CurrentDate
)
)
RETURN
IF(ISBLANK(PreviousDate), BLANK(), DATEDIFF(PreviousDate, CurrentDate, DAY))

Hakuna_matata_0-1731158677557.png


Please mark this as a solution if this help. Hope it helps!



Thankyou @Hakuna_matata !

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.