Forum Discussion

amit_maniyar's avatar
amit_maniyar
Icon for Helper I rankHelper I
3 years ago
Solved

Trying to get count - Point in time based on 2 datetime fields

I have a scenario based on given datase

I am trying to count valid Ids at point in time say Yesterday, today, today -7 days - on hourly line graph

 

Test Cases

Yesterday (13 Feb 2022 at 3 pm) , how many records were valid ?  (any id which has start date prior to that and end date later to that will be counted as valid


other info
Same applies for today - 7 days counts - on hourly basis

Any record with null enddate is valid Starting from Startdatetime

 

 

IDStart DatetimeEnd Datetime
12/14/23 11:01 
22/14/23 10:58 
32/14/23 10:55 
42/14/23 10:53 
52/14/23 10:52 
62/14/23 10:49 
72/14/23 10:46 
82/14/23 10:43 
92/14/23 10:41 
102/14/23 10:37 
112/14/23 10:35 
122/14/23 10:31 
132/14/23 10:30 
142/14/23 10:24 
152/14/23 10:21 
162/14/23 10:20 
172/14/23 10:15 
182/14/23 10:15 
192/14/23 10:08 
202/14/23 10:05 
212/14/23 10:00 
222/14/23 9:58 
232/14/23 9:53 
242/14/23 9:51 
252/14/23 9:492/14/23 11:01
262/14/23 9:442/14/23 11:00
272/14/23 9:442/14/23 10:59
282/14/23 9:38 
292/14/23 9:36 
302/14/23 9:32 
312/14/23 9:272/14/23 10:35
322/14/23 9:272/14/23 10:59
332/14/23 9:22 
342/14/23 9:08 
352/14/23 9:07 
362/14/23 9:032/14/23 10:32
372/14/23 9:01 
382/14/23 8:57 
392/14/23 8:572/14/23 10:34
402/14/23 8:532/14/23 10:33
412/14/23 8:50 
422/14/23 8:47 
432/14/23 8:41 
442/14/23 8:38 
452/14/23 8:342/14/23 10:31
462/14/23 8:212/14/23 10:35
472/14/23 8:192/14/23 10:34
482/14/23 7:512/14/23 10:36
492/14/23 7:442/14/23 10:33
502/14/23 7:35 
512/14/23 7:30 
522/14/23 7:292/14/23 11:00
532/14/23 7:182/14/23 10:58
542/14/23 7:02 
552/14/23 6:582/14/23 10:58
562/14/23 6:542/14/23 10:35
572/14/23 6:512/14/23 10:36
582/14/23 6:18 
592/14/23 6:152/14/23 11:00
602/14/23 6:112/14/23 7:17
612/14/23 5:352/14/23 11:00
622/14/23 5:312/14/23 10:36
632/14/23 5:282/14/23 8:38
642/14/23 4:522/14/23 5:43
652/14/23 3:152/14/23 7:25
662/14/23 2:592/14/23 7:26
672/14/23 2:54 
682/14/23 2:512/14/23 6:01
692/14/23 2:402/14/23 6:02
702/14/23 1:352/14/23 7:25
712/14/23 1:322/14/23 7:26
722/14/23 1:232/14/23 5:07
732/14/23 1:212/14/23 5:59
742/14/23 0:492/14/23 6:00
752/14/23 0:452/14/23 7:51
762/14/23 0:382/14/23 7:22
772/14/23 0:362/14/23 4:37
782/14/23 0:322/14/23 7:24
792/14/23 0:32 
  • Hi amit_maniyar ,

     

    Please try:

    First create a new table:

    Use [Time] as x-axis

    Then apply the measure to the line chart:

    _Today = 
    var _a = TODAY()+MAX('Time'[Time])
    return COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])
    
    _Today-7 = 
    var _a = TODAY()-7+MAX('Time'[Time])
    return COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])
    
    _Yesterday = 
    var _a = TODAY()-1+MAX('Time'[Time])
    return COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])

    Final output:

     

     

    Best Regards,

    Jianbo Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Hi amit_maniyar ,

     

    Sorry I misunderstood you before, you don't need to add measure to the visual level filter if you only want to limit today's display, you just need to change today's measure:

    _Today = 
    var _a = TODAY()+MAX('Time'[Time])
    var _b =  COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])
    return IF(MAX('Time'[Time])<=TIME(HOUR(NOW()),0,0),_b)

    Best Regards,

    Jianbo Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

6 Replies

  • Hi amit_maniyar ,

     

    Please try:

    First create a new table:

    Use [Time] as x-axis

    Then apply the measure to the line chart:

    _Today = 
    var _a = TODAY()+MAX('Time'[Time])
    return COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])
    
    _Today-7 = 
    var _a = TODAY()-7+MAX('Time'[Time])
    return COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])
    
    _Yesterday = 
    var _a = TODAY()-1+MAX('Time'[Time])
    return COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])

    Final output:

     

     

    Best Regards,

    Jianbo Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • amit_maniyar's avatar
      amit_maniyar
      Icon for Helper I rankHelper I

      Hi v-jianboli-msft ,

      I dont want to show the streight line for today's counts. I tried to appy this to visual filter, but the Yesterday and today - 7 - rest of the hours data also wont show up.

       

       

      Any help is appreciated! thanks so much !

      Amit

  • Hi amit_maniyar ,

     

    Sorry I misunderstood you before, you don't need to add measure to the visual level filter if you only want to limit today's display, you just need to change today's measure:

    _Today = 
    var _a = TODAY()+MAX('Time'[Time])
    var _b =  COUNTX(FILTER('Table',[Start Datetime]<=_a&&OR([End Datetime]>=_a,ISBLANK([End Datetime]))),[ID])
    return IF(MAX('Time'[Time])<=TIME(HOUR(NOW()),0,0),_b)

    Best Regards,

    Jianbo Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

  • Thanks v-jianboli-msft.

    Logic appears to be very clear to me. 

     

    I am surprised to know what can I do if I have a dimtime table already linked with my startdatetime field. 

    right now, I created a separate time table (as you did, thanks). which dont need a relationship.

     

     

  • Hi v-jianboli-msft , 

     

    You are genius. it worked really great for my other dataset as well. 

    Although I have a question about Today's line as per attached the image below.

     

    I am wondering if we can somehow restrict today's line would be upto now (current hour).

    Right now, its a constant number going across all the hours.

     

     

     

    Appreciate your help!!

    Thanks, 

    Amit