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
jamuka
Advocate II
Advocate II

Find Top - Bottom Performers

Hello all,

 

I'd like to show top - bottom brands, based on

 

  • margin % of last 4 weeks sales


Margin % Last 28 Days Sales =
CALCULATE ( [Total Margin %],
    DATESINPERIOD ( 'Sales'[Sales Date],
        LASTDATE ( 'Sales'[Sales Date] ), -28, DAY )) 

 

  • and brand's share in related category


Margin Share Watch=

VAR MarginAmount28Days =
    CALCULATE ( [Total Margin Amount],
        DATESINPERIOD ( 'Sales'[Sales Date],
            LASTDATE ( 'Sales'[Sales Date] ), -28, DAY ))

 

VAR MarginAmountWatch =
    CALCULATE ( [Margin Amount Last 28 Days Sales],
        KEEPFILTERS ( 'Sales'[Category] = "Watch" ),
        ALL ( 'Product'[Brand Name]))

 

VAR Result = MarginAmount28Days / MarginAmountWatch
RETURN
    Result


But instead of showing figures for last 4 weeks, it shows last 4 week of related brand.

e.g my last sales date is 25 September so it should calculate dates between August 29 - September 25,  

but if a brand's last sale date was September 20, calculation was between  August 24 - September 20 which is wrong.

 

I believe I have to update my DATESINPERIOD formula in "Margin % Last 28 Days Sales", I tried to add a Variable to get MAX Date of Sales Table but couldn't write a correct measure.

 

How should I add Max Date to my formula?

 

edit: I added a sample file

 

thanks in advance, regards

1 ACCEPTED SOLUTION
jamuka
Advocate II
Advocate II

I found the solution from another post calculating sum for max date

 

Instead of using LASTDATE in my formula to find latest date in Sales Table I used MAX. 

 

original code

Margin % Last 28 Days Sales = 
CALCULATE (
    [Total Margin %],
    DATESINPERIOD ( 'Sales Report'[Sales Date], LASTDATE ( 'Sales Report'[Sales Date] ), -28, DAY )
)

 

updated code

Margin % Last 28 Days Sales_V2 = 
VAR LatestDate = 
CALCULATE(MAX('Sales Report'[Sales Date]), 
ALL('Sales Report'))
Return
CALCULATE (
    [Total Margin %],
    DATESINPERIOD ( 'Date'[Date], LatestDate, -28, DAY )
)

 

View solution in original post

3 REPLIES 3
jamuka
Advocate II
Advocate II

I found the solution from another post calculating sum for max date

 

Instead of using LASTDATE in my formula to find latest date in Sales Table I used MAX. 

 

original code

Margin % Last 28 Days Sales = 
CALCULATE (
    [Total Margin %],
    DATESINPERIOD ( 'Sales Report'[Sales Date], LASTDATE ( 'Sales Report'[Sales Date] ), -28, DAY )
)

 

updated code

Margin % Last 28 Days Sales_V2 = 
VAR LatestDate = 
CALCULATE(MAX('Sales Report'[Sales Date]), 
ALL('Sales Report'))
Return
CALCULATE (
    [Total Margin %],
    DATESINPERIOD ( 'Date'[Date], LatestDate, -28, DAY )
)

 

v-henryk-mstf
Community Support
Community Support

Hi @jamuka ,

 

Maybe you can try DATESBETWEEN function. Such like below:

sumOf_date = 
CALCULATE (
    SUM ( 'Table'[Value] ),
    DATESBETWEEN ( 'Table'[Date], DATE ( 2022, 9, 25 ), TODAY() )
)

vhenrykmstf_0-1664248449496.png

 

If the problem is still not resolved, please provide detailed error information and test data. Looking forward to your reply.


Best Regards,
Henry


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

hello @v-henryk-mstf,

 

thank you for you reply.

I don't want to add static date to my measure because that means I have to change it every time.

Which at the moment I did by filtering my visual via date filter. I attached a sample file to my post.

 

kind regards

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.