Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Rolling average on distinct count of doc nr

Dear all,

 

I have a list of order lines, and i would like to calculate the moving average (12 month) of orders that have been placed:

 

i have the following table:

DateDoc nrMonth

1-1-202000011
1-1-202000011
2-1-202000021
1-2-202000032
2-2-202000042
2-2-202000042
3-2-202000052
1-3-202000063
2-3-202000073
2-3-202000073
3-3-202000083
4-3-202000093
4-3-202000093

 

but somehow the following measure doesnt work:

12 month average =
CALCULATE(DISTINCTCOUNT('Table'[Doc nr]);DATESINPERIOD('Table'[Date];LASTDATE('Table'[Date]);-12;MONTH))
 
i tried to count the distinctcount of orders in the last twelve months. this gives me just the distinctcount of orders in that specific month.
 the requested results are in the last two columns in the table below
MonthDistinctcountDistinctcount last twelve monthsMoving average
1222,00
2352,50
3493,00
1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario.

Table:

c1.png

 

Calendar:

 

Calendar = CALENDARAUTO()

 

 

There is a many-to-one relationship between 'Table' and 'Calendar'.

 

You may create a calculated column and three measures as below.

 

Calculated column:
Monthnum = MONTH('Calendar'[Date])

Measure:
Distinctcount = DISTINCTCOUNT('Table'[Doc nr])

Distinctcount last 12 months = 
IF(
    NOT(ISBLANK([Distinctcount])),
    CALCULATE(
        DISTINCTCOUNT('Table'[Doc nr]),
        DATESINPERIOD(
            'Calendar'[Date],
            LASTDATE('Calendar'[Date]),
            -12,
            MONTH
        )
    )
)

Moving average = 
var monthnum = SELECTEDVALUE('Calendar'[Monthnum])
var _lastdistinct = 
CALCULATE(
    DISTINCTCOUNT('Table'[Doc nr]),
    FILTER(
        ALLSELECTED('Calendar'),
        'Calendar'[Monthnum] = monthnum-1
    )
)
return
IF(
    monthnum = 1,
    DIVIDE(
        [Distinctcount],
        1
    ),
    DIVIDE(
        [Distinctcount last 12 months],
        _lastdistinct
    )
)

 

 

Result:

c2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario.

Table:

c1.png

 

Calendar:

 

Calendar = CALENDARAUTO()

 

 

There is a many-to-one relationship between 'Table' and 'Calendar'.

 

You may create a calculated column and three measures as below.

 

Calculated column:
Monthnum = MONTH('Calendar'[Date])

Measure:
Distinctcount = DISTINCTCOUNT('Table'[Doc nr])

Distinctcount last 12 months = 
IF(
    NOT(ISBLANK([Distinctcount])),
    CALCULATE(
        DISTINCTCOUNT('Table'[Doc nr]),
        DATESINPERIOD(
            'Calendar'[Date],
            LASTDATE('Calendar'[Date]),
            -12,
            MONTH
        )
    )
)

Moving average = 
var monthnum = SELECTEDVALUE('Calendar'[Monthnum])
var _lastdistinct = 
CALCULATE(
    DISTINCTCOUNT('Table'[Doc nr]),
    FILTER(
        ALLSELECTED('Calendar'),
        'Calendar'[Monthnum] = monthnum-1
    )
)
return
IF(
    monthnum = 1,
    DIVIDE(
        [Distinctcount],
        1
    ),
    DIVIDE(
        [Distinctcount last 12 months],
        _lastdistinct
    )
)

 

 

Result:

c2.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

parry2k
Super User
Super User

@Anonymous for any time intelligence it is a best practice to add a calendar table and perform calculations from that. There are many posts that show you can add calendar/date dimensions in the model. 

 

I would 💖 Kudos 🙂 if my solution helped. If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Thanks, so in my actual example im using a calendar dim table, but in this example i left it out.

 

with or without, still facing the problem.. lets say you replace the date field from the fact table with the calendar table.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors