Forum Discussion
DAX last 4 weeks
- 1 year ago
Hi ash_best_
1. Create a disconnected Week table:
WeekCalendar = SUMMARIZE( 'Date', 'Date'[WeekStart], 'Date'[YearWeek] )2. Derive the max week from the slicer selection:
MaxWeek = CALCULATE( MAX('Date'[YearWeek]), ALLSELECTED('Date') )3. Sell-through by week measure
Put this measure on the visual Y-axis. It uses the WeekCalendar row context (the axis), pulls sales & Monday stock for that week from the actual tables (ignoring slicer min), and returns BLANK() when the week is not among the last 4 weeks relative to MaxWeek.SellThrough_ByWeek = VAR _ThisWeek = SELECTEDVALUE(WeekCalendar[YearWeek]) VAR _WeekStart = SELECTEDVALUE(WeekCalendar[WeekStart]) VAR _MaxWeek = [MaxWeek] VAR _Sales = CALCULATE( SUM(Sales[SalesAmount]), FILTER( ALL('Date'), 'Date'[YearWeek] = _ThisWeek ) ) VAR _Stock = CALCULATE( SUM(Stock[StockQty]), FILTER( ALL('Date'), 'Date'[Date] = _WeekStart ) ) VAR _Result = DIVIDE(_Sales, _Stock) RETURN IF( _ThisWeek >= _MaxWeek - 3 && _ThisWeek <= _MaxWeek, _Result, BLANK() )Set WeekCalendar[WeekStart] as the X-axis (sorted ascending) and use SellThrough_ByWeek on the Y-axis. Keep your Date slicer on Date[Date], as it drives the [MaxWeek] measure. No additional visual-level filters are required, though you may optionally filter out blank SellThrough_ByWeek values for clarity.
If this still does not resolve your scenario, kindly share a sample PBIX file with masked or dummy data so we can better understand your data model and assist you in the best possible way.
Regards,
Microsoft Fabric Community Support Team.
Step 1: Create a Week Number Column (if not already in Date table
YearWeek key:
YearWeek = 'Date'[Year] * 100 + WEEKNUM('Date'[Date], 2)
Step 2: Find Max Week in Context
MaxWeek =
CALCULATE(
MAX('Date'[YearWeek]),
ALLSELECTED('Date')
)
Step 3: Create a Last 4 Weeks Flag
IsLast4Weeks =VAR _MaxWeek = [MaxWeek]
VAR _MinWeek = _MaxWeek - 3
RETURN
IF('Date'[YearWeek] >= _MinWeek && 'Date'[YearWeek] <= _MaxWeek, 1, 0)
Step 4: Sell-through Measure
SellThrough =
DIVIDE(
SUM(Sales[SalesAmount]),
CALCULATE(
SUM(Stock[StockQty]),
FILTER(
ALL('Date'),
'Date'[IsLast4Weeks] = 1 &&
WEEKDAY('Date'[Date],2) = 1 // take stock only Mondays
)
)
)
Step 5: Visual Setup
Put Date[Week Start] (or YearWeek) on X-axis.
Use SellThrough as Y-axis.
Add visual-level filter → IsLast4Weeks = 1.
Now your line chart will always show last 4 weeks from the latest week selected, even if slicer range is bigger.
Hi,
There are problems:
- if i put this stock is same for every week and the stock value should be as per the week
- if i reduce my slicer range the week data dissapears, I want it to show last 4 weeks from the max week of slicer regardless of the slicer min week dates