Forum Discussion

etane's avatar
etane
Helper V
5 months ago
Solved

Previous Year Measure for Line Chart

Hello.   I am trying to create a YOY cumulative active customer by month line chart.  The line should show the number of customers who made at least one purchase.  One for Selected Year and another...
  • mizan2390's avatar
    5 months ago

    hi etane,

     The reason you are encountering a flat line when trying to compute the previous year is due to how ALLSELECTED and REMOVEFILTERS interact with your filter context, combined with the hardcoded year filter in your base measure.
    Here is exactly what is happening:
    The flat line issue: When you use REMOVEFILTERS ( 'Z - Calendar' ), DAX removes all filters on the calendar table, including the Month filter coming from your line chart's X-axis
    . Without the Month filter, the measure evaluates the grand total for every point on the axis, resulting in a flat line.
    The year-shift issue: In your Active Acustomrs CY (Cumulative) measure, you used ALLSELECTED('Z - Calendar'). ALLSELECTED restores the original filter context from the visual
    . If you have a slicer set to 2026, ALLSELECTED will aggressively restore the 2026 filter, overriding any attempt to shift the date back to 2025. Additionally, your Active Customer CY Count measure explicitly forces 'Z - Calendar'[Year#] = CurrentYear, which locks the calculation into the currently visible year and breaks standard time-intelligence shifts.
    To easily calculate a cumulative distinct count for both the current year and the previous year, the best practice is to remove the manual year overrides and use standard DAX time intelligence functions like DATESYTD and SAMEPERIODLASTYEAR.

     

    Active Customer Base Count = 
    CALCULATE(
        DISTINCTCOUNT(Order_Table[Customer_Code]),
        FILTER(
            VALUES(Order_Table[Customer_Code]),
            [A Type Order Count] > 0
        )
    )
    Create the CY Cumulative Measure
    Instead of using ALLSELECTED, use the DATESYTD function. DATESYTD automatically modifies the filter context, gathering all dates from January 1st up to the last visible date on your chart's axis.
    Because DISTINCTCOUNT is evaluated over this expanding set of dates, a customer buying in both January and February will correctly only be counted once.
    Active Customers CY (Cumulative) = 
    VAR TodayDate = TODAY()
    VAR MaxDate = MAX('Z - Calendar'[Date])
    VAR MonthStart = DATE(YEAR(MaxDate), MONTH(MaxDate), 1)
    
    RETURN
    IF(
        MonthStart > TodayDate,
        BLANK(),   -- hides months that are entirely in the future
        CALCULATE(
            [Active Customer Base Count],
            DATESYTD('Z - Calendar'[Date])
        )
    )

    To get the exact same cumulative calculation for the previous year, you can nest time intelligence functions. By wrapping DATESYTD inside SAMEPERIODLASTYEAR, DAX will calculate the year-to-date distinct count but safely shift the date context backward by exactly one year.

    Active Customers PY (Cumulative) = 
    VAR TodayDate = TODAY()
    VAR MaxDate = MAX('Z - Calendar'[Date])
    VAR MonthStart = DATE(YEAR(MaxDate), MONTH(MaxDate), 1)
    
    RETURN
    IF(
        MonthStart > TodayDate,
        BLANK(),
        CALCULATE(
            [Active Customer Base Count],
            SAMEPERIODLASTYEAR( DATESYTD('Z - Calendar'[Date]) )
        )
    )
    • By relying on DATESYTD, you no longer wipe out the month filter on the X-axis, meaning the line will correctly slope upwards rather than remaining flat.
    • SAMEPERIODLASTYEAR seamlessly shifts the DATESYTD period back 12 months without requiring you to manually strip filters with REMOVEFILTERS.
    • Your line chart will now correctly plot 2026 vs. 2025 cumulative active customers.

    I have attached the link for the file: Customer Count Test.pbix

    Please mark this if this solved your problem.