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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
DR2022
Helper I
Helper I

TOPN Values and MAX Date

Hi! How to return the highest values and itens, for example the top 5, in the current date (last dste), using the TOPN and MAX functions?

2 ACCEPTED SOLUTIONS
TomMartens
Super User
Super User

Hey @DR2022 ,

 

this measure returns the last date (within the given filter context) inside a fact table exists:

 

 

LastDate = 
MAXX(
    SUMMARIZE(
        'FactOnlineSales'
        , 'DimDate'[Datekey]
    )
    , 'DimDate'[Datekey]
)

 

 

 

This date then is used to filter a virtual table using FILTER(SUMMARIZE(...), ...), this table is nested inside the TOPN function.
Finally, the new INDEX function is used to find the  4th value, the same approach can be used to find the product key, only the last line in the SELECTCOLUMNS has to be changed to reference the productkey column:

 

 

 

GetTheNthValueFromTheTopNList = 
var NthValue = 4
var topnItems = 5
var theLastDate = [LastDate]
var theTopNTable = 
    TOPN(
        topnItems
        , CALCULATETABLE(
            ADDCOLUMNS(
                SUMMARIZE(
                    'FactOnlineSales'
                    , 'DimProduct'[ProductKey]
                    , 'DimDate'[Datekey]
                )
                , "v" , CALCULATE( SUM( 'FactOnlineSales'[SalesQuantity] ) )
            )
            , 'DimDate'[Datekey] =  theLastDate
        )
        , CALCULATE( SUM( 'FactOnlineSales'[SalesQuantity] ) )
        , DESC
    )
var theNValue =
SELECTCOLUMNS(
    iNDEX(
        NthValue
        , theTopNTable
        , ORDERBY( [v] , DESC)
        , KEEP
    )
    , [v]
)
return
theNthValue

 

 

 

The next picture shows separate card visuals, the left shows the last date, the one in the middle visualizes the TOPN table using the DAX function TOJSON(...), and the rightmost visual shows the nth value.
image.png

Hopefully, this provides what you are looking for,

Regards,

Tom





Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

AlB
Community Champion
Community Champion

Hi @DR2022 

You can place the sales values in a table visual, select the current date in a slicer and  use a filter visual to only  show the top 5

Check this out https://community.powerbi.com/t5/Desktop/TopN/m-p/131646

 

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

View solution in original post

2 REPLIES 2
AlB
Community Champion
Community Champion

Hi @DR2022 

You can place the sales values in a table visual, select the current date in a slicer and  use a filter visual to only  show the top 5

Check this out https://community.powerbi.com/t5/Desktop/TopN/m-p/131646

 

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

TomMartens
Super User
Super User

Hey @DR2022 ,

 

this measure returns the last date (within the given filter context) inside a fact table exists:

 

 

LastDate = 
MAXX(
    SUMMARIZE(
        'FactOnlineSales'
        , 'DimDate'[Datekey]
    )
    , 'DimDate'[Datekey]
)

 

 

 

This date then is used to filter a virtual table using FILTER(SUMMARIZE(...), ...), this table is nested inside the TOPN function.
Finally, the new INDEX function is used to find the  4th value, the same approach can be used to find the product key, only the last line in the SELECTCOLUMNS has to be changed to reference the productkey column:

 

 

 

GetTheNthValueFromTheTopNList = 
var NthValue = 4
var topnItems = 5
var theLastDate = [LastDate]
var theTopNTable = 
    TOPN(
        topnItems
        , CALCULATETABLE(
            ADDCOLUMNS(
                SUMMARIZE(
                    'FactOnlineSales'
                    , 'DimProduct'[ProductKey]
                    , 'DimDate'[Datekey]
                )
                , "v" , CALCULATE( SUM( 'FactOnlineSales'[SalesQuantity] ) )
            )
            , 'DimDate'[Datekey] =  theLastDate
        )
        , CALCULATE( SUM( 'FactOnlineSales'[SalesQuantity] ) )
        , DESC
    )
var theNValue =
SELECTCOLUMNS(
    iNDEX(
        NthValue
        , theTopNTable
        , ORDERBY( [v] , DESC)
        , KEEP
    )
    , [v]
)
return
theNthValue

 

 

 

The next picture shows separate card visuals, the left shows the last date, the one in the middle visualizes the TOPN table using the DAX function TOJSON(...), and the rightmost visual shows the nth value.
image.png

Hopefully, this provides what you are looking for,

Regards,

Tom





Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.