Forum Discussion
I calculate in hours between two different record dates
Hello again
I just tested the code.
I have gone line by line returning the value of each variable with a return, when I have come to return the value of the PreviousEnd variable, it returns all the values of the empty column and it is this variable that slows everything down, it takes about 2 minutes to refresh the report.
Any suggestions? I hope we can find that.
Thank you very much for your help
Syndicate_Admin RWRW
Please try
Time Measure =
VAR SelectedTable = FILTER ( ALLSELECTED ( Orders[Start Date], Orders[End Date] ), Orders[End Date] <> BLANK ( ) )
VAR CurrentStart= MAX ( Orders[Start Date] )
VAR PreviousEnd = MAXX ( FILTER ( SelectedTable, Orders[Start Date] < CurrentStart ), Orders[End Date] )
VAR TimeDifference = DATEDIFF ( PreviousEnd, CurrentStart, MINUTE )
VAR Result = DIVIDE ( TimeDifference, 60 )
RETURN
Result- Syndicate_Admin4 years ago
Administrator
The same thing continues to happen to me. I need that without the mesasure of INdateRange I keep the records whose time is White, I always mean the first record of the table will be white since it will be the first order depending on the selected dates.
With the InRangeDate measure
without the InRangeDate measure
I would need the final solution to come out like this
I hope I have explained, I am waiting
Thank you very much for your help
- Syndicate_Admin4 years ago
Administrator
Hello again
Test the code and when I return this line the Time Measure column is displayed like this.
WHERE PreviousEnd = MAXX ( FILTER ( SelectedTable, Pmx[BDL_SIG_TIMESTAMP] < CurrentStart ), Pmx[CPK_SIG_TIMESTAMP] )
Another thing that keeps happening is when I press the Report button that is on the left hand side in Power Bi, a load icon appears at the top left of the visual and it takes about 2 minutes to load into the table.Thanks a lot
- Syndicate_Admin4 years ago
Administrator
I refer to this line
VAR PreviousEnd = MAXX ( FILTER ( SelectedTable, Orders[Start Date] < CurrentStart ), Orders[End Date] )
Rename in my code the table and the fields but it is still the same.
Thank you
- Syndicate_Admin4 years ago
Administrator
Good morning
I will try to explain myself as best as possible. I've been doing a lot of testing and I've figured out what's going on.
As I told you yesterday, I have a measure called InDateRange that what it does is put a 0 (to those records that are not in the date range) and a 1 (records that are between the selected dates). So far so good.
Then in the visual I annotate it in the filters and I indicate that InDateRange is 1 so that it only shows me those that are in range. I leave you a screenshot of how the table looks and the InDateRange filter.
The columns I have associated with the table are as follows:
and that's how it looks on the screen
My surprise comes now, the measure called InDateRange I do not need to be displayed in the table so I remove the column from the visual and the following happens in the data that is shown.
Now the columns are as follows:
And when you remove the measure in the columns of the table, now the data looks like this
It removes the first row of the table.
As for the code of the calculation of time I am using the last one you passed me and it works correctly.
Time Measure =// Measure to calculate the time difference between the completion of one batch and the beginning of the next batchWHERE SelectedTable = FILTER ( ALLSELECTED ( Pmx[Start Date], Pmx[End Date] ), Pmx[End Date] <> BLANK ( ) )WHERE CurrentStart= MAX(Pmx[Start Date])WHERE PreviousEnd =MAXX(FILTER (SelectedTable, Pmx[Start Date] < CurrentStart ),Pmx[End Date])WHERE TimeDifference = DATEDIFF ( PreviousEnd, CurrentStart, MINUTE )WHERE Result = DIVIDE ( TimeDifference, 60 )RETURNResultI hope you explained me correctly and you can help me solve it.
Thank you very much and I am waiting
- tamerj14 years ago
Community Champion
Hi Syndicate_Admin RWRW
Yes becuase power bi automatically hides the blank measure value. We can use the following simple trickTime Measure = // Measure to calculate the time difference between the completion of one batch and the beginning of the next batch VAR SelectedTable = FILTER ( ALLSELECTED ( Pmx[Start Date], Pmx[End Date] ), Pmx[End Date] <> BLANK () ) VAR CurrentStart = MAX ( Pmx[Start Date] ) VAR PreviousEnd = MAXX ( FILTER ( SelectedTable, Pmx[Start Date] < CurrentStart ), Pmx[End Date] ) VAR TimeDifference = DATEDIFF ( PreviousEnd, CurrentStart, MINUTE ) VAR Result = DIVIDE ( TimeDifference, 60, " " ) RETURN Result - tamerj14 years ago
Community Champion
Hi Syndicate_Admin , RWRW
I hope the following solves the problemTime Measure = // Measure to calculate the time difference between the completion of one batch and the beginning of the next batch VAR SelectedTable = FILTER ( ALLSELECTED ( Pmx[Start Date], Pmx[End Date] ), Pmx[End Date] <> BLANK () ) VAR CurrentStart = MAX ( Pmx[Start Date] ) VAR PreviousDatesTable = FILTER ( SelectedTable, Pmx[Start Date] < CurrentStart ) VAR PreviousEnd = MAXX ( PreviousDatesTable, Pmx[End Date] ) VAR FirstSelectedDate = MINX ( PreviousDatesTable, Pmx[End Date] ) VAR TimeDifference = DATEDIFF ( PreviousEnd, CurrentStart, MINUTE ) VAR Result = DIVIDE ( TimeDifference, 60, " " ) RETURN IF ( PreviousEnd = FirstSelectedDate, " ", Result )