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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Jacob_Li
Frequent Visitor

Count total rows within given time range in a given date range

I am working on a measure to get the total headcounts of customers within a given time range. But I need to consider two time ranges:

  1. The active period of the customer (StartDate & EndDate)
  2. The time period we are interested in

Jacob_Li_0-1651687552119.png

As can be seen on the screenshot above, I want to calculate the total headcounts within the date range 2022-05-03 to 2022-05-04.

Since it only includes two days, the calculation is pretty straightfoward: on day 1 we have Customer 5 and Customer 6 meeting the criteria, on day 2 we still have these two customers active. Thus the results should be: 2+2=4

 

Based on the idea of v-yingjl

https://community.powerbi.com/t5/Power-Query/Count-rows-if-between-two-time-slots/td-p/1424481 and other posts,

 

I wrote the measure as follows:

Headcounts=

var calendardate=max('Calendar'[Date])
var num_ava=calculate(countrows(filter(all('fact'),calendardate>='fact'[StartDate] && calendardate<='fact'[EndDate])))
 
I am wondering how could I get the total headcounts based on the measure. Any thoughts and ideas are appreciated.
1 ACCEPTED SOLUTION

@Jacob_Li 

My code wat not correct. Sorry I'm typing on the phone

try this one

Headcounts =
SUMX (
    CROSSJOIN ( VALUES ( 'Calendar'[Date] ), VALUES ( 'fact'[StartDate] ) ),
    CALCULATE (
        VAR SelectedDates =
            VALUES ( 'Calendar'[Date] )
        RETURN
            SUMX (
                'fact',
                VAR ActiveDates =
                    CALENDAR ( 'fact'[StartDate], 'fact'[EndDate] )
                RETURN
                    IF ( COUNTROWS ( INTERSECT ( SelectedDates, ActiveDates ) ) > 0, 1, 0 )
            )
    )
)

View solution in original post

10 REPLIES 10
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

I tried to create a sample pbix file like below in order to simplify the sample.

I suggest having a calendar table as a dimension table.

I hope the below measure can help to have an idea to create a similar measure and apply it to your data model.

 

Slide1.jpg

 

Customers count measure: =
COUNTROWS (
    FILTER (
        Data,
        Data[StartDate] <= MAX ( 'Calendar'[Date] )
            && Data[EndDate] >= MIN ( '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.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thanks for your reply. I took a screenshot from your pbix file.

Jacob_Li_0-1651759130874.png

As you could see, two dates are selected: 2022/12/30 and 2022/12/31, is there any way that I could get the value of 2, which is the sum of the column of the calculated measure (1+1)?

tamerj1
Super User
Super User

Hi @Jacob_Li 

assuming no relationships invloved, you may try

Headcounts =
VAR SelectedDates =
    VALUES ( 'Calendar'[Date] )
RETURN
    SUMX (
        'fact',
        VAR ActiveDates =
            CALENDAR ( 'fact'[StartDate], 'fact'[EndDate] )
        RETURN
            IF ( COUNTROWS ( INTERSECT ( SelectedDates, ActiveDates ) ) > 0, 1, 0 )
    )

 

Thank you for your reply. Is there any way that I could get the sum of the "column" of headcounts measure from the table visual below. I am pretty sure I am very close to it.

Jacob_Li_1-1651759826461.png

 

@Jacob_Li 

Try

Headcounts =
SUMX (
    CROSSJOIN ( VALUES ( 'Calendar'[Date] ), VALUES ( 'fact'[StartDate] ) ),
    CALCULATE (
        VAR SelectedDates =
            VALUES ( 'Calendar'[Date] )
        VAR ActiveDates =
            CALENDAR ( 'fact'[StartDate], 'fact'[EndDate] )
        RETURN
            IF ( COUNTROWS ( INTERSECT ( SelectedDates, ActiveDates ) ) > 0, 1, 0 )
    )
)

@tamerj1 

 

Jacob_Li_0-1651762744066.png

By calendar('fact'[StartDate],'fact'[EndDate]), do you mean by CALENDAR (calculate(max('data'[startdate]),allexcept('data','Data'[Customer])) , calculate(max('data'[EndDate]),allexcept('data','Data'[Customer])) )? Because otherwise this measure is not working properly.

 

By replacing the underlined part, the measure is written as follows

Headcounts =
SUMX (
    CROSSJOIN ( VALUES ( 'Calendar'[Date] ), VALUES ( 'fact'[StartDate] ) ),
    CALCULATE (
        VAR SelectedDates =
            VALUES ( 'Calendar'[Date] )
        VAR ActiveDates =
            CALENDAR (calculate(max('data'[startdate]),allexcept('data','Data'[Customer])) , calculate(max('data'[EndDate]),allexcept('data','Data'[Customer])) )
        RETURN
            IF ( COUNTROWS ( INTERSECT ( SelectedDates, ActiveDates ) ) > 0, 1, 0 )
    )
)

However, the result is not as expected. 

Jacob_Li_1-1651763058431.png

It gives the total 12 instead of 2.

@Jacob_Li 

My code wat not correct. Sorry I'm typing on the phone

try this one

Headcounts =
SUMX (
    CROSSJOIN ( VALUES ( 'Calendar'[Date] ), VALUES ( 'fact'[StartDate] ) ),
    CALCULATE (
        VAR SelectedDates =
            VALUES ( 'Calendar'[Date] )
        RETURN
            SUMX (
                'fact',
                VAR ActiveDates =
                    CALENDAR ( 'fact'[StartDate], 'fact'[EndDate] )
                RETURN
                    IF ( COUNTROWS ( INTERSECT ( SelectedDates, ActiveDates ) ) > 0, 1, 0 )
            )
    )
)

It works! Thank you! Also lots of thanks for all the replies under this post.

Arul
Super User
Super User

@Jacob_Li ,

 

Check if it does helps or not?

 

Count customer = 
Var _Mindate = DATEVALUE( "3/5/2022")
Var _Maxdate = DATEVALUE( "4/5/2022")

Var _Count = CALCULATE(COUNTROWS('Customer Count'),'Customer Count'[Start Date]>=_Mindate,'Customer Count'[End Date]<=_Maxdate)
return _Count

 

Thanks,

Arul





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


LinkedIn


Jacob_Li
Frequent Visitor

Thank you, but I am afraid the restriction might be correct. Because I want to evaluate each individual date between 05/03/2022 and 05/04/2022, I need to compare these two dates with start date and end date for twice, instead of comparing 05/03/2022 with StartDate and 05/04/2022 with EndDate. 

 

Thanks anyway.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

Top Solution Authors