Forum Discussion
Calculation over rows with condition
- Anonymous8 years ago
Hi Anonymous,
If you want to get the summaried stop time you can try to add new measure with below formula:
Stop time(Summaried) = VAR temp = ADDCOLUMNS ( ALL ( Trips ), "Stoped Time", LOOKUPVALUE ( Trips[TripEndDateTime], Trips[VehicleID], [VehicleID], Trips[Date], [Date], Trips[Index], [Index] - 1 ) ) RETURN SUMX ( ADDCOLUMNS ( FILTER ( temp, [VehicleID] = MAX ( [VehicleID] ) && [Date] = MAX ( [Date] ) ), "Diff", DATEDIFF ( [Stoped Time], [TripStartDateTime], SECOND ) ), [Diff] )Regards,
Xiaoxin Sheng
Anonymous
That's impressive. I dindn't know that logic for grouping with criteria. That's awesome :smileysurprised:
For Number of Stops and Return Time, it works perfectly, as it grabs one value only.
However, for the Stop Time, it doesn't, because the calculation should somehow group (sum) all the partial stop times for each trip. With the Measure you proposed, I think we only get one value.
So, for each trip we should do the time difference between the connected values (diagonally) and sum them,
Is it possible to calculate the total Stop Time?
Thanks again, this is something I'll definitely use on future projects.
Hi Anonymous,
If you want to get the summaried stop time you can try to add new measure with below formula:
Stop time(Summaried) =
VAR temp =
ADDCOLUMNS (
ALL ( Trips ),
"Stoped Time", LOOKUPVALUE (
Trips[TripEndDateTime],
Trips[VehicleID], [VehicleID],
Trips[Date], [Date],
Trips[Index], [Index] - 1
)
)
RETURN
SUMX (
ADDCOLUMNS (
FILTER ( temp, [VehicleID] = MAX ( [VehicleID] ) && [Date] = MAX ( [Date] ) ),
"Diff", DATEDIFF ( [Stoped Time], [TripStartDateTime], SECOND )
),
[Diff]
)
Regards,
Xiaoxin Sheng
- Anonymous8 years agoNot applicable
Anonymous
Again, impressive and straight to the point. It works flawlessly!
Anyway, I didn't get one bit of the code:
SUMX ( ADDCOLUMNS ( FILTER ( temp, [VehicleID] = MAX ( [VehicleID] ) && [Date] = MAX ( [Date] ) ), "Diff", DATEDIFF ( [Stoped Time], [TripStartDateTime], SECOND ) ), [Diff] )If we filter according to Vehicle ID and Date, which makes perfect sense, I would expect the first "Stopped Value" to be wrong, as it comes from the previous entry (i.e. a different Date or Vehicle), but it doesn't happen to be like this, which is good for our purpose, but I don't get it. Let me show as if we did it in a table ourselves:
VehicleID TripStartDateTime TripEndDateTime Index Stopped Time
2585 02/10/2017 12:20:39 02/10/2017 12:23:04 1 NA 2585 02/10/2017 13:07:25 02/10/2017 13:13:36 2 02/10/2017 12:23:04 2585 03/10/2017 12:31:09 03/10/2017 12:37:25 3 02/10/2017 13:13:362585 03/10/2017 12:41:01 03/10/2017 12:44:56 4 03/10/2017 12:37:25 2585 03/10/2017 12:56:03 03/10/2017 13:01:31 5 03/10/2017 12:44:56 2585 03/10/2017 13:02:52 03/10/2017 13:07:28 6 03/10/2017 13:01:31 3815 02/10/2017 12:20:00 02/10/2017 12:24:17 7 03/10/2017 13:07:283815 02/10/2017 12:26:40 02/10/2017 12:34:41 8 02/10/2017 12:24:17 3815 02/10/2017 12:36:56 02/10/2017 12:44:22 9 02/10/2017 12:34:41 3815 02/10/2017 13:04:37 02/10/2017 13:12:54 10 02/10/2017 12:44:22 To finalize this topic, where am I failing on the logic?
Thanks again! :smileyhappy:
- Anonymous8 years agoNot applicable
Hi Anonymous,
>> I would expect the first "Stopped Value" to be wrong, as it comes from the previous entry (i.e. a different Date or Vehicle), but it doesn't happen to be like this, which is good for our purpose, but I don't get it.
Actually, you can use current date as filter condition to simply avoid this situation.
The bolded part is the conditions, I used current vehicle ID and current date as the filter.(Some of summary functions can used to get current row contents)
Regards,
Xiaoxin Sheng
- Anonymous7 years agoNot applicable
Hi Xiaoxin Sheng
I am new to BI . I have simlar requirement wherein need ot get ATM idletime in a column against ATM Machine wherein no deposit or withdrawal hapened during operations time . For example 1:00 Am to 4 AM if there is no deposit or withdrawal then the column is populated with no. of hours smachine remained idle i.e no cash movement
Also I need a measure to get number of all such idle machines .
Can you help for both requirements ?
Pls see the image