Forum Discussion

brekeke's avatar
brekeke
Frequent Visitor
8 years ago
Solved

Moving Average Of Calculated Measure

Hi All,

 

I would like to calculate the moving average of an already calculated (% of grand total) measure. I've tried a lot of solutions but none of them seem to work correctly.

 

Table structure:

1. original table with dimensions and facts (simple DWH table)

2. calculated table with DAX

 

Date structure of 1st table: (of course, it's simplified...)

- date/time column

- period

- fact

 

Data structure of 2nd table:

- period (text): i.e. 2016_P1

- artificial date (date): i.e. 2016.04.01 (dynamic column to have a date column for the DAX date functions)

- calculated % of GT measure (it's a simple measure in the table)

- moving average of the calculated measure (it should be simple: = (value of 2016_P1 + 2016_P2 + 2016_P3) / 3

 

Any ideas how to solve it?

 

Thanks,

breki

 

  • Hi brekeke,

     

    Please try this formula.

    Measure 2 =
    VAR weekPeriods =
        CALCULATE (
            DISTINCTCOUNT ( tbl_periods_all[Period Column] ),
            DATESINPERIOD (
                'Calendar'[Date],
                MIN ( tbl_periods_all[Start Of Period] ),
                -21,
                DAY
            ),
            ALL ( tbl_periods_all[Period Column] )
        )
    VAR pPeriods =
        CALCULATE (
            DISTINCTCOUNT ( tbl_periods_all[Period Column] ),
            DATESINPERIOD (
                'Calendar'[Date],
                MIN ( 'tbl_periods_all'[Start Of Period] ),
                -7,
                MONTH
            ),
            ALL ( tbl_periods_all[Period Column] )
        )
    RETURN
        IF (
            MIN ( 'Period Selector'[period] ) = "Week",
            CALCULATE (
                SUM ( data[fact] ),
                DATESINPERIOD (
                    'Calendar'[Date],
                    MIN ( 'tbl_periods_all'[Start Of Period] ),
                    -21,
                    DAY
                ),
                ALL ( tbl_periods_all[Period Column] )
            )
                / weekPeriods,
            CALCULATE (
                SUM ( data[fact] ),
                DATESINPERIOD (
                    'Calendar'[Date],
                    MIN ( 'tbl_periods_all'[Start Of Period] ),
                    -7,
                    MONTH
                ),
                ALL ( tbl_periods_all[Period Column] )
            )
                / pPeriods
        )

    Moving_Average_Of_Calculated_Measure2

     

    Best Regards,

    Dale

     

12 Replies

    • brekeke's avatar
      brekeke
      Frequent Visitor

      Thanks for the advice. It might work but first I need to fully understand your solution. I'm fairly new into power BI and the logic is less than obvious to me sometimes...

      • v-jiascu-msft's avatar
        v-jiascu-msft
        Microsoft Employee

        Hi brekeke,

         

        It would be great if you can provide the DUMMY pbix file (Not your real data). I can add the solution to the file and send back to you. Then it could be easier for you to try the solution out.

         

        Best Regards,

        Dale

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft Employee

    Hi brekeke,

     

    Did you try the method of Greg_Deckler? You measure could be like this:

    New Measure =
    CALCULATE (
        SUMX (
            SUMMARIZE ( 'Sales', 'Product'[Color], "Amount", [Your old measure] ),
            [Amount]
        ),
        DATESINPERIOD ( 'Calender'[Date], MAX ( 'Calender'[Date] ), -3, MONTH )
    )

    It would be great if you can share your pbix file. Dummy one is enough.

     

    Best Regards,

    Dale

    • brekeke's avatar
      brekeke
      Frequent Visitor

      Thanks v-jiascu-msft,

       

      I quickly tried the below formula but it doesn't work. My assumption is that my data structure is making me fool.

       

      What I did not mention previously is that the period column is dynamically calculated: it can be changed with a slicer, thus it can show quarterly and weekly data as well.

       

      IMO, your formula doesn't work because it wrongly summarizing:

      1. measure is the original calculated measure

      2. measure is exactly your formula

       

      Values are different, so filtering for time interval works, but somehow it's grouping badly (according to my humble opinion)...

       

      Thanks