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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
NydelFyr
Frequent Visitor

Calculating Total Sales of the last 4 weeks for each weeks (axis)

Hello everybody. I'm struggling since a week on a dax formula.

 

For each week of my week axis, I would like to have the value of the production of the last 4 weeks of the week concerned.

 

I think i will have to use some loops, but i have no idea how to deal with.

 

The granularity of my database is the day.

 

Best regards,

Kilian

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

-- This measure sums up Value in the Production fact table:

[Total Production] =
	SUM( Production[Value] )
	
	
-- The Production table must have a date field on which
-- it'll join in a many-to-one fashion to a Calendar
-- date table (that is marked as Date Table).
-- In the Calendar the granularity will -- be a single day (obviously). -- Let's say you've selected for your x-axis the -- unique name of the week. Then your measure will be: [Production L4W] = -- L4W = Last 4 Weeks var __lastDayVisible = LASTDATE( 'Calendar'[Date] ) var __numOfDaysInPeriod = 7 * 4 - 1 --> -1 since the last day counts as well! var __production = CALCULATE( [Total Production], DATESINPERIOD( 'Calendar'[Date], __lastDayVisible, - __numOfDaysInPeriod, DAY ) ) return __production -- By the way, this will work for any periods you select -- from Calendar. It'll first grab the last day in the -- period and then will calculate the total production -- for the period from (the last visible date - 7 * 4 days + 1) -- up to and including the last visible date. This interval
-- has exactly 4 * 7 days.

Best

Darek

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

You should be careful when you're close to the beginning of your Calendar. What would you like to calculate if there are not enough days when you go back 4 weeks from the last visible day? You might want to blank out the measure. To do that you'll have to calculate the number of days returned by DATESINPERIOD and if it's not 4 * 7, then return a BLANK().

 

Best

Darek

Anonymous
Not applicable

-- This measure sums up Value in the Production fact table:

[Total Production] =
	SUM( Production[Value] )
	
	
-- The Production table must have a date field on which
-- it'll join in a many-to-one fashion to a Calendar
-- date table (that is marked as Date Table).
-- In the Calendar the granularity will -- be a single day (obviously). -- Let's say you've selected for your x-axis the -- unique name of the week. Then your measure will be: [Production L4W] = -- L4W = Last 4 Weeks var __lastDayVisible = LASTDATE( 'Calendar'[Date] ) var __numOfDaysInPeriod = 7 * 4 - 1 --> -1 since the last day counts as well! var __production = CALCULATE( [Total Production], DATESINPERIOD( 'Calendar'[Date], __lastDayVisible, - __numOfDaysInPeriod, DAY ) ) return __production -- By the way, this will work for any periods you select -- from Calendar. It'll first grab the last day in the -- period and then will calculate the total production -- for the period from (the last visible date - 7 * 4 days + 1) -- up to and including the last visible date. This interval
-- has exactly 4 * 7 days.

Best

Darek

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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