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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
BJE
Helper I
Helper I

Creating a conditional progressive average based on a date value

I have created a date based table which looks like this:

 

YearMonth Start DateMonth End DateYear YTD Start DateYear YTD End DateFTE
2018-191/07/201831/07/20181/07/201831/07/2018473.6
2018-191/08/201831/08/20181/07/201831/08/2018474.6
2018-191/09/201830/09/20181/07/201830/09/2018478.3
2018-191/10/201831/10/20181/07/201831/10/2018487.1
2018-191/11/201830/11/20181/07/201830/11/2018492.1
2018-191/12/201831/12/20181/07/201831/12/2018493.1
2018-191/01/201931/01/20191/07/201831/01/2019497.7
2018-191/02/201928/02/20191/07/201828/02/2019508
2018-191/03/201931/03/20191/07/201831/03/2019512.4
2018-191/04/201930/04/20191/07/201830/04/2019514.4
2018-191/05/201931/05/20191/07/201831/05/2019518.4
2018-191/06/201930/06/20191/07/201830/06/2019521.4
2019-201/07/201931/07/20191/07/201931/07/2019527.4
2019-201/08/201931/08/20191/07/201931/08/2019531.8
2019-201/09/201930/09/20191/07/201930/09/2019535.8
2019-201/10/201931/10/20191/07/201931/10/2019541.2
2019-201/11/201930/11/20191/07/201930/11/2019548.2
2019-201/12/201931/12/20191/07/201931/12/2019551.9
2019-201/01/202031/01/20201/07/201931/01/2020557.9

 

I want to be able to calculate a conditional progressive average for all values in a given year (i.e. YTD average). For instance for the year 2018-19, for July 2018 the result should be 473.6, for August 2018 the result should be 474.1 (being the average of July and August), for September 2018 thes result should be 475.5 (being the average of July, August and September) and so on. When it gets to 2019-20, the average calculations should start again.

 

Advice on working through this as an added column greatly appreciated.

1 ACCEPTED SOLUTION

Thanks - have resolved my issue using grouping of data and then using both the index and year columns to calculate a filtered sum function, divided by index number which works.

View solution in original post

5 REPLIES 5
ziying35
Impactful Individual
Impactful Individual

Hi,@BJE

let
    Source = Table.FromRecords(Json.Document(Binary.Decompress(Binary.FromText("rdW9bsIwFAXgV4k8g/FvfM0M3TqVBZUOkYrUCSSUreq7FxKX2FZjc7HnYx19OST4/Zvsj92FrIlgHJbckgV5PZ/6r+at7y59s+n64zXkhxUzh9XtzP3A9vT5F8swvzU2+90m1XE/M1/zstuStTKStj+Lx5mQYUIdJsRMhWPaBJMF+dPMsMYxgUoEk7P0ml5esqZXMzLBUI5h8vSaXl6yplczMq3AMUVmTVFnTREzJYrJxse0s5/QlBd9QlONYxpqMEwxzxQQ5M8yo5qBqRlgkDKzpayzpYyQXFCFYaoEkwV50d+RipkKx9SZNXWdNXXMBByzzazZ1lmzjZiCx0y7FCx3o8+vaR5m2tSaJmYaHBMyTKjDhIgpOQUM02Z+dFvODGscU6OY7qqdXdPLS9b0akam4lRgmKk7iAV5yZpejWMCjpm4g2SYF60Z30GaU4t5N4fH9A78exUPedEnNNU4prkyP34B",BinaryEncoding.Base64),Compression.Deflate))),
    rows = Table.ToRows(Source),
    acc = List.Accumulate(
              rows,
              {"", "", 0, {}},  //Year/counter/running total/output
              (s,c)=>
                  if c{0}<>s{0} then {c{0}, 2, c{5}, s{3}&{c&{c{5}}}} 
                  else {c{0}, s{1}+1, c{5}+s{2}, s{3}&{c&{(c{5}+s{2})/s{1}}}}
           ){3},
    tbl = Table.FromRows(acc, Table.ColumnNames(Source)&{"Avg"})
in
    tbl

 

If you don't have a lot of data, try my code above.

If my code solves your problem, please mark it as a solution

Thanks - have resolved my issue using grouping of data and then using both the index and year columns to calculate a filtered sum function, divided by index number which works.

Hi @BJE ,

 

Have you resolved the problem? You could accept any reply to close this topic.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
lbendlin
Super User
Super User

In DAX you should be able to use AVERAGEX() with ALLEXCEPT(year). Or did you want this in M ?

Yes, I need this in M.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors