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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Countrows with multiple conditions

Hi,

I'd like to count rows with multiple conditions. The first measure seems to work well. But the second one doesn't work. The only thing I changed is the time difference, from 10s to 2hr. Event V2 count should return as 1. 

Events = VAR y = ADDCOLUMNS(
    TABLE,"Batch",
    1-CALCULATE(
        COUNTROWS(TABLE),
        FILTER(
            TABLE,
            TABLE[SITE_NO]=EARLIER(TABLE[SITE_NO]) &&
            TABLE[START_DT]>EARLIER(TABLE[START_DT]) &&           
            TABLE[EVENT_CODE]=EARLIER(TABLE[EVENT_CODE]) &&
            DATEDIFF(EARLIER(TABLE[END_DT]),TABLE[START_DT],SECOND)<10
            )))
RETURN SUMX(
    y,[Batch])
Events V2 = VAR y = ADDCOLUMNS(
    TABLE,"Batch",
    1-CALCULATE(
        COUNTROWS(TABLE),
        FILTER(
            TABLE,
            TABLE[SITE_NO]=EARLIER(TABLE[SITE_NO]) &&
            TABLE[START_DT]>EARLIER(TABLE[START_DT]) &&           
            TABLE[EVENT_CODE]=EARLIER(TABLE[EVENT_CODE]) &&
            DATEDIFF(EARLIER(TABLE[END_DT]),TABLE[START_DT],SECOND)<7201
            )))
RETURN SUMX(
    y,[Batch])

pbi community.JPG

 

Please find another example below. Event V2 should return as 6.

 

Capture.JPG

11 REPLIES 11
Anonymous
Not applicable

Hi @Anonymous ,

 

I think you need to show the expected result of your sample data.

 

I use your sample data to create a calculated column, hope that's what you want, please try.

 

Column = SUMX(Sheet1,IF([SITE_NO]=EARLIER([SITE_NO]) &&
                        [START_DT]>EARLIER([START_DT]) &&
                        [EVENT_CODE]=EARLIER([EVENT_CODE]) && 
                        DATEDIFF(EARLIER(Sheet1[END_DT]),Sheet1[START_DT],SECOND) <= 10,
                        1,
                        0))

 

Countrows with multiple conditions.PNG

 

Aiolos Zhao

Anonymous
Not applicable

Hi @Anonymous 

 

Please read message 5 for expected output. The sample data size is so tiny compared to the one I am working on. 

I am afraid I won't be able to add the calculated column for years worth of data (approx. 10M rows). That's why I wanted DAX measure to combine repeated events with above conditions. 

Anonymous
Not applicable

Hi @Anonymous ,

 

What's your the expected result of your sample data, in your sample data, the Site No is A / B / C, NO 4780 and 3841

 

And I use the measure for SITE NO = B and EVENT_CODE = OB, the result is 24 and 27.

 

Countrows with multiple conditions 2.PNG

 

Aiolos Zhao

Anonymous
Not applicable

Hi @Anonymous 

By site 4780 and 3841, I was referring to screenshots in my message 1. Anyway, as for sample data, the expected output (site B, event OB) should be 27. I think you created the right measure.

 

excel.JPG

 

Anonymous
Not applicable

Hi @Anonymous ,

 

The measures are:

 

Measure = VAR y = ADDCOLUMNS(
    Sheet1,"Batch",
    CALCULATE(
        COUNTROWS(Sheet1),
        FILTER(
            Sheet1,
            [SITE_NO]=EARLIER([SITE_NO]) &&
            [START_DT]>EARLIER([START_DT]) &&
            [EVENT_CODE]=EARLIER([EVENT_CODE]) &&
            DATEDIFF(EARLIER([END_DT]),[START_DT],SECOND)<=7200)
            ))
RETURN SUMX(
    y,[Batch])

 

Measure 2 = VAR y = ADDCOLUMNS(
    Sheet1,"Batch",
    CALCULATE(
        COUNTROWS(Sheet1),
        FILTER(
            Sheet1,
            [SITE_NO]=EARLIER([SITE_NO]) &&
            [START_DT]>EARLIER([START_DT]) &&
            [EVENT_CODE]=EARLIER([EVENT_CODE]) &&
            DATEDIFF(EARLIER([END_DT]),[START_DT],SECOND)<=10)
            ))
RETURN SUMX(
    y,[Batch])

 

Please try.

Aiolos Zhao

Anonymous
Not applicable

Hi @Anonymous 

 

Thanks for quick reply. Your measures are similar to mine in first post. 

However, in large dataset, Measure 2 works but somehow Measure doesn't work. 

Anonymous
Not applicable

Hi @Anonymous ,

 

I think if the columns are not changed in your large data, the measure will also work.

 

You may need to show the data model, columns in the tables, and selections in the dashboard, and the case which doesn't work.

 

That may help others to figure out the problem.

 

Aiolos Zhao

amitchandak
Super User
Super User

@Anonymous , Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data. Along with Formula logic

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@Anonymous , Checked out the pbix. But not able to get the wrong data as it not the same(sample data) as the one you shared on the post.

 

Please share the expected output

 

Also try

countx(values(Sheet[SITE_NO]) ,calculate(COUNTROWS(Sheet1),
FILTER(
Sheet1,
[SITE_NO]=max([SITE_NO]) &&
[START_DT]>max([START_DT]) &&
[EVENT_CODE]=max([EVENT_CODE]) &&
DATEDIFF(min(EARLIER([END_DT])),max([START_DT]),SECOND)<10) , allselected(Sheet1)
))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Hi @amitchandak 

Thanks for swift replies. I think I should clarify more. 

  • Events dax measure merges and counts the events that has same site number, same event code and the difference of less than or equal to 10s between current and previous event. 
  • Event V2 dax measure does the same whereas the difference is 2hr. 

Basically, I am trying to combine the repeated events, however they are recorded as separate rows. 

 

My expected output for Event V2 would be:

  • for Site No 4087 : 1 (refer to 1st pic, since its event start time and previous end time are <2hr)
  • for Site No 3841: 6 (refer to 2nd pic and the colour lines)

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors