Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago

Between dates on previous rows to then exclude, include, or include the extension of time.

Hi all,

 

I'm a little stumped.

 

I need to come up with a query to exclude an entry where :

For the same 'ClientN' value, if the Start Time is after a previous start time AND before a previous End Time, then it's excluded (mentioned below as exclude)

But, if a second time extends beyond the first entry, then the extension of the time is counted (mentioned in the table as 'partial include').

 

Below is some data that has had this rule manually applied.

 

I was planning on using PowerQuery for this, but I am open to other ideas.

 

The premise is to remove instances of 'double counts' against the same project/client.

 

 

Start TimeEnd TimeDurationClientNProjectNDecision
6/01/2022 12:306/01/2022 13:120.00:42:00Client1Project6include
6/01/2022 13:146/01/2022 13:440.00:30:18Client1Project6include
6/01/2022 13:446/01/2022 14:180.00:33:36Client2Project3include
6/01/2022 14:186/01/2022 15:050.00:47:24Client1Project6include
6/01/2022 15:126/01/2022 15:390.00:26:06Client2Project3include
6/01/2022 21:006/01/2022 22:000.01:00:00Client2Project5include
7/01/2022 8:307/01/2022 9:000.00:30:00Client1Project1include
7/01/2022 12:007/01/2022 13:000.01:00:00Client1Project7include
7/01/2022 13:307/01/2022 14:300.01:00:00Client1Project6include
7/01/2022 17:107/01/2022 17:420.00:32:12Client2Project8include
10/01/2022 13:4510/01/2022 14:450.01:00:00Client1Project6include
10/01/2022 13:4610/01/2022 13:590.00:12:53Client1Project1exclude
10/01/2022 13:5910/01/2022 14:000.00:01:20Client2Project2include
10/01/2022 14:0010/01/2022 14:090.00:08:18Client1Project1exclude
10/01/2022 14:0910/01/2022 14:100.00:01:20Client2Project2include
10/01/2022 14:0910/01/2022 14:110.00:02:02Client1Project1exclude
10/01/2022 14:1110/01/2022 14:120.00:00:38Client2Project2include
10/01/2022 14:1210/01/2022 14:310.00:19:36Client1Project1exclude
10/01/2022 14:3110/01/2022 14:320.00:00:59Client2Project2include
10/01/2022 14:3210/01/2022 14:320.00:00:02Client1Project1exclude
10/01/2022 14:3210/01/2022 14:330.00:01:12Client2Project2include
10/01/2022 14:3310/01/2022 14:340.00:00:59Client1Project1exclude
10/01/2022 14:3410/01/2022 14:390.00:04:20Client2Project2include
10/01/2022 14:3910/01/2022 14:420.00:03:14Client1Project1exclude
10/01/2022 14:4310/01/2022 14:550.00:12:14Client1Project1partial include

4 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    Anonymous Not sure how to do that in Power Query although I am sure there is a way. In DAX, you could do this using the approach used by MTBF. In your case, grab the MAXX and MINX values for Start and End for all previous rows (might want an Index column). Then you could compare and decide include or exclude.

    See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
    The basic pattern is:
    Column = 
      VAR __Current = [Value]
      VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

      VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
    RETURN
      __Current - __Previous

    • Anonymous's avatar
      Anonymous
      Not applicable

      Greg_Deckler Thanks Greg for your reply.

       

      I've attempted to use what you've provided, but it's not quite there yet! 

       

      Column =
      VAR __Current = [end time]
      VAR __PreviousDate = MAXX(FILTER('detailedreportextracts__small',[start Time] < EARLIER([start Time] ) && [ClientN] = earlier([ClientN]) ),[start Time])
      var __Previous = MAXX(filter(detailedreportextracts__small,[start Time]= __PreviousDate),[end Time])
      return
      __Current - __Previous

       

      The issue appears to be that it only looks back one data row.

      so, where I see a negative value on the first exclusion (this is correct), I would also *think* I would see negative values below this line on the lines that I have marked as 'exclude'.

       

      I might have the columns in the wrong spot in the formula. I did swap them around, but then the row calculated value didn't work for me.

       

       

       Thanks for your help with this.

       

      Is there a reason why you would use DAX instead of PowerQuery in this instance? (or any instances, really)

      I've primarily used powerquery as it works within excel and powerbi. Since you posted I've since read that you can use Dax within PowerPivot. 

  • Anonymous's avatar
    Anonymous
    Not applicable

    Greg_Deckler  Hi Greg,

    I also noticed you mentioned MINX in your reply, but it wasn't in your suggested code.

    Any chance you could take another look?


    Edit: added the below after playing around for another hour or so without much luck.

    I've been working to pull apart the DAX queries as they stand so I can understand them better.
    I'm not sure if its the best way to do it, but I'm trying to extract the 2:45pm (circled) into another column, to then check if the [end time] is < the earlier larger end time.
    But for whatever reason, the 'earlier' function doesn't seem to look back very many rows earlier.

     

    btw, I came across your LinkedIn pulse articles, very very amusing! 

  • v-yingjl's avatar
    v-yingjl
    Community Support

    Hi Anonymous ,

    Based on your sample data, could you please share more details about it? For example:

    How did you get the Decision column manually? by the current default Start Time sort or else?

    In this table, I have only filtered Client1 and sort the table by the Start Time, but seems like the result is something strange.

     

    For the row Start Time = "10/1/2022 2:00:00 PM", it is after its previous Start Time '10/1/2022 1:46:00 PM' but it is also after its previous End Time '10/1/2022 1:59:00 PM' so whether the decision of this row should be 'include' instead of 'exclude', or am I understand it wrong?

     

    But, if a second time extends beyond the first entry, then the extension of the time is counted (mentioned in the table as 'partial include').

    In addition, could you explain it in details combined with the above table that how to get the 'partial include' result for the last row?

     

    Best Regards,
    Community Support Team _ Yingjie Li