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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
cdcarnes
Frequent Visitor

Count items on the most recent document by ID withing selected date range

I am having a hard time figuring this one out.   I have a table similar to the one shown below

 

EffectiveDateIDNoProblems
4/1/20251Y
3/31/20255NULL
3/28/20254Y
3/26/20256Y
3/21/202510Y
3/21/202511Y
3/18/20251NULL
3/18/20255Y
3/18/202512Y
3/18/202511Y
3/18/20258Y
3/18/20256NULL
3/18/20257Y
3/18/20255Y
3/18/20251NULL
3/7/202515Y
3/7/202520Y
3/7/202521Y
3/7/202524Y
3/7/20253Y

 

I need to be able to provide a count of the "Y" in the table based on the most recent document that is within the date range selected on the slicer.  

So, if someon selected a date range of 3-7 to 3-20 it would return a value of 8 (ID 1 has a Y value but most recent document is NULL so would not count them and ID 5 is listed twice with most recent document a "Y" value so only count them one time).

 

I can't seem to figure out how to get this completed.  My first thought was to create a virtual table based on the date ranges. But I can't keep it from duplicating data and only selecting the most recent document in that date range. 

 

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

@cdcarnes Try:

Measure = 
  VAR __Table = 
    ADDCOLUMNS(
        SUMMARIZE( 'Table', [ID], "MaxDate", MAX('Table'[EffectiveDate] ) ),
        "__NoProblems", MAXX(FILTER( 'Table', [ID] = EARLIER('Table'[ID]) && [EffectiveDate] = [MaxDate]), [NoProblems] )
    )
  VAR __Table2 = FILTER( __Table, [__NoProblems] = "Y" )
  VAR __Result = COUNTROWS( __Table2 )
RETURN
  __Result

Pretty sure the answer is 10 not 8. PBIX attached below signature.



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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Community Champion
Community Champion

@cdcarnes Try:

Measure = 
  VAR __Table = 
    ADDCOLUMNS(
        SUMMARIZE( 'Table', [ID], "MaxDate", MAX('Table'[EffectiveDate] ) ),
        "__NoProblems", MAXX(FILTER( 'Table', [ID] = EARLIER('Table'[ID]) && [EffectiveDate] = [MaxDate]), [NoProblems] )
    )
  VAR __Table2 = FILTER( __Table, [__NoProblems] = "Y" )
  VAR __Result = COUNTROWS( __Table2 )
RETURN
  __Result

Pretty sure the answer is 10 not 8. PBIX attached below signature.



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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

That worked.  Thank you so much for your help.

 

 

pankajnamekar25
Super User
Super User

Hello @cdcarnes 

 

try below DAX

 

CountOfYOnMostRecent =

VAR DateRangeTable =

    FILTER(

        YourTable,

        YourTable[EffectiveDate] >= MIN('DateTable'[Date]) &&

        YourTable[EffectiveDate] <= MAX('DateTable'[Date])

    )

 

VAR MostRecentPerID =

    ADDCOLUMNS(

        SUMMARIZE(DateRangeTable, YourTable[ID]),

        "MostRecentDate",

            CALCULATE(

                MAX(YourTable[EffectiveDate]),

                DateRangeTable,

                ALLEXCEPT(YourTable, YourTable[ID])

            )

    )

 

VAR MostRecentRecords =

    FILTER(

        ADDCOLUMNS(

            MostRecentPerID,

            "NoProblemsValue",

                CALCULATE(

                    MAX(YourTable[NoProblems]),

                    FILTER(

                        DateRangeTable,

                        YourTable[ID] = EARLIER(YourTable[ID]) &&

                        YourTable[EffectiveDate] = EARLIER([MostRecentDate])

                    )

                )

        ),

        [NoProblemsValue] = "Y"

    )

 

RETURN

    COUNTROWS(MostRecentRecords)

 

 

Thanks,
 Pankaj Namekar | LinkedIn

If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.

Doesn't seem to be working.  Is returning 0 on my table (not the sample above) when it should be 3.

 

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.