Forum Discussion
Get date delta between 2 rows
- 3 years ago
the issue could be with earlier. let's try without earlier.
before trying this, make sure Ensure that the 'DateTime' column in your data is indeed of DateTime data type. If it is a string, the DATEDIFF function might not work as expected.
Verify that for each "Connect" event, there is a corresponding "Disconnect" event. If not, the calculation would not have a disconnect time to subtract from, and this may result in blank values.
lastly, check for case sensitivity. write exactly what is stored in your column.
// Add an index column to 'YourTable' using Power Query
// You should add this column before loading the data into Power BI
Index = Table.AddIndexColumn('YourTable', "Index", 1, 1)// DAX measure
Usage Time =
SUMX(
FILTER(
'YourTable',
'YourTable'[Event] = "Disconnect"
),
VAR CurrentIndex = 'YourTable'[Index]
VAR CurrentDevice = 'YourTable'[DeviceIP]
VAR ConnectTime =
CALCULATE(
MAX('YourTable'[DateTime]),
FILTER(
'YourTable',
'YourTable'[Event] = "Connect" &&
'YourTable'[DeviceIP] = CurrentDevice &&
'YourTable'[Index] < CurrentIndex
)
)
RETURN
DATEDIFF(
ConnectTime,
'YourTable'[DateTime],
MINUTE
)
)
Hi, alexmoller96
try using date diff and minx
example code
Usage Time = SUMX(
FILTER(
'YourTable',
'YourTable'[Event] = "Disconnect"
),
DATEDIFF(
MINX(
FILTER(
'YourTable',
'YourTable'[Event] = "Connect" &&
'YourTable'[DeviceIP] = EARLIER('YourTable'[DeviceIP]) &&
'YourTable'[DateTime] < EARLIER('YourTable'[DateTime])
),
'YourTable'[DateTime]
),
'YourTable'[DateTime],
MINUTE
)
)
also this thread should be helpful
Solved: Re: Show text for date range between 45 days and 5... - Microsoft Fabric Community
Hi rubayatyasmin, thanks a lot for your reply. I tried using the code that you supplied but dont get any values, am I missing something?
- rubayatyasmin3 years agoCommunity Champion
the issue could be with earlier. let's try without earlier.
before trying this, make sure Ensure that the 'DateTime' column in your data is indeed of DateTime data type. If it is a string, the DATEDIFF function might not work as expected.
Verify that for each "Connect" event, there is a corresponding "Disconnect" event. If not, the calculation would not have a disconnect time to subtract from, and this may result in blank values.
lastly, check for case sensitivity. write exactly what is stored in your column.
// Add an index column to 'YourTable' using Power Query
// You should add this column before loading the data into Power BI
Index = Table.AddIndexColumn('YourTable', "Index", 1, 1)// DAX measure
Usage Time =
SUMX(
FILTER(
'YourTable',
'YourTable'[Event] = "Disconnect"
),
VAR CurrentIndex = 'YourTable'[Index]
VAR CurrentDevice = 'YourTable'[DeviceIP]
VAR ConnectTime =
CALCULATE(
MAX('YourTable'[DateTime]),
FILTER(
'YourTable',
'YourTable'[Event] = "Connect" &&
'YourTable'[DeviceIP] = CurrentDevice &&
'YourTable'[Index] < CurrentIndex
)
)
RETURN
DATEDIFF(
ConnectTime,
'YourTable'[DateTime],
MINUTE
)
)- alexmoller963 years agoRegular Visitor
Great - okay so now I get a value, but they all have the value of 1 - is this a formatting issue?
- alexmoller963 years agoRegular Visitor
Okay - I think I understand after looking at DATEDIFF - If I use SECOND instead of MINUTE, it gives me the number of seconds that the device was in use for - thanks a lot!!!!
rubayatyasmin subsequent question - is there a way that I can filter this by date so I can get a value for usage time by day