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
youconnect
Helper I
Helper I

Countrows last date

I have this table

idstatedate
1active01/01/2020
1reserved05/01/2020
1suspended08/01/2020
1sold15/01/2020
2active02/01/2020
2suspended03/01/2020
2active05/01/2020
3active05/01/2020
4active06/01/2020
4sold08/01/2020

 

I want count rows of distinct IDs where last date equal to "active"

Result expected is: 2 (last date from these 2 IDs have the state "active".
Can you help me?

 

Thanks

1 ACCEPTED SOLUTION

Hi @youconnect ,

 

I am understanding your logic and have made the following calculation:

count_active_id = 
var last_active_date = CALCULATE(MAX(Sheet2[date]),FILTER(ALLEXCEPT(Sheet2,Sheet2[id],Sheet2[date]),Sheet2[state]="active"))
var last_date = CALCULATE(MAX(Sheet2[date]),ALLEXCEPT(Sheet2,Sheet2[id]))
return CALCULATE(COUNTROWS(FILTER(Sheet2,last_active_date=last_date)))

test_count-active-id.PNG

 

Best Regards,
Liang
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

6 REPLIES 6
edhans
Community Champion
Community Champion

This will do what you describe, but it is 1, not 2.

Latest Record Count = 
VAR LatestDate = 
CALCULATE(
    LASTDATE('Sample Data'[Date]),
    'Sample Data'[state] = "active"
)
VAR ActiveIDCount = 
CALCULATE(
    DISTINCTCOUNT('Sample Data'[id]),
    FILTER(
        'Sample Data',
        'Sample Data'[Date] = LatestDate
    )
)
RETURN
COALESCE(ActiveIDCount,0)

the last date of an active item is Jan 6, 2020, and only one ID has that date.

How are you getting 2?



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

@edhans 

No.
last date for id 1 is 15/01/2020 and state is not active, not count

last date for id 2 is 05/01/2020 and state is active, count

last date for id 3 is 05/01/2020 and state is active, count

last date for id 4 is 08/01/2020 and state is not active, not count.

So count is 2

Hi @youconnect ,

 

I am understanding your logic and have made the following calculation:

count_active_id = 
var last_active_date = CALCULATE(MAX(Sheet2[date]),FILTER(ALLEXCEPT(Sheet2,Sheet2[id],Sheet2[date]),Sheet2[state]="active"))
var last_date = CALCULATE(MAX(Sheet2[date]),ALLEXCEPT(Sheet2,Sheet2[id]))
return CALCULATE(COUNTROWS(FILTER(Sheet2,last_active_date=last_date)))

test_count-active-id.PNG

 

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

Thanks @V-lianl-msft it worked like a charm.

Another question: If I want only the records before 01-01-2020 how do that?

I suspect there is an cleaner way to do this, but this does the trick.

 

Counting Measure = 
VAR FirstTable =
    ADDCOLUMNS (
        SUMMARIZECOLUMNS ( 'Sample Data'[id] ),
        "Date2", LASTDATE ( 'Sample Data'[Date] )
    )
VAR CombinedTable =
    NATURALINNERJOIN (
        'Sample Data',
        firsttable
    )
VAR RowCount =
    COUNTROWS (
        FILTER (
            FILTER (
                CombinedTable,
                [Date] = [Date2]
            ),
            [state] = "active"
        )
    )
RETURN
COALESCE( RowCount, 0)

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thanks @edhans but it's not working. Higher results expected.

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.