Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

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
1 ACCEPTED SOLUTION

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.

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

@Anonymous - Should be something like:

Column = 
  VAR __Start = [Timestart]
  VAR __End = [Time + 10 Minutes]
RETURN
  COUNTROWS([Timestamp]>=__Start && [Timestamp]<=__End)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
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!
 

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.

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors