Forum Discussion

Hichamas4's avatar
Hichamas4
Frequent Visitor
8 months ago
Solved

Calculate reference year for all year per month

Hello masters, I have a question, I would like to calculate the percentage (Sales %) = DIVIDE( ( Sales Ref - Sales), Sales Ref, 0) Where the values for Sales Ref should be deduplicated for all Year...
  • amitchandak's avatar
    amitchandak
    8 months ago

    Hichamas4 , I think for sales refer you would still want the month filter while losing the year filter 

    New Measure = 

    Calculate( Sum(Sales[Sales refe]), FIlter(All('Date') , 'Date' [Month] = Max( 'Date' [Month] ) &&  'Date' [Year] = "2021") ) 

  • HarishKM's avatar
    8 months ago

    Hichamas4 Hey,

    • Your [Sales Ref] is fixed to 2021, but at the YearMonth grain the context includes the current Year (e.g., 2022‑01). Filtering to Year=2021 and 2022‑01 together returns no rows → BLANK.
    • Fix: ignore the current Year filter, keep only Month‑of‑Year, and force Year = 2021.

    Setup

    • Use a proper Date table with columns: Date, Year, MonthNumberOfYear (1–12), YearMonth (YYYY‑MM, sorted by MonthNumberOfYear).

    Measures

    1) Reference to same month in 2021
    Sales Ref (2021 same month) =
    VAR m = SELECTEDVALUE('Date'[MonthNumberOfYear])
    RETURN
    CALCULATE(
    [Sales],
    FILTER(
    ALL('Date'),
    'Date'[Year] = 2021 &&
    'Date'[MonthNumberOfYear] = m
    )
    )

    Alternative (same logic, shorter):
    Sales Ref (2021 same month) =
    CALCULATE(
    [Sales],
    ALLEXCEPT('Date', 'Date'[MonthNumberOfYear]),
    KEEPFILTERS('Date'[Year] = 2021)
    )

    2) Percentage
    Sales % =
    VAR Ref = [Sales Ref (2021 same month)]
    RETURN IF(NOT ISBLANK(Ref), DIVIDE([Sales] - Ref, Ref), BLANK())

    Optional: dynamic reference year (What‑If slicer)
    Ref Year = SELECTEDVALUE('Ref Year'[Value], 2021)

    Sales Ref (dynamic) =
    VAR ry = [Ref Year]
    VAR m = SELECTEDVALUE('Date'[MonthNumberOfYear])
    RETURN
    CALCULATE(
    [Sales],
    FILTER(ALL('Date'), 'Date'[Year] = ry && 'Date'[MonthNumberOfYear] = m)
    )

    Notes

    • If you want 2021 full‑year repeated across every month, drop the month filter:


    CALCULATE([Sales], REMOVEFILTERS('Date'[MonthNumberOfYear]), KEEPFILTERS('Date'[Year] = 2021))

    • Ensure YearMonth on the visual comes from the Date table.



      Thanks

      Haish K 

      If I resolve your issue. Kindly give kudos to this post and accept it as a solution so other can refer this.