Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreShape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.
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?
Solved! Go to Solution.
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:
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,
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
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
Proud to be a Super User!
Easy enough,
Expertise = List.Accumulate( {Days as from Today}, {Skills and Knowledge}, (Current, Everyday) => Current & Day.LeanAndPractise(Everyday) ) |
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.
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.
You are welcome.
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
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
Proud to be a 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:
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,
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)
)
)
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.
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))
Please mark this as a solution if this help. Hope it helps!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Check out the November 2024 Power BI update to learn about new features.
User | Count |
---|---|
94 | |
90 | |
83 | |
76 | |
49 |
User | Count |
---|---|
145 | |
140 | |
109 | |
68 | |
55 |