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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Agreenwood
Helper I
Helper I

COUNTROWS on last record that matches a status prior to date slicer

Hi,

 

Please consider the following fact table:

 

user_iddatestatus
101 January 2020A
101 February 2020B
101 March 2020A
201 January 2020B
201 February 2020A
201 March 2020C
201 April 2020B
301 January 2020D
301 February 2020B
301 March 2020C
301 April 2020A
401 January 2020B
401 February 2020A
401 March 2020B
401 April 2020B

 

Now please consider a date slicer on a report set to "March 2020".

 

I would like to create a measure which returns the number of records where the last record for each user_id prior to the slicer date matches status "A". In the example above, with the date set to March 2020, then the measure would return 2 as both user_id 2 and 4 match the conditions.

 

In my attempts to get this to work I have used GROUPBY along with MAXX within a VAR RETURN measure to get the last date for each user_id prior to the date slicer, though I'm now unable to return the status for last record found.

 

Any help is greatly appreciated, and I hope I have explained my question clearly.

 

Edit: example PBIX file link here: Example PBIX File 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

// Assumptions:
// You have a Dates table that should be disconnected
// from your fact table (let's call it T). For each
// user_id the combinations of (date, status) must
// be unique.

// Once you've made a selection in the
// slicer (Dates[Date])...

[# Records] =
var __lastVisibleDate = MAX( Dates[Date] )
return
    SUMX(
        VALUES( T[user_id] ),
        var __lastStatus =
            MAXX(
                TOPN(1,
                    CALCULATETABLE(
                        T,
                        // You may want to use <
                        // instead of <=
                        T[date] <= __lastVisibleDate
                    ),
                    T[Date],
                    DESC
                ),
                T[status]
            )
        return
            if( __lastStatus = "A", 1 )
    )
    
// Bear in mind that this measure is
// responding to slicing, so you should
// carefully examine why the result is
// what it is.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

// Assumptions:
// You have a Dates table that should be disconnected
// from your fact table (let's call it T). For each
// user_id the combinations of (date, status) must
// be unique.

// Once you've made a selection in the
// slicer (Dates[Date])...

[# Records] =
var __lastVisibleDate = MAX( Dates[Date] )
return
    SUMX(
        VALUES( T[user_id] ),
        var __lastStatus =
            MAXX(
                TOPN(1,
                    CALCULATETABLE(
                        T,
                        // You may want to use <
                        // instead of <=
                        T[date] <= __lastVisibleDate
                    ),
                    T[Date],
                    DESC
                ),
                T[status]
            )
        return
            if( __lastStatus = "A", 1 )
    )
    
// Bear in mind that this measure is
// responding to slicing, so you should
// carefully examine why the result is
// what it is.
amitchandak
Super User
Super User

@Agreenwood , assumed you have date table joined with your date

have this measure with user_id in visual

measure =
var _max = eomonth(maxx(allselected(Date),Date[Date]),-1)
return
countx(filter(summarize(Table, table[user_id] ,"_1", calculate(lastnonblankvalue(Table[date],maxx(Table[status])), filter(all(Date),Date[Date]<_max))),[_1]="A"),[user_id])

 

You can try with date of your table too

Thanks @amitchandak , this looks as though it is really close, though I think it is excluding user_id's who do not have a record in the month selected.

 

For example, using your measure and the same data table as before with the date set to "April 2020", then the measure returns BLANK. I would like it to return 1, since user_id 1's last status prior to April is A despite him not having a record in April.

 

Is there a way of adapting the measure to accomodate this?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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