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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Dicken
Responsive Resident
Responsive Resident

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 tried; this ;

 

TopItem:=VAR top3 = TOPN( 3, VALUES( Table1[Item]), [TotlalUnits], DESC )

VAR result = CONCATENATEX( top3, Table1[Item], ", ", [TotlalUnits] ,DESC )

RETURN IF( ISFILTERED('Calendar'[Month]),result,BLANK())

 

This does work but if I replace the item with the Calandar table date 

TopDay:=VAR top3 = TOPN( 3, VALUES( 'Calendar'[Date]), [TotlalUnits], DESC )

VAR result = CONCATENATEX( top3, 'Calendar'[Date], ", ", [TotlalUnits] ,DESC )

RETURN IF( ISFILTERED('Calendar'[Month]),result,BLANK())
Then for each monht I am just getting all the dates returned ? 
Richard. 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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:

vlinyulumsft_0-1730707962118.png

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.

vlinyulumsft_1-1730708012718.png

 

 

vlinyulumsft_2-1730708012719.png

vlinyulumsft_3-1730708020206.png

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.

 

View solution in original post

3 REPLIES 3
darkniqht
Advocate I
Advocate I

Hi Richard! It looks like the issue is that the TOPN function is applied to the Calendar[Date] column, but it doesn’t consider the context of your month filter correctly.

 

Instead of using VALUES('Calendar'[Date]), try creating a measure that filters the dates based on the current month context. Here’s a modified version of your DAX:

 

TopDay =
VAR CurrentMonth = MAX('Calendar'[Month])
VAR top3 = 
    TOPN(
        3,
        FILTER(Table1, MONTH(Table1[Date]) = CurrentMonth),
        [TotalUnits], 
        DESC
    )
VAR result = CONCATENATEX(top3, Table1[Date], ", ", [TotalUnits], DESC)
RETURN IF(ISFILTERED('Calendar'[Month]), result, BLANK())

 

This way, it filters the table to only include the relevant dates for the current month before selecting the top 3. Give it a try and see if that works!

Dicken
Responsive Resident
Responsive Resident

Thanks I also tried this for 'day'  distinct day by adding an index ; 

Distinct top day:=

VAR tops = TOPN( 3, SUMMARIZE(Table1,Table1[Index],'Calendar'[Day Of Week]) ,[TotalUnits],DESC)

VAR sumt = SUMMARIZE( tops, 'Calendar'[Day Of Week])

RETURN CONCATENATEX(sumt, 'Calendar'[Day Of Week],", ")
Richard 

Anonymous
Not applicable

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:

vlinyulumsft_0-1730707962118.png

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.

vlinyulumsft_1-1730708012718.png

 

 

vlinyulumsft_2-1730708012719.png

vlinyulumsft_3-1730708020206.png

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.

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors