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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
michellepace
Resolver III
Resolver III

Dax formula for "4 Weeks to Date" (I have 1WTD)

Hi there. In the first image below I have plotted "One Week to Date (1WTD)" sales and "Month to Date (MTD)" sales. I would like to replace MTD with "4 weeks to date (4WTD)". I would then get a consistent pattern as illustrated in image 2 (where I hand drew 4WTD). 

Question:

q.png

t.png

s.png

 

 

 

 

 

 

 

 

 

 

As always, thank you.
Michelle

 

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@michellepace 

Please find below the measures that calculates  One Week-To-Date and  Four Week-To-Date. I used my approach for One Week To Date as it will dynamically change the week based on the current date selection range.

 

New Sales 1WTD = 
var CurrentDate=LASTDATE(Dates[Date])
var DayOne = CALCULATE(MIN(Dates[Date]),WEEKDAY(Dates[Date],3)=0,ALLSELECTED(Dates[Date]))
var FourWeekStart = MAXX(GENERATESERIES(DayOne,CurrentDate,7),[Value])
return
CALCULATE(
    [Total Sales],
    FILTER(
        ALLSELECTED(Dates[Date]),
        Dates[Date] >= FourWeekStart && Dates[Date] <= CurrentDate)
)
New Sales 4WTD = 
var CurrentDate=LASTDATE(Dates[Date])
var DayOne = CALCULATE(MIN(Dates[Date]),WEEKDAY(Dates[Date],3)=0,ALLSELECTED(Dates[Date]))
var FourWeekStart = MAXX(GENERATESERIES(DayOne,CurrentDate,28),[Value])
return
CALCULATE(
    [Total Sales],
    FILTER(
        ALLSELECTED(Dates[Date]),
        Dates[Date] >= FourWeekStart && Dates[Date] <= CurrentDate)
)

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

6 REPLIES 6
Fowmy
Super User
Super User

@michellepace 

Please find below the measures that calculates  One Week-To-Date and  Four Week-To-Date. I used my approach for One Week To Date as it will dynamically change the week based on the current date selection range.

 

New Sales 1WTD = 
var CurrentDate=LASTDATE(Dates[Date])
var DayOne = CALCULATE(MIN(Dates[Date]),WEEKDAY(Dates[Date],3)=0,ALLSELECTED(Dates[Date]))
var FourWeekStart = MAXX(GENERATESERIES(DayOne,CurrentDate,7),[Value])
return
CALCULATE(
    [Total Sales],
    FILTER(
        ALLSELECTED(Dates[Date]),
        Dates[Date] >= FourWeekStart && Dates[Date] <= CurrentDate)
)
New Sales 4WTD = 
var CurrentDate=LASTDATE(Dates[Date])
var DayOne = CALCULATE(MIN(Dates[Date]),WEEKDAY(Dates[Date],3)=0,ALLSELECTED(Dates[Date]))
var FourWeekStart = MAXX(GENERATESERIES(DayOne,CurrentDate,28),[Value])
return
CALCULATE(
    [Total Sales],
    FILTER(
        ALLSELECTED(Dates[Date]),
        Dates[Date] >= FourWeekStart && Dates[Date] <= CurrentDate)
)

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Hello @Fowmy .

You've done it again, thank you so very much! I have uploaded the solution file here, in case it is useful to anyone else. This is what it looks like:

1.png

 

It's going to take me a little while to pick through your equations to understand and learn from them. Is there a way I can "step-through" them and see the values of the variables as the equation evaluates? Would I need to use something like Dax Studio to do this? (I've only heard of it, never used it)

@michellepace 

Glad it worked for!

Follow these links to learn how to step through DAX codes and even debug them. I mostly use CONCATENATEX and COMBINEVALUES.

https://www.youtube.com/watch?v=9SV2VnYbgg4
https://radacad.com/some-simple-ways-to-debug-your-dax-measure-code-in-power-bi-debugging-virtual-ta...

Good Luck
Fowmy




Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

For anyone interested - the youtube video Fowny recommends above (10 minutes from guy in a cube) is EXCELLENT.

Greg_Deckler
Super User
Super User

@michellepace - First, you probably want something like Rolling Weeks - https://community.powerbi.com/t5/Quick-Measures-Gallery/Rolling-Weeks/m-p/391694#M128

 

Also, you might find Sequential helpful - https://community.powerbi.com/t5/Quick-Measures-Gallery/Sequential/m-p/380231#M116

 

Also, you may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors