Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Count rows if between two time slots

Dear all,

 

I have the following issue which I think should be relatively easy to solve, but I have not found a solution yet. Currently I have below table in excel but I would like to get the same result (Column F) in a query in PowerBI.  Basically it comes down to this, Column A is a long list of events happening on the specified date/time. I would like to count how many events happen (F) in a timeframe of 10 minutes (between C and D). Some helper columns are made to come to the result in column F (Column B en E)

 

In Excel the following calculations are applied, but perhaps the calcaluation could be made way easier in PowerBI. Hope somebody could help me out with this one! If more info is needed please let me know.

 

A = raw data

B = VLOOKUP(A2;C$2:E$8527;3)

C = A

D = A + 10 minutes

E = Index column

F= COUNTIF(B:B;E2)

 

Column AColumnBColumnCColumnDColumnEColumnF
TimestampCountTimestartTime + 10 minutesIndexSlice
count
1-09-20 0:00:0411:9:2020 00:00:041:9:2020 00:10:0411
1-09-20 0:02:4821:9:2020 00:02:481:9:2020 00:12:4821
1-09-20 0:14:0731:9:2020 00:14:071:9:2020 00:24:0731
1-09-20 0:24:4941:9:2020 00:24:071:9:2020 00:34:0743
1-09-20 0:27:0441:9:2020 00:34:071:9:2020 00:44:0751
1-09-20 0:31:3741:9:2020 00:44:071:9:2020 00:54:0760
1-09-20 0:35:0451:9:2020 00:54:071:9:2020 01:04:0770
1-09-20 1:15:3591:9:2020 01:04:071:9:2020 01:14:0780
1-09-20 1:19:2391:9:2020 01:14:071:9:2020 01:24:0792
1-09-20 1:24:08101:9:2020 01:24:071:9:2020 01:34:07103
1-09-20 1:25:45101:9:2020 01:34:071:9:2020 01:44:07111
1-09-20 1:33:30101:9:2020 01:44:071:9:2020 01:54:07121
  • v-yingjl's avatar
    v-yingjl
    5 years ago

    Hi Anonymous ,

    You can modify the formula like this:

    Column =
    VAR __Start = [Timestart]
    VAR __End = [Time + 10 Minutes]
    VAR _count =
        COUNTROWS (
            FILTER ( ALL ( 'table' ), [Timestamp] >= __Start && [Timestamp] <= __End )
        )
    RETURN
        IF ( ISBLANK ( _count ), 0, _count )
    

     

    Best Regards,
    Yingjie Li

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

3 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    Anonymous - Should be something like:

    Column = 
      VAR __Start = [Timestart]
      VAR __End = [Time + 10 Minutes]
    RETURN
      COUNTROWS([Timestamp]>=__Start && [Timestamp]<=__End)
    • Anonymous's avatar
      Anonymous
      Not applicable

      Dear Greg_Deckler 

       

      Thanks for your quick reply! Your solution translates as following in my powerBI file:

       

      Column =
      VAR __Start = 'KPI-Export-2020-09'[Start]
      VAR __End = 'KPI-Export-2020-09'[End]
      RETURN
      COUNTROWS('KPI-Export-2020-09'[Date/time]'>=__Start && 'KPI-Export-2020-09'[Date/time]<=__End)
       
      However, I get the following error:
      Syntax for '>=__Start&& is not correct
       
      Do you perhaps know how I should re-write the syntax? I've tried a couple of things but not succesful unfortunately.
       
      Best regards!
       
      • v-yingjl's avatar
        v-yingjl
        Community Support

        Hi Anonymous ,

        You can modify the formula like this:

        Column =
        VAR __Start = [Timestart]
        VAR __End = [Time + 10 Minutes]
        VAR _count =
            COUNTROWS (
                FILTER ( ALL ( 'table' ), [Timestamp] >= __Start && [Timestamp] <= __End )
            )
        RETURN
            IF ( ISBLANK ( _count ), 0, _count )
        

         

        Best Regards,
        Yingjie Li

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