cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Pedro503
Resolver I
Resolver I

Count events between two dates

Hey there

 

I'm facing a situation where I must calculate how many occurrences (from the Fact table) took place between two dates (that also comes from my Fact table).

 

Screenshot 2022-10-11 021918.png

 

This is my fact table, and from here I must count how many transactions took place between the EventStart and EventEnd, but inserting this onto a matrix panel that contains a hierarchy-format date (from day > week > month > quarter > year). However, this must be calculated taking into consideration the end of the date in the row (e.g. 31 January, 2020 , 29 Feb., 2020 and so on). The next image has an example of what I must calculate, so on May/20, there were 128 active transactions, i.e., transactions that started before the last date of May, and lasted longer than the end of the month (128 transactions that started before May 31, 2020 and ended after May 31, 2020). Althoug I must calculate this by using the end of the month as a reference, my Calendar table uses the start date, so I'd say it's necessary to create a condition to return the end of month (MAX('Calendar'[Date]) or whatever date is selected on the hierarchy.

 

Pedro503_1-1665464258415.png

These numbers were created out of my imagination and does not stands for the actual calculation.

 

I've seen some posts like this one, but none of them had exactly what I aim to achieve. 

 

Here's the sample file.

 

(@tamerj1 do you know how I could do that?)

 

 

Thanks in advance.

1 ACCEPTED SOLUTION

This piece of code solved my problem:

 

COUNTROWS(FILTER(ALL(Data), MAX('Calendar'[Date]) > [EventStart] && MAX('Calendar'[Date]) < [EventEnd]))

View solution in original post

6 REPLIES 6
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1665469741322.png

 

 

Active events count: =
COUNTROWS (
    FILTER (
        Data,
        Data[EventEnd] >= MIN ( 'Calendar'[Date] )
            && Data[EventStart] <= MAX ( 'Calendar'[Date] )
    )
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


This piece of code solved my problem:

 

COUNTROWS(FILTER(ALL(Data), MAX('Calendar'[Date]) > [EventStart] && MAX('Calendar'[Date]) < [EventEnd]))

I might not have expressed myself clearly, so I'll try again (thanks for the feedback):

In your example, in April 2022 your measure results in the number 3, however, it should've returned 9, because there are nine IDs that match my criteria, i.e., that started before April 30, 2022 and ended after April 30, 2022.

Pedro503_1-1665492157724.png

Pedro503_3-1665492262031.png

 

EventStart date filter:

Pedro503_4-1665492381255.png

 

EventEnd date filter: 

Pedro503_5-1665492408334.png

 

Do you know what I should edit in order to achieve this result?

 

Thanks in advance.

 

mangaus1111
Solution Sage
Solution Sage

Hi @Pedro503 ,

 

try this:

 

VAR Open Events =
SELECTCOLUMNS (
    GENERATE (
        Fact Table,
        DATESBETWEEN (
            'Date'[Date],
            Fact Table[EventStart],
            Fact Table[EventEnd] - 1
        )
    ),
    "TransactionID", Fact Table[TransactionID],
    "Date", 'Date'[Date]
)
 
RETURN
COUNTROWS(Open Events)
 
Please hit the thumbs up & mark it as a solution if it helps you. Thanks.

Unfortunely it didn't work. Using countrows I got a huge number and when I tried using Distinctcount, this error pops up:

“The DISTINCTCOUNT function only accepts a column reference as an argument.”

 

Thanks for your help, friend.

sorry, instead of 

RETURN
COUNTROWS(Open Events)
 
you must use
DISTINCTCOUNT(Open Events[TransactionID])

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

BizApps LATAM 2023

Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

Power Platform Bootcamp

Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.