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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Anonymous
Not applicable

Calculate average of values of first month for each group

Hi All,

 

I have provided sample data below and explained my requirement. Please let me know if and how this can be done on PowerBI.

Data has first 4 columns and I want to get the values in column 5 on Power BI

 

i.e. Avg Purchase value of month - Avg Purchase value of first month of each product

 

Is this possible? 

Product IDOrder MonthPurchase ValueAvg Purchase Value for monthIncr in Avg Purchase Value compared to first month of Purchase
AApr-19120130130-130=0
AApr-19140130130-130=0
AJun-19155145145-130=15
AJun-19135145145-130=15
AOct-19150150150-130=20
BMay-19555555-55=0
BSep-19757575-55=20
BDec-19656565-55=10

 

Thanks.

1 ACCEPTED SOLUTION

hi  @Anonymous 

For your case, you could use EARILER Function to create a calculate column as below:

https://docs.microsoft.com/en-us/dax/earlier-function-dax

Result = 
VAR _firstordermonth=CALCULATE(MIN('Table'[Order Month]),FILTER('Table','Table'[Product ID]=EARLIER('Table'[Product ID]))) 
return

CALCULATE(AVERAGE('Table'[Purchase Value]),FILTER('Table','Table'[Product ID]=EARLIER('Table'[Product ID])&&'Table'[Order Month]=EARLIER('Table'[Order Month])))-
CALCULATE(AVERAGE('Table'[Purchase Value]),FILTER('Table','Table'[Product ID]=EARLIER('Table'[Product ID])&&'Table'[Order Month]=_firstordermonth))

 

Result:

6.JPG

 

and here is sample pbix file, please try it.

 

Regards,

Lin

Community Support Team _ Lin
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

6 REPLIES 6
Anonymous
Not applicable

Do you need DAX or a calculation in Power Query? 'Cause to me it looks like you want Power Query to enrich the data. In Power Query this calculation is dead simple.

Best
D
Anonymous
Not applicable

Hi @Anonymous ,

 

Anything would be fine as long as it solves this. If Power Query is simple, could you please let me know how it can be done? Would be a big help. 

 

Thanks.

hi  @Anonymous 

For your case, you could use EARILER Function to create a calculate column as below:

https://docs.microsoft.com/en-us/dax/earlier-function-dax

Result = 
VAR _firstordermonth=CALCULATE(MIN('Table'[Order Month]),FILTER('Table','Table'[Product ID]=EARLIER('Table'[Product ID]))) 
return

CALCULATE(AVERAGE('Table'[Purchase Value]),FILTER('Table','Table'[Product ID]=EARLIER('Table'[Product ID])&&'Table'[Order Month]=EARLIER('Table'[Order Month])))-
CALCULATE(AVERAGE('Table'[Purchase Value]),FILTER('Table','Table'[Product ID]=EARLIER('Table'[Product ID])&&'Table'[Order Month]=_firstordermonth))

 

Result:

6.JPG

 

and here is sample pbix file, please try it.

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Here's the solution in Power Query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUcCwoUjAyMLQEcYwMQKSxgVKsDjZpE0xpr9I8uLSpKViRKQ5pY0xp/+QShG4DKAmSdgJL+yZWwqTBZpuaIkkGpxbAJM1NIQRC0iU1GSZpZgohYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ProductID = _t, OrderMonth = _t, #"Purchase Value" = _t, #"Avg Purchase Value for month" = _t]),
    #"Set Correct Types on Columns" = Table.TransformColumnTypes(Source,{{"ProductID", type text}, {"OrderMonth", type date}, {"Purchase Value", Int64.Type}, {"Avg Purchase Value for month", Int64.Type}}),
    #"Calculate First Month Averages by Product" =
        let
            Source = #"Set Correct Types on Columns",
            #"Removed Columns" = Table.RemoveColumns(Source,{"Avg Purchase Value for month"}),
            #"Grouped Rows" = Table.Group(#"Removed Columns", {"ProductID", "OrderMonth"}, {{"MonthlyAvg", each List.Average([Purchase Value]), type number}}),
            #"Added Custom" = Table.AddColumn(
                #"Grouped Rows", "ShouldRetain",
                (r) =>
                    let
                        MinOrderMonth = List.Min(
                            Table.SelectRows(
                                #"Grouped Rows",
                                each [ProductID] = r[ProductID]
                            )[OrderMonth]
                        ),
                        Output = r[OrderMonth] = MinOrderMonth
                    in
                        Output
                ),
            #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([ShouldRetain] = true)),
            #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"OrderMonth", "ShouldRetain"})
        in
            #"Removed Columns1",
    #"Join Data To First Month Averages" =
        Table.Join(
            #"Set Correct Types on Columns", "ProductID", #"Calculate First Month Averages by Product", "ProductID", JoinKind.Inner
        ),
    #"Add Increase" = Table.AddColumn(#"Join Data To First Month Averages", "Increase", each [Avg Purchase Value for month] - [MonthlyAvg]),
    #"Changed Increase to Decimal" = Table.TransformColumnTypes(#"Add Increase",{{"Increase", type number}})
in
    #"Changed Increase to Decimal"

 

Just paste this in a blank query in the Advanced Editor and you'll see it happening step by step.

 

Best

D

TomMartens
Super User
Super User

Hey @Anonymous ,

 

for my solution, I created three measures.
This calculates the average of the first month:

 

first monthly average = 
AVERAGEX(
    VALUES('Table'[Product ID])
    , var _firstDate = 
        CALCULATE(
            FIRSTNONBLANK(
                'Calendar'[Date] 
                , CALCULATE(SUM('Table'[Purchase Value]))
            )
            , ALL('Calendar')
        )
    var currentmonthend = EOMONTH(_firstDate, 0)
    var currentmonthstart = EOMONTH(_firstDate , -1) + 1
    return  
    CALCULATE(
        SUM('Table'[Purchase Value])
        , DATESBETWEEN('Calendar'[Date] , currentmonthstart , currentmonthend)
    )
)

 

This calculated the average of the current month:

 

monthly average = 
var currentmonthend = EOMONTH(MAXX(VALUES('Calendar'[Date]), 'Calendar'[Date]), 0)
var currentmonthstart = EOMONTH(MAXX(VALUES('Calendar'[Date]), 'Calendar'[Date]), -1) + 1
return
AVERAGEX(
    VALUES('Table'[Product ID])
    , CALCULATE(
        SUM('Table'[Purchase Value])
        , DATESBETWEEN('Calendar'[Date] , currentmonthstart , currentmonthend)
    )
)

 

And the 3rd calculates the change:

 

change = [monthly average] - [first monthly average] 

 

This allows to create a table visual like this:
image.png
Please be aware that my solution is based on the usage of a Calendar table (it's always a good idea to use a dedicated calendar table).
I created a relationship between both tables, this makes it necessary that the Order Month column from your table is of the data type date.
The table visual is using the Month-Year column from the Calendar table. I used this very simple DAX statement to create a very simple Calendar table, there are much more sophisticated statements available, e.g. here: https://www.sqlbi.com/tools/dax-date-template/


As always if averages are used it's necessary to specify what has to happen, at the Total line

 

Hopefully this provides what you are looking for.

 

Regards,
Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Hi @TomMartens ,

 

Thank you for taking the time out to respond and explain in such detail. Will try this out and get back.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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
Top Kudoed Authors