Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Average Running total each quarter interval

I need running total for each quarter interval DAX Formula

result shown in the table "average running total"

2025-Q2 still running quarter show value as zero once this quarter is completed then automatically change to as usual running total, suggest

 

Quarter Salesaverage running total
2023-Q1 3030
2023-Q2 2025
2023-Q3 1020
2023-Q4 3022.5
2024-Q1 3423.5
2024-Q2 4529.75
2024-Q3 3435.75
2024-Q4 3236.25
2025-Q1 4539
2025-Q2 500

 

  • danextian's avatar
    danextian
    1 year ago

    Try this:

    Qtr Avg Running Total 3 = 
    // Get latest visible date with data
    VAR LatestDateWithData =
        CALCULATE (
            LASTNONBLANK ( Dates[Date], [Total Revenue] ),
            ALLSELECTED ( Dates )
        ) // Get earliest YearQuarter
    VAR EarliestQtr =
        CALCULATE ( MIN ( Dates[YearQuarter] ), ALL ( Dates ) )
    VAR LatestMonthNumber =
        MONTH ( LatestDateWithData ) // Get month number of latest date
    VAR QuarterNumber4Start =
        EDATE (
            CALCULATE ( MINX ( STARTOFQUARTER ( Dates[Date] ), [Date] ), ALL ( Dates ) ),
            3 * 3
        )
    VAR LatestYearQuarter =
        FORMAT ( LatestDateWithData, "YYYY0Q" ) // Get YearQuarter key of latest date
    VAR RowQuarter =
        MAX ( Dates[YearQuarter] ) // Get YearQuarter key for current row
    VAR QuarterEnds = { 3, 6, 9, 12 } // Quarter end months
    VAR StartOfQtr =
        MINX ( STARTOFQUARTER ( Dates[Date] ), [Date] )
    VAR StartOfPrevQtr = StartOfQtr - 1 // Day before start of current quarter
    VAR _Result =
        IF (
            HASONEVALUE ( Dates[YearQuarter] ),
            SWITCH (
                TRUE (),
                StartOfQtr <= QuarterNumber4Start, 0, // Show 0 if earliest quarter
                LatestYearQuarter = RowQuarter
                    && NOT LatestMonthNumber
                        IN QuarterEnds && ISFILTERED ( Dates[YearQuarter] ), 0, // Show 0 if latest incomplete quarter
                CALCULATE (
                    AVERAGEX (
                        VALUES ( Dates[YearQuarter] ),
                        [Total Revenue]
                    ),
                    DATESINPERIOD ( Dates[Date], StartOfPrevQtr, -4, QUARTER ),
                    REMOVEFILTERS ( Dates )
                )
            )
        )
    RETURN
        _Result
    

     

14 Replies

  • Assuming you have a date table and if there is not a quarter column in it you need to add one

    QuarterID = 
    "Q" & FORMAT(QUARTER('Date'[Date]), "0") & "-" & FORMAT('Date'[Date], "YYYY")

    Use a measure or variable to dynamically detect the current quarter:

    CurrentQuarter =
    VAR TodayDate = TODAY()
    RETURN
        FORMAT(TodayDate, "YYYY") & "-Q" & FORMAT(TodayDate, "Q")
    

     

    Running Average Excluding Current Quarter

    Average Running Total =
    VAR CurrentQ = FORMAT(TODAY(), "YYYY") & "-Q" & FORMAT(TODAY(), "Q")
    VAR SelectedQ = SELECTEDVALUE('Date'[QuarterLabel])
    VAR IsPast = SelectedQ < CurrentQ
    
    RETURN
    IF (
        IsPast,
        CALCULATE (
            AVERAGEX (
                DATESQTD('Date'[Date]),
                CALCULATE(SUM('SalesTable'[Sales]))
            ),
            FILTER (
                ALLSELECTED('Date'),
                'Date'[QuarterLabel] <= SelectedQ &&
                'Date'[QuarterLabel] < CurrentQ
            )
        ),
        0
    )


    Please mark this post as a solution if it helps you. Appreciate Kudos.

  • Hi Anonymous ,

     

    You can achieve this requirement in DAX with a measure that checks if the current quarter is incomplete (i.e., the latest quarter and still running), and returns zero for it; otherwise, it will calculate the average running total as usual.

    Suggested DAX Measure:

    dax
     
    Average Running Total =
    VAR MaxQuarter = MAX('Table'[Quarter])
    VAR CurrentQuarter = SELECTEDVALUE('Table'[Quarter])
    VAR IsCurrent = CurrentQuarter = MaxQuarter
    RETURN
        IF(
            IsCurrent,
            0,  // Show 0 for the latest (still running) quarter
            CALCULATE(
                AVERAGEX(
                    FILTER(
                        ALL('Table'),
                        'Table'[Quarter] <= CurrentQuarter
                    ),
                    'Table'[Sales]
                )
            )
        )
    • Replace 'Table' and [Sales] with your actual table and column names if different.
    • This measure shows 0 for the most recent quarter, and once the next quarter’s data is added, the previous one will become part of the running total as expected.

    If you want the logic to be based on a date field instead of a quarter column, or need the measure to update dynamically when new data is available, let me know!

    Hope this helps!
    translation and formatting supported by AI

    • Anonymous's avatar
      Anonymous
      Not applicable
      result showing as zero :
       
      Running Total Quarterly =
      VAR MaxQuarter = MAX('Calendar'[YYYY-QQ])
      VAR CurrentQuarter = SELECTEDVALUE('Calendar'[YYYY-QQ])
      VAR IsCurrent = CurrentQuarter = MaxQuarter
      RETURN
          IF(
              IsCurrent,
              0,  // Show 0 for the latest (still running) quarter
              CALCULATE(
                  AVERAGEX(
                      FILTER(
                          ALL('Calendar'),
                          'Calendar'[YYYY-QQ] <= CurrentQuarter
                      ),
                      [value]
                  )
              )
          )
      • danextian's avatar
        danextian
        Icon for Super User rankSuper User

        burakkaragoz another AI copy-paste?

         

        Anonymous  That is because MaxQuarter and CurrentQuarter return the same value in the current row,  IsCurrent is TRUE and thus the condition returns zero.


  • Ashish_Excel's avatar
    Ashish_Excel
    Icon for Solution Supplier rankSolution Supplier

    Hi,

    Does your actual data have a proper date column?  If yes, then share that data in a format that can be pasted in an MS Excel file.  Also, would Q1 be Apr-Jun or Jan-Mar?

  • Hi Anonymous 

     

    Use a dedicated dates table with a quarterkey that can be sorted chronologically and try this:

    Qtr Avg Running Total = 
    // Get the latest date that has data, considering slicers (ALLSELECTED)
    VAR LatestDateWithData =
        CALCULATE (
            LASTNONBLANK ( Dates[Date], [Total Revenue] ),
            ALLSELECTED ( Dates )
        )
    // Get the month number of the latest date with data
    VAR LatestMonthNumber =
        MONTH ( LatestDateWithData )
    // Get the YearQuarter key (formatted as "YYYYQQ") for the latest date with data
    VAR LatestYearQuarter =
        FORMAT ( LatestDateWithData, "YYYYQQ" )
    // Get the YearQuarter key for the current row
    VAR RowQuarter =
        MAX ( Dates[YearQuarter] )
    // Define the end months of each quarter
    VAR QuarterEnds = { 3, 6, 9, 12 }
    RETURN
        IF (
            // Check if the current row is the latest (still running) quarter
            LatestYearQuarter = RowQuarter
                && NOT LatestMonthNumber
                    IN QuarterEnds && ISFILTERED ( Dates[YearQuarter] ),
            0, // Show 0 for the latest incomplete quarter
            CALCULATE (
                // Calculate the running average up to the current quarter
                AVERAGEX ( VALUES ( Dates[YearQuarter] ), [Total Revenue] ),
                FILTER ( ALL ( Dates[YearQuarter] ), Dates[YearQuarter] <= RowQuarter )
            )
        )
    

     

    Please see the attached pbix for the details.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      danextian  - Thanks for your response.

       

      Basically we need to compare current quarter Vs previous 4 Quarter Average average price

      example current quarter price 40 and last 4 quarter average = 60 i.e 240/4  (60,30,70,80)

      revenue down = 20 so highlight as red color using conditional formatting.

       

      I gone thru pbix file and my view outputs - Highlighted and should be numbers, column C as per DAX Formula

      please suggest

      • danextian's avatar
        danextian
        Icon for Super User rankSuper User

        You can change the RETURN statement to this:

        RETURN
            IF (
                // Check if the current row is the latest (still running) quarter
                LatestYearQuarter = RowQuarter
                    && NOT LatestMonthNumber
                        IN QuarterEnds && ISFILTERED ( Dates[YearQuarter] ),
                0, // Show 0 for the latest incomplete quarter
                CALCULATE (
                    // Calculate the running average up to the current quarter
                    AVERAGEX ( VALUES ( Dates[YearQuarter] ), [Total Revenue] ),
                    DATESINPERIOD ( Dates[Date], MAX ( Dates[Date] ), -4, QUARTER ),
                    REMOVEFILTERS ( Dates )
                )
            )

        REMOVEFILTERS ( Dates ) is not necessary if you have set your Dates/Calendar table as such. 

        Set and use date tables in Power BI Desktop 

  • v-saisrao-msft's avatar
    v-saisrao-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous,
    I wanted to check if you had the opportunity to review the information provided by danextian andrewsommer burakkaragoz . Please feel free to contact us if you have any further questions. If the response has addressed your query, please accept it as a solution so other members can easily find it.
    Thank you.