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
PhilC
Resolver I
Resolver I

Median for Days between Status Dates in same table

Hello,

 

I would like to calculate the Median # Days between Application Submitted (AS) Date to Success (S) Date from a table of Statuses.

 

Key Business Rules

  • There may be multiple occurrences of a Status for an ID, use MIN date
  • Exclude where there is no AS status
  • Exclude where there is an AS status and no S status
  • Exclude where the S date is less than the AS date

 

Desired answer is Median of 116.5, based on workings in Excel sheet attached.

 

Reading through the code below I have definitely confused myself while trying different things to get this to work.  Read at your peril.

 

 

 

Median =
VAR ApplicationDate =
    CALCULATE (
        MIN ( 'Status Dates'[STATUPDATE_FLD] ),
        'Status Dates'[STATUS_FLD] = "AS"
    )
VAR SuccessDate =
    CALCULATE (
        MIN ( 'Status Dates'[STATUPDATE_FLD] ),
        'Status Dates'[STATUS_FLD] = "S"
    )
VAR ExcludeBadDates =
    IF ( ApplicationDate > SuccessDate, 1, 0 )
VAR SuccessTable =
    CALCULATETABLE (
        'Status Dates',
        'Status Dates'[STATUPDATE_FLD] = ApplicationDate
            || 'Status Dates'[STATUPDATE_FLD] = SuccessDate
            && ExcludeBadDates = 0
    )
VAR AverageTimetoSuccess =
    MEDIANX (
        FILTER (
            VALUES ( 'Status Dates'[UID_FLD] ),
            CALCULATE ( COUNTROWS ( 'Status Dates' ), 'Status Dates'[STATUS_FLD] = "AS" ) > 0
                && CALCULATE ( COUNTROWS ( 'Status Dates' ), 'Status Dates'[STATUS_FLD] = "S" ) > 0
                && ExcludeBadDates = 0
        ),
        DATEDIFF ( ApplicationDate, SuccessDate, DAY )
    )
RETURN
    AverageTimetoSuccess

 

 

 

 

Data is below (cannot see how to attached PBIX and Excel file, will try to upload when at home)

 

STATUPDATE_FLDSTATUS_FLDUID_FLD
1/08/1990AS8
22/11/1990S8
16/12/1994S13
22/11/1994S13
24/11/1994CR13
25/11/1994CS13
1/12/1994CG13
21/01/1994DS13
4/02/1994DR13
16/12/1994IS13
27/07/1993AS2
2/06/1994S15
19/12/1994CR15
2/06/1994CS15
30/07/1993AS11
6/09/1993P11
22/09/1993AR11
14/08/1995EI20
14/08/1995ES20
3/11/1995CR20
7/11/1995CS20
10/11/1995CG20
13/11/1995IS20
26/05/1993AR15
26/05/1993AS15
12/09/1995SE20
31/12/1995CF13
15/02/1994AR13
24/11/1993S2
2/10/1995AR20
31/12/1995Z15
28/02/1994AS13
8/12/1995S20
5/11/1993S11
2/10/1995AS20
31/12/1991Z8
11/09/2000OW20
30/09/2000D2
31/12/1997Z2
26/06/1998C13
18/05/2001Z13
24/07/2001Z20
24/03/2000C11
2/08/2000FF11
6/08/2001Z11
8/08/2001FF20
30/09/2000D8
31/12/2004D15
31/12/2004D2
24/07/2009D20
13/06/2016AS40
10/06/2016S40
15/06/2016AS50
20/06/2016G50

 

 

Would appreciate any guidance.

 

Cheers

Phil

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @PhilC 

 

please check this code:

 

 

 

MyMedian =
VAR FilterRel =
    FILTER (
        'Status Date',
        OR ( 'Status Date'[STATUS_FLD] = "AS", 'Status Date'[STATUS_FLD] = "S" )
    )
VAR GroupOnUID =
    ADDCOLUMNS (
        SUMMARIZE ( FilterRel, 'Status Date'[UID_FLD] ),
        "Date", CALCULATE ( MIN ( 'Status Date'[STATUPDATE_FLD] ) ),
        "UID", 'Status Date'[UID_FLD],
        "S", CALCULATE (
            MIN ( 'Status Date'[STATUPDATE_FLD] ),
            'Status Date'[STATUS_FLD] = "S"
        ),
        "AS", CALCULATE (
            MIN ( 'Status Date'[STATUPDATE_FLD] ),
            'Status Date'[STATUS_FLD] = "AS"
        )
    )
VAR Selection =
    FILTER (
        ADDCOLUMNS ( GroupOnUID, "TimeRange", ( [S] - [AS] ) * 1 ),
        [TimeRange] >= 0
            && NOT ( ISBLANK ( [AS] ) )
    )
VAR Result =
    MEDIANX ( Selection, [TimeRange] )
RETURN
    Result

 

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
ImkeF
Super User
Super User

Hi @PhilC 

 

please check this code:

 

 

 

MyMedian =
VAR FilterRel =
    FILTER (
        'Status Date',
        OR ( 'Status Date'[STATUS_FLD] = "AS", 'Status Date'[STATUS_FLD] = "S" )
    )
VAR GroupOnUID =
    ADDCOLUMNS (
        SUMMARIZE ( FilterRel, 'Status Date'[UID_FLD] ),
        "Date", CALCULATE ( MIN ( 'Status Date'[STATUPDATE_FLD] ) ),
        "UID", 'Status Date'[UID_FLD],
        "S", CALCULATE (
            MIN ( 'Status Date'[STATUPDATE_FLD] ),
            'Status Date'[STATUS_FLD] = "S"
        ),
        "AS", CALCULATE (
            MIN ( 'Status Date'[STATUPDATE_FLD] ),
            'Status Date'[STATUS_FLD] = "AS"
        )
    )
VAR Selection =
    FILTER (
        ADDCOLUMNS ( GroupOnUID, "TimeRange", ( [S] - [AS] ) * 1 ),
        [TimeRange] >= 0
            && NOT ( ISBLANK ( [AS] ) )
    )
VAR Result =
    MEDIANX ( Selection, [TimeRange] )
RETURN
    Result

 

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi Imke, thanks so much for the quick response.  That code is producing the expected result with the test data, and also with the original dataset, awesome (especially as I did not even have time to post the file).

 

Median correct.JPG

 

Now to try to understand the approach 🙂

 

Thanks again.

Cheers, Phil

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.