Forum Discussion

SeanGaffney's avatar
SeanGaffney
New Member
1 year ago
Solved

Cumulative Average but for a specific date range

Hello,

 

I currently have a cumulitive average calculated but it does it progressively for the whole sheet.

 

CumulativeAverage =
CALCULATE(
    AVERAGE('Roast'[Spec 430nm Colour]),
    FILTER(
        ALL('roast'),
        'Roast'[Tropos Lot Number] <= MAX('Roast'[Tropos Lot Number])
    )
)

 

What I would like is for the timeline slicer to determine the rolling average as well as the UCL and LCL

 

Ive tried formating it with the Calender DAX, 

Calender = CALENDAR(MIN(Roast[Date].[Date]),MAX(Roast[Date].[Date]))
 
But I dont end up with the correct average for example June should be 1099 average for the month but currently It is doing it per Tropos lot no.
 
Ive attahced the excel sheet Excel  , its  the Tab Roast and the Model Power BI . Any more than 5 months displayed at the same time and the model will crash.
 
Thanks
Sean
  • Hi SeanGaffney 

    Please try this one:

    OptimizedCumulativeAverage = 
    VAR SelectedDate = MAX('Calendar'[Date])
    VAR YearMonthValue = SELECTEDVALUE('Calendar'[YearMonth]) // Create yyyymm column
    RETURN
    CALCULATE(
        AVERAGE('Roast'[Spec 430nm Colour]),
        FILTER(
            ALLSELECTED('Roast'),
            'Roast'[YearMonth] <= YearMonthValue
        )
    )

    If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

    Thank you.

     

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi SeanGaffney,

    Thanks for reaching out to the Microsoft fabric community forum.


    To enable a rolling average by month in Power BI, we first create a Calendar table using DAX, including both YearMonth and YearMonthNumber for sorting:

     

    Here’s the DAX measure I used:

     

    Calendar =
    VAR MinDate = CALCULATE(MIN('Roast'[Date]), ALL('Roast'))
    VAR MaxDate = CALCULATE(MAX('Roast'[Date]), ALL('Roast'))
    RETURN
    ADDCOLUMNS(
    CALENDAR(MinDate, MaxDate),
    "YearMonth", FORMAT([Date], "YYYY-MM"),
    "YearMonthNumber", YEAR([Date]) * 100 + MONTH([Date])
    )


    We relate Calendar[Date] to Roast[Date], and sort YearMonth by YearMonthNumber. Then we create the rolling average measure:

     

    Here’s the DAX measure I used:

     

    OptimizedCumulativeAverage =
    VAR SelectedDate = MAX('Calendar'[Date])
    RETURN
    CALCULATE(
    AVERAGE('Roast'[Spec 430nm Colour]),
    FILTER(ALL('Calendar'), 'Calendar'[Date] <= SelectedDate)
    )


    Using Calendar[YearMonth] in a slicer and this measure in a line chart or table, users can view dynamic rolling trends month by month.

     

    Find attached .PBIX for your reference.

     

    If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it

    Best Regards,
    Tejaswi.
    Community Support Team.

     

     

     

     

4 Replies

  • Hi SeanGaffney 

    Please try this one:

    OptimizedCumulativeAverage = 
    VAR SelectedDate = MAX('Calendar'[Date])
    VAR YearMonthValue = SELECTEDVALUE('Calendar'[YearMonth]) // Create yyyymm column
    RETURN
    CALCULATE(
        AVERAGE('Roast'[Spec 430nm Colour]),
        FILTER(
            ALLSELECTED('Roast'),
            'Roast'[YearMonth] <= YearMonthValue
        )
    )

    If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

    Thank you.

     

    • SeanGaffney's avatar
      SeanGaffney
      New Member

      Hello,

       

      Thank you very much for your help this worked once I added the column in

       

      Sean

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi SeanGaffney,

    Thanks for reaching out to the Microsoft fabric community forum.


    To enable a rolling average by month in Power BI, we first create a Calendar table using DAX, including both YearMonth and YearMonthNumber for sorting:

     

    Here’s the DAX measure I used:

     

    Calendar =
    VAR MinDate = CALCULATE(MIN('Roast'[Date]), ALL('Roast'))
    VAR MaxDate = CALCULATE(MAX('Roast'[Date]), ALL('Roast'))
    RETURN
    ADDCOLUMNS(
    CALENDAR(MinDate, MaxDate),
    "YearMonth", FORMAT([Date], "YYYY-MM"),
    "YearMonthNumber", YEAR([Date]) * 100 + MONTH([Date])
    )


    We relate Calendar[Date] to Roast[Date], and sort YearMonth by YearMonthNumber. Then we create the rolling average measure:

     

    Here’s the DAX measure I used:

     

    OptimizedCumulativeAverage =
    VAR SelectedDate = MAX('Calendar'[Date])
    RETURN
    CALCULATE(
    AVERAGE('Roast'[Spec 430nm Colour]),
    FILTER(ALL('Calendar'), 'Calendar'[Date] <= SelectedDate)
    )


    Using Calendar[YearMonth] in a slicer and this measure in a line chart or table, users can view dynamic rolling trends month by month.

     

    Find attached .PBIX for your reference.

     

    If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it

    Best Regards,
    Tejaswi.
    Community Support Team.