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

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

Reply
bvy
Helper V
Helper V

DAX - Help with Measure to find Sales from Last Week or Earlier Week

Hello. I have a very typical data model with a Sales fact table and a Dates table. I have a measure that looks like this: 

 

SalesLastWeek = CALCULATE([Sales], FILTER(ALL(Dates), Dates[WeekNum] = MAX(Dates[WeekNum]) - 1))
 
The issue is that there isn't always sales for a particular selection from last week -- in which case I'd like to alter the formula to look at earlier weeks and pick the most recent one that had sales. Imagine for example that the store is closed during Christmas week. Then SalesLastWeek for the first week in January should return sales for the week before Christmas, loosely speaking. Yes, I realize "SalesLastWeek" becomes a misnomer now... 
 
I know details are sketchy here so feel free to make assumptions. I just need a direction. Thanks. 
1 REPLY 1
MFelix
Super User
Super User

Hi @bvy ,

 

You need to pick up the last week that has data so you need to do somethig similar to the following:

 

SalesLastWeek =
VAR MAXWEEKYEAR =
    MAXX (
        TOPN (
            1,
            FILTER (
                ALL ( Dates[WeeKNumYear] ),
                Dates[WeeKNumYear] <= MAX ( Dates[WeeKNumYear] )
            ),
            Dates[WeeKNumYear], DESC
        ),
        Dates[WeeKNumYear]
    )
RETURN
    CALCULATE (
        [Sales],
        FILTER ( ALL ( Dates ), Dates[WeekNumYear] = MAXWEEKYEAR )
    )

I added a WeenunmYear because if you only have week num on the change of year result will be incorrect.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português





Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.