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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
RJMonteza
Regular Visitor

Need help with DAX to filter and count

I have a data of 3 years with 12 Periods each. I need to count the redlights categorized by YES/NO per year and period:

I have my initial DAX: 

1. CALCULATE(COUNTA(REDLIGHT[Start Time]),REDLIGHT[VALID OE ERROR (Y/N?)] IN {"YES"})
 
2. SUMX(
    VALUES(REDLIGHT[Start Time]),
    CALCULATE(
        COUNTROWS('REDLIGHT'),
        REDLIGHT[VALID OE ERROR (Y/N?)] IN {"YES"}
 
However, both DAX count the valid redlights ("YES") to all years though I stated to count per date.

Thanks!
 
8 REPLIES 8
Greg_Deckler
Super User
Super User

@RJMonteza Maybe:

Measure = COUNTROWS( FILTER( 'REDLIGHT', [VALID OE ERROR (Y/N?)] = "YES" ) ) 


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...

Hello Greg,

 

I also need to filter the date and year. I need to count how many YES in a perod in this year.

 

Thanks!

@RJMonteza I assumed those would just be part of the visual.



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...

Nope, I need to count all the valid redlights (YES) per month of the year. Right now, my DAX is calculating all the valid redlights for those 3 years. Example: Sum of 2021, 2022, and 2023 of PD1 = RL count. Which is not correct, I need it to look like this up to perod 12:

202120222023
PD 1 = 2PD 1 = 0PD 1 = 1
PD 2 = 1PD 2 = 2PD 2 = 0
PD 3 = 0PD 3 = 1PD 3 = 1

@RJMonteza Not sure I am completely following but you can do this:

Measure 2021 = COUNTROWS( FILTER( 'REDLIGHT', [VALID OE ERROR (Y/N?)] = "YES" && [YEAR] = "2021" ) ) 


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...

Hmm... Can we have it apply to all years instead of filtering just 1 year, then per period, then countrows. I am seeing that there is a need to use VAR, but I'm not sure how..

@RJMonteza OK, so like this?

Measure 2021 = 
  VAR __Table = 
    SUMMARIZE(
      FILTER( 'REDLIGHT', [VALID OE ERROR (Y/N?)] = "YES" ),
      [YEAR},
      [PERIOD],
      "__Count", COUNTROWS( 'REDLIGHT' )
    )
  VAR __Result = SUMX( __Table, [__Count] )
RETURN
  __Result

Note that sample data and expected result would make this FAAARRRR more easy to figure out what you are going for here.



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...

I'm having this as a result:

 

RJMonteza_0-1686064445254.png

The result I need is a a single digit one up to PD 8 only, since we only have a data until PD 8. I need this to be:

 

PD 1 = 1

PD 2 = 1

PD 3 = 0

PD 4 = 1
PD 5 = 1

etc.

 

Note: The redlight table is another table from this one.

Helpful resources

Announcements
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!

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.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.