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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
JGroothedde
Resolver II
Resolver II

Find count of rows between start-end date in same table

Hi there,

 

I'm having a brainfart for something that i initially thought would be very easy. I hope someone can help me with this.

I have a single table like so:

ID persondatetime startdatetime planned endstatus
101-10-24 09:0001-10-24 14:00completed
208-11-24 13:0008-11-24 23:00completed
310-10-24 09:0010-10-24 17:00cancelled
102-10-24 10:0002-10-24 14:00completed
310-10-24 10:3010-10-24 14:00completed

 

What i'm trying to do is find the count of rows for each person and row that either began or ended in the timeframe between the start and end datetime. For example, what i'm looking for is this:

ID persondatetime startdatetime planned endstatus# between start-end
101-10-24 09:0001-10-24 14:00completed0
208-11-24 13:0008-11-24 23:00completed0
310-10-24 09:0010-10-24 17:00cancelled1
102-10-24 10:0002-10-24 14:00completed0
310-10-24 10:3010-10-24 14:00completed0


How do i go about this? Many thanks in advance!

1 ACCEPTED SOLUTION
v-denglli-msft
Community Support
Community Support

Hi @JGroothedde ,

Please refers to the following steps.

1. Create two dimension tables for the /datetime start/ field and the /datetime planned end/ field

DimStartime = VALUES('Table'[datetime start])

DimEndtime = VALUES('Table'[datetime planned end])

vdengllimsft_0-1731551868471.png

vdengllimsft_1-1731551887100.png

 

2. Creates a measure that calculates the count of rows.

Test = 
VAR _result = 
IF(SELECTEDVALUE('DimEndtime'[datetime planned end])>SELECTEDVALUE('DimStartime'[datetime start]),
CALCULATE(
     COUNTROWS('Table'),
     KEEPFILTERS('Table'[datetime planned end]<=SELECTEDVALUE('DimEndtime'[datetime planned end])&&'Table'[datetime start]>=SELECTEDVALUE(DimStartime[datetime start]))
     ,'Table'[status]="Completed"
     )
    )
    RETURN
IF(_result<>BLANK(),1,0)


3. Create slicers using fields from both dimension tables.
The result is shown below and hopefully meets your needs.

vdengllimsft_2-1731552198927.png


Please see the attached pbix for reference.

Best Regards,
Dengliang 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
v-denglli-msft
Community Support
Community Support

Hi @JGroothedde ,

Please refers to the following steps.

1. Create two dimension tables for the /datetime start/ field and the /datetime planned end/ field

DimStartime = VALUES('Table'[datetime start])

DimEndtime = VALUES('Table'[datetime planned end])

vdengllimsft_0-1731551868471.png

vdengllimsft_1-1731551887100.png

 

2. Creates a measure that calculates the count of rows.

Test = 
VAR _result = 
IF(SELECTEDVALUE('DimEndtime'[datetime planned end])>SELECTEDVALUE('DimStartime'[datetime start]),
CALCULATE(
     COUNTROWS('Table'),
     KEEPFILTERS('Table'[datetime planned end]<=SELECTEDVALUE('DimEndtime'[datetime planned end])&&'Table'[datetime start]>=SELECTEDVALUE(DimStartime[datetime start]))
     ,'Table'[status]="Completed"
     )
    )
    RETURN
IF(_result<>BLANK(),1,0)


3. Create slicers using fields from both dimension tables.
The result is shown below and hopefully meets your needs.

vdengllimsft_2-1731552198927.png


Please see the attached pbix for reference.

Best Regards,
Dengliang Li

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

Thank you @v-denglli-msft i have no clue why i didn't think of creating dimtables in the first place. Lifesaver, cheers!

JGroothedde
Resolver II
Resolver II

I'm trying something like this now. It works, but is extremely slow. Anyone have any tips to speed this up? 🙂

 

Test = 

VAR _date = FORMAT(SELECTEDVALUE(Orders[Datetime start]),"dd-MM-yyyy")
VAR _starttime = FORMAT(SELECTEDVALUE(Orders[Datetime start]),"HH:mm")
VAR _endtime = FORMAT(SELECTEDVALUE(Orders[Datetime planned end]),"HH:mm")
VAR _idperson = SELECTEDVALUE(Orders[ID person])
VAR _idorder = SELECTEDVALUE(Orders[ID Order])

RETURN

CALCULATE(
    [Count of orders],
    ,FILTER(
        ALL(Orders),
        Orders[ID Person] = _idperson &&
        Orders[ID Order] <> _idorder &&
        Orders[Date planned] = _date &&
        Orders[Status] = "Completed" &&
        OR(
            AND(
             FORMAT(Orders[Datetime start],"HH:mm") >= _starttime
            ,FORMAT(Orders[Datetime start],"HH:mm") <= _endtime
            )
            ,
            AND(
             FORMAT(Orders[Datetime planned end],"HH:mm") >= _starttime
            ,FORMAT(Orders[Datetime planned end],"HH:mm") <= _endtime
            )
        )
    )
)

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.