Forum Discussion

Dicken's avatar
Dicken
Icon for Post Prodigy rankPost Prodigy
1 year ago
Solved

DAX Measure to return top n dates

Hi I set up a simple tabel of date , item and units and a pivot of year month and wanted to  returnn the top 3 dates for each month but I keep getting all of hte dates in each month;  as a test is ...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Thanks for the reply from darkniqht , please allow me to provide another insight:

    Hi, Dicken 

    Regarding the issue you raised, my solution is as follows:

    1.First I have created the following table and the column names and data are the data you have given:

    2. Below are the measure I've created for your needs:

    TopDay = 
    VAR CurrentMonth =
        MAX ( 'Calendar'[Month] )
    VAR top3 =
        TOPN (
            3,
            FILTER ( ALLSELECTED ( 'Table' ), MONTH ( 'Table'[Date] ) = CurrentMonth ),
            'Table'[Units], DESC
        )
    VAR result1 =
        CONCATENATEX ( top3, 'Table'[Date], ", ", 'Table'[Units], DESC )
    RETURN
        IF (
            ISFILTERED ( 'Calendar'[Month] ),
            IF (
                MAX ( 'Table'[Date] ) IN SELECTCOLUMNS ( top3, "2", 'Table'[Date] ),
                result1,
                BLANK ()
            ),
            MAX ( 'Table'[Date] )
        )
    
    

    3.Here's my final result, which I hope meets your requirements.

     

     

    Please find the attached pbix relevant to the case.

     

    Best Regards,

    Leroy Lu

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