Forum Discussion

Francisco1's avatar
Francisco1
Regular Visitor
4 years ago
Solved

SUMX, AVERAGEX and SUMMARIZE in the same calculation

Hello,

 

I'm trying to calculate the average of the total stock of some products over a period of time.

 

My data is a table with the sales of the products. Each row is a combination of date-warehouse-product. If a product in a warehouse in a day has sales and/or stock, there is a row in the table. If a product has no sales and no stock, there is no row in the table.

 

I want to see the global stock by warehouse and the same thing by product. And see that data filtered by periods of time.

 

It is wrong to sum the stocks because I would be adding from different days the same product from the same warehouse.

It is wrong to average the stocks because I would be averaging from different products and warehouses.

It must be a combination of sums and averages.

My attempt was:

Total_Stock_Avg = SUMX(ADDCOLUMNS(SUMMARIZE(Sales, Sales[Warehouse], Sales[Product]), "Stock", CALCULATE(AVERAGEX(Sales, Sales[Stock]))), [Stock])

 

For example, in warehouse "U" the average should be "83.75":

But my formula in Power BI results in "87.83".

I think the problem is that the AVERAGEX does not consider the days when a product has zero stock.

There is no date when the warehouse "U" had more than 87 units. So, it is impossible for the average to be "87.83".

 

The right concept is to sum the lines of each day and average those results.

Alternatively, one can average each combination of product-warehouse and then sum those results.

 

How can I do this?

 

Thanks in advance,

Francisco.

 

  • If you want an average over days, then that's what you need to average over.

    AVERAGEX ( VALUES ( dimDate[Date] ), CALCULATE ( SUM ( Sales[Stock] ) ) + 0 )

     Leave out the "+ 0" if you want to exclude days where the sum is blank.

2 Replies

  • If you want an average over days, then that's what you need to average over.

    AVERAGEX ( VALUES ( dimDate[Date] ), CALCULATE ( SUM ( Sales[Stock] ) ) + 0 )

     Leave out the "+ 0" if you want to exclude days where the sum is blank.

    • Francisco1's avatar
      Francisco1
      Regular Visitor

      Brilliant! That's precisely what I wanted and it's so simple. Thank you!