Forum Discussion
Trailing twelve month variable on computed field doesn't update with time slicer
Dear Community, I am confronted with the following situation:
Context:
I want to create a graph that shows the evolution of my sales over the last 12 months (Trailing Twelve months - TTM) BUT where each month is already the sum of the last 12 months (Moving Annul Total - MAT). And I'd like this view to change according to a time slicer. For example, if I choose the month of May 23, I should see:
- The month of May 23', the sum of the months from May 22 to May 23.
- The month of April 23', the sum of april 22 to april 23.
- The month of March 23', the sum of march 22 to march 23.
- Etc... for a total of 12 months
Problem:
My problem is that the graph doesn't follow the time slicer at all. When I put a month in my slicer, the graph gives me a single value for the last 30 days, whereas I'd like to see the last 12 months.
This is what I've done:
- I created a value for the MAT of sales
Sales_MAT = CALCULATE(
sum(Sales[Sales]),DATESINPERIOD('Date Dimension'[DateDimension],
MAX('Date Dimension'[DateDimension]),-12,MONTH) )
- I created a value allowing me to have the last 12 months
TTM_Total_Sales =
VAR CurrentDate = MAX('Date Dimension'[DateDimension])
VAR PreviousDate = DATE(YEAR(CurrentDate),MONTH(CurrentDate)-12,DAY(CurrentDate))
VAR Result =
CALCULATE(
Sales[Sales_MAT],
FILTER(
ALL('Date Dimension'[DateDimension]),
'Date Dimension'[DateDimension]>=PreviousDate && 'Date Dimension'[DateDimension]<=CurrentDate
)
)
RETURN
Result
And I don't understand what else I need to do....
Already thank you for your help !
For those who prefer to work with the full file, you can download it here (through "We transfer"): https://we.tl/t-xDys0zqAHG
1 Reply
- Palak_AgarwalRegular VisitorStep 1: Create a Disconnected Date Table for the SlicerCreate a new table for the slicer that won't interfere with the main model:Slicer_MonthYear =ADDCOLUMNS(CALENDARAUTO(),"YearMonth", FORMAT([Date], "MMM YYYY"),"YearMonth_sort", YEAR(Slicer_MonthYear[Date]) + (MONTH(Slicer_MonthYear[Date])/100))YearMonth_sort column to sort YearMonth in slicerThen add:Slicer_Month = STARTOFMONTH([Date])This table is not connected to your main Date table — it's used only for the slicer. (As shown in following image)Step 2: Add the Slicer to Your ReportAdd a slicer visualUse 'Slicer_MonthYear'[Slicer_Month]Set the slicer to Single Select (optional but recommended)Step 3: Build Your Line ChartX-axis: 'Date Dimension'[Date] (from your connected Date table)Y-axis: SUM('Sales'[SalesAmount])Make sure 'Date Dimension' is connected to 'Sales' via the 'Date Dimension'[Date] field.Step 4: Create the Visual Filter LogicCreate the following measure to act as a visual-level filter:Sales_Last12Months =VAR SelectedDate = MAX(Slicer_MonthYear[Date])VAR StartDate = EDATE(SelectedDate, -12)RETURNIF(MAX('Date Dimension'[Date]) >= StartDate && MAX('Date Dimension'[Date]) <= SelectedDate,1,0)This measure checks whether each point on the X-axis falls within the last 12 months based on the selected slicer value.Step 5: Apply Visual-Level Filter to the Line ChartSelect the line chartDrag the Sales_Last12Months measure into the Visual Level Filters paneSet the filter to is = 1This filters the line chart to only show the dates in the rolling 12-month window.Result:When the user selects Apr 2023 → chart shows Apr 2022 to Apr 2023When the user selects Dec 2024 → chart shows Dec 2023 to Dec 2024If this post helps, please accept this as a solution. Appreciate your kudos.Regards,Palak