Forum Discussion

podma's avatar
podma
Regular Visitor
2 years ago
Solved

Quick measure: rolling average - ERROR Power BI-provided date hierarchy / primary date column

Dear all, my data model doesn't contain any valid date column. That's why I tried to convert an invalide column to a valid one using a formula that was proposed in this forum. At a first view, it se...
  • Greg_Deckler's avatar
    Greg_Deckler
    2 years ago

    podma In general, for Time Intelligence DAX functions to work you need to have Auto date/time intelligence turned on. You also need a separate Date table where you right-click the table and choose Mark as Date Table and then you set the date column that you want to use in your TI calculations to the primary date column for the date table.

     

    If you want to avoid all that nonsense, you can use Better Rolling Average. I linked the video but here is the Quick Measure Gallery entry: (2) Better Rolling Average - Microsoft Fabric Community

     

    The code is actually much shorter and simpler than what you get back from using the quick measure in Power BI Desktop. Below I'll try to adapt the code to your model using your measure as an example:

    Better Rolling Average = 
        VAR __EndDate = MAX('Demand + PVA'[ValidDate])
        VAR __3MonthsAgo = EOMONTH(__EndDate, -3)
        VAR __StartDate = DATE(YEAR(__3MonthsAgo), MONTH(__3MonthsAgo), 1)
        VAR __Table = 
            SUMMARIZE(
                FILTER(ALL('Demand + PVA'),[ValidDate]>=__StartDate && [ValidDate]<=__EndDate),
                'Demand + PVA'[Month],
                "__Value",SUM('Demand + PVA'[Qty])
            )
    RETURN
        AVERAGEX(__Table,[__Value])

    You may have to add a column called "Month" using MONTH([ValidDate])