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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
MMPowerBI
Frequent Visitor

DAX: How to Multiply the Sum of a Column by the Maximum Value of Another Column

Hi all,

thanks in advance for your support.

I'm facing a complex calculation with DAX that I'm not able to solve and I need support.

I have a fact table of inventory movements composed as follows:

ProductCodeDateQuantityUnitCost
x01/01/2024105
x01/02/2024-56
x01/03/2024207
y01/01/2024302
y01/02/2024103
y01/03/2024-204

 

The quantity in stock of a product at a given date is determined as the sum of the Quantity column from the point of time until the selected date.

On the other hand, the value of the inventory is to be determined as the product of the quantity in stock on the date (defined as above) for the last valid UnitCost with respect to the selected date (obviously per product, not the last valid cost ever).

For example: if I want to see the stock as of 02/03/2024 I would expect this result:

ProductStock QuantityStock Value
x25175
y2080

 

Where:

StockValue of product x need to be multiplying the sum of the quantities in stock up to the desired date and the ultimate unit cost with respect to the selected date. In the case under analysis, the date of 02/03/2024 has been selected, there is no cost on this date because the last movement is the previous day so that is the cost to be considered to value the entire stock.

The same reasoning for the stock of product y.

 

I would need to write a measure that can calculate this stock value. The desire is to have a measure that works both if it is used in a PBI table that has the product as a row attribute but also as a total.

 

Can any of you give me some suggestions on how to do this?

Thank you so much in advance.

 

4 REPLIES 4
lbendlin
Super User
Super User

That seems to be highly confusing. I played with your sample data a bit and here is what I came up with instead.  Feel free to modify as needed 

 

lbendlin_0-1710524145063.png

 

 

Hi Ibendlin,

thank for reply.

The proposed solution is not the aspected one.

I probably didn't explain myself well.

let's consider the row dated 03/02/2024 in your example dataset.

in this case the quantity in stock is actually correct, it must be the sum of the quantities up to that moment (therefore 10-5=5). The value, however, must be calculated by multiplying the previously calculated quantity (therefore 5) with the last unit cost valid on that date (in this case the last valid cost for product x on the date of 02/03/2024 is the one that, in the original table I posted, refers to 02/01/2024 which is 6). So as a stock value on 02/03/2024 I would have expected to see 30 (5*6).
Furthermore, the calculation must be done per product because the unit value must be the latest for each product multiplied by the quantity in stock of the specific product).
On the total I would therefore expect to find the following value:
Stock value total = StockQtyX*LastUnitPriceX+StockQtyY*LastStockPriceY
Where:
- StockQtyX is the sum of the quantities of product
- LastUnitPriceX is the last unit price valid on the selected date (6 in the previous example)
Same considerations for Y.

 

I tried to sketch a solution like this:

- Measure: fx_StockUnitPrice = MIN('Movimenti'[Price])

- Measure: 

LastStockUnitPrice = var _SelectedDate = MAX('Calendario'[Data])

RETURN CALCULATE(LASTNONBLANKVALUE('Movimenti'[Data], 'Movimenti'[fx_StockUnitPrice]), 'Movimenti'[Data]<= _SelectedDate)
 
- Measure: 
StockValueBaseTable = var MainTable = SUMMARIZE('Movimenti', Movimenti[Prodotto], "LastStockUnitPrice", [LastStockUnitPrice], "Stock Qty", [Stock qty])
RETURN
SUMX(MainTable, [LastStockUnitPrice]*[Stock Qty])
 
Where table movimenti is the fact table i posted in the first post.
 
That solution works fine in a test case with few data.
When I implement that solution in the real case with over 50M data (There will be over 600M when fully operational) this solution goes in "exceded resource limits" of the powerBI report.
 
Is there a way to achieve the same result more efficiently?
 
Thanks in advance

Please provide sample data that fully covers your issue.
Please show the expected outcome based on the sample data you provided.

Hi, 

I have a fact table like that:

MMPowerBI_0-1711568627574.png

 

The aspected result are:

- If i select the date of 01/01/2024 on the report slicer:

The following result, boot by product and total:

MMPowerBI_1-1711568802194.png

- If i select the date of 02/02/2024 on the report slicer:

MMPowerBI_2-1711568865174.png

- If i select the date of 03/03/2024 on the report slicer:

MMPowerBI_3-1711568901704.png

- If i select the date of 04/04/2024 on the report slicer:

MMPowerBI_4-1711568955598.png

the reasoning of this last case, but the same applies to the previous ones is:
- The StockQty column must be the sum of the movements from the dawn of time until the selected date (04/04/2024). Which, for the product x is 100 (i.e. the sum of its movements up to the date: 100+20+30-50), for the product Y it is 50 (100-80+20+10).
- The LastStockUnitPrice column is, for each product x, the last unit cost valid on the date. For the product, the last valid cost is the one corresponding to the record of the original dataset as of 04/04/2024, i.e. 16, for the product Y 22. Obviously this measurement on the total line makes no sense.
- The StockValueBaseTable column is the product, per row, of the two previous values. This is the real value I want to get. On the total line, the result to be obtained is not the product of the two previous measurements on the total line, but rather the sum of the products by product.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.