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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

I'm new to DAX. Want to know how to get the best sales and previous best sales?

 
3 ACCEPTED SOLUTIONS
TomMartens
Super User
Super User

Hey @Anonymous ,

not sure if I understand correctly as there is also a red box 😞

 

Nevertheless, this is my sample data:

image.png

I create three measures that allow to create these three card visuals:
image.png
Here are the measures:
Top 1 sale:

 

 

 

Top 1 sale = 
var top1salserows =
    SELECTCOLUMNS(
        TOPN(
            1
            , ALLSELECTED( 'Sales Table' )
            , 'Sales Table'[sales]
            , DESC
        )
        , "top" ,  'Sales Table'[sales]
    )
return
top1salserows

 

 

 

2nd best sales:

 

 

 

Top 2 sale = 
var top2salserows =
    minx(
        TOPN(
            2
            , ALLSELECTED( 'Sales Table' )
            , 'Sales Table'[sales]
            , DESC
        )
        , 'Sales Table'[sales]
    )
return
top2salserows

 

 

 

previous best sale:

 

 

 

previous bestsale = 
var top1salesdate = 
    SELECTCOLUMNS(
        TOPN( 1 , ALLSELECTED( 'Sales Table' ) , CALCULATE(SUM( 'Sales Table'[sales] ) ) , desc )
        , [date]
    )
var prevSales =
    maxx(
        filter(
            allselected( 'sales table' )
            , 'sales table'[date] < top1salesdate
        )
        , [sales]
    )
return

prevSales

 

 

 

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

Anonymous
Not applicable

Thanks! Tom,

This solved the problem to a great extent.

Now the corresponding dates are to be calculated. Not able to get the "previous best sale date".

View solution in original post

Hey @Anonymous ,

 

here are the measures to calculate the dates.

Top 1 best sales date:

Top 1 sales date = 
var top1salserows =
    SELECTCOLUMNS(
        TOPN(
            1
            , ALLSELECTED( 'Sales Table' )
            , 'Sales Table'[sales]
            , DESC
        )
        , "topdate" ,  'Sales Table'[date]
    )
return
top1salserows


Top 2 sales date:

Top 2 sales date = 
var top2salserows =
    SELECTCOLUMNS(
        TOPN(
            1
            , TOPN(
                2
                , ALLSELECTED( 'Sales Table' )
                , 'Sales Table'[sales]
                , DESC
            )
            , 'Sales Table'[sales]
            , ASC
        )
        , "date"  , [date]
    )
return
top2salserows


Previous best sales date:

previous bestsales date = 
var top1salesdate = 
    SELECTCOLUMNS(
        TOPN( 1 , ALLSELECTED( 'Sales Table' ) , CALCULATE(SUM( 'Sales Table'[sales] ) ) , desc )
        , [date]
    )
var prevSales =
    SELECTCOLUMNS(
        TOPN(
            1
            , filter(
                allselected( 'sales table' )
                , 'sales table'[date] < top1salesdate
            )
            , [sales]
            , DESC
        )
        , "date" , [date]
    )
return
prevSales

The report now looks like this:
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

7 REPLIES 7
TomMartens
Super User
Super User

Hey @Anonymous ,

not sure if I understand correctly as there is also a red box 😞

 

Nevertheless, this is my sample data:

image.png

I create three measures that allow to create these three card visuals:
image.png
Here are the measures:
Top 1 sale:

 

 

 

Top 1 sale = 
var top1salserows =
    SELECTCOLUMNS(
        TOPN(
            1
            , ALLSELECTED( 'Sales Table' )
            , 'Sales Table'[sales]
            , DESC
        )
        , "top" ,  'Sales Table'[sales]
    )
return
top1salserows

 

 

 

2nd best sales:

 

 

 

Top 2 sale = 
var top2salserows =
    minx(
        TOPN(
            2
            , ALLSELECTED( 'Sales Table' )
            , 'Sales Table'[sales]
            , DESC
        )
        , 'Sales Table'[sales]
    )
return
top2salserows

 

 

 

previous best sale:

 

 

 

previous bestsale = 
var top1salesdate = 
    SELECTCOLUMNS(
        TOPN( 1 , ALLSELECTED( 'Sales Table' ) , CALCULATE(SUM( 'Sales Table'[sales] ) ) , desc )
        , [date]
    )
var prevSales =
    maxx(
        filter(
            allselected( 'sales table' )
            , 'sales table'[date] < top1salesdate
        )
        , [sales]
    )
return

prevSales

 

 

 

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
Anonymous
Not applicable

Thanks! Tom,

This solved the problem to a great extent.

Now the corresponding dates are to be calculated. Not able to get the "previous best sale date".

Hey @Anonymous ,

 

here are the measures to calculate the dates.

Top 1 best sales date:

Top 1 sales date = 
var top1salserows =
    SELECTCOLUMNS(
        TOPN(
            1
            , ALLSELECTED( 'Sales Table' )
            , 'Sales Table'[sales]
            , DESC
        )
        , "topdate" ,  'Sales Table'[date]
    )
return
top1salserows


Top 2 sales date:

Top 2 sales date = 
var top2salserows =
    SELECTCOLUMNS(
        TOPN(
            1
            , TOPN(
                2
                , ALLSELECTED( 'Sales Table' )
                , 'Sales Table'[sales]
                , DESC
            )
            , 'Sales Table'[sales]
            , ASC
        )
        , "date"  , [date]
    )
return
top2salserows


Previous best sales date:

previous bestsales date = 
var top1salesdate = 
    SELECTCOLUMNS(
        TOPN( 1 , ALLSELECTED( 'Sales Table' ) , CALCULATE(SUM( 'Sales Table'[sales] ) ) , desc )
        , [date]
    )
var prevSales =
    SELECTCOLUMNS(
        TOPN(
            1
            , filter(
                allselected( 'sales table' )
                , 'sales table'[date] < top1salesdate
            )
            , [sales]
            , DESC
        )
        , "date" , [date]
    )
return
prevSales

The report now looks like this:
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
Anonymous
Not applicable

Thanks a lot Tom.

Hey @Anonymous ,

 

if one or both of my posts answers your question, mark one or both posts as an answer. This will help other users to find a solution more quickly.

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
Anonymous
Not applicable

Ok.

I will try to explain with an example.

hsk_8-1671538093480.png

What I'm interested is the four results in blue boxes.

TomMartens
Super User
Super User

Hey @Anonymous ,

 

mastering DAX starts with a clear description of what you want to achieve, what has to be calculated.

This:  "... best sales and previous best sales ..."

Does not reveal anything in regards to business requirements/business rules, as long you provide more context, what defines "best" and "previous best."

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

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