Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hello Everyone,
I have a simple table that has Quantity and Stock. The goal is to know how many products are being consumed on a daily basis.
Here is the sample data table and desired result. Wondering how I can execute the desired result into a measure or calculated column using DAX or Power Query perhaps.
Essentially this is how it was being computed in Excel.
And below is the result it was giving.
LOB | Warehouse | Product | Date | Quantity | Stock | Result |
A | Target | ABD-1155124 | 13/04/2023 | 3 | 11 | 8 |
B | Target | ABD-1155124 | 27/01/2023 | 1 | 11 | 7 |
B | Target | ABD-1155124 | 05/04/2023 | 1 | 11 | 6 |
B | Target | ABD-1155124 | 11/04/2023 | 1 | 11 | 5 |
B | Target | ABD-1155124 | 27/04/2023 | 1 | 11 | 4 |
B | Target | ABD-1155124 | 22/05/2023 | 1 | 11 | 3 |
B | Target | ABD-1155124 | 19/06/2023 | 1 | 11 | 2 |
B | Target | ABD-1155124 | 04/07/2023 | 1 | 11 | 1 |
B | Target | ABD-1155124 | 19/07/2023 | 1 | 11 | 0 |
B | Target | ABD-1155124 | 20/07/2023 | 1 | 11 | -1 |
B | Target | ABD-1155124 | 01/08/2023 | 1 | 11 | -2 |
Solved! Go to Solution.
@v_mark
Please refer to attached sample file. See how it works with your real data and return back with your comments.
Result =
VAR Stock = 'Table'[Stock]
VAR CurrentLOB = 'Table'[LOB]
VAR CurrentDate = 'Table'[Date]
VAR CurrentWarehoursProductTable =
CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Warehouse], 'Table'[Product] ) )
VAR TableBefore =
FILTER ( CurrentWarehoursProductTable, 'Table'[LOB] < CurrentLOB )
VAR TableOnAndBefore =
FILTER ( CurrentWarehoursProductTable, 'Table'[Date] <= CurrentDate && 'Table'[LOB] = CurrentLOB )
VAR QuantityBefore =
SUMX ( TableBefore, 'Table'[Quantity] )
VAR QuantityOnAndBefore =
SUMX ( TableOnAndBefore, 'Table'[Quantity] )
RETURN
StocK - QuantityBefore - QuantityOnAndBefore
Hi @v_mark
Can you update the sample including the date and the product? Are you trying to create a calculated column or a measure?
@tamerj1 - Sure I updated the original post. They have the same product all across and Warehouse.
The difference they have is based in Dates and Line of business (LOB).
@v_mark
Why the quantity of LOB A which is (3) is subtracted twice? I mean shouldn't the result of the 2nd row be 7? What is the logic behind your expected result?
@tamerj1 'm sorry that is a mistake on my end. As far creating measure or calculated column either way will work I guess. The idea is to drag it over to a table in the report.
You are right.. Ideally it should be 7 . Please see details below.
I will update my post
@v_mark
Please refer to attached sample file. See how it works with your real data and return back with your comments.
Result =
VAR Stock = 'Table'[Stock]
VAR CurrentLOB = 'Table'[LOB]
VAR CurrentDate = 'Table'[Date]
VAR CurrentWarehoursProductTable =
CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Warehouse], 'Table'[Product] ) )
VAR TableBefore =
FILTER ( CurrentWarehoursProductTable, 'Table'[LOB] < CurrentLOB )
VAR TableOnAndBefore =
FILTER ( CurrentWarehoursProductTable, 'Table'[Date] <= CurrentDate && 'Table'[LOB] = CurrentLOB )
VAR QuantityBefore =
SUMX ( TableBefore, 'Table'[Quantity] )
VAR QuantityOnAndBefore =
SUMX ( TableOnAndBefore, 'Table'[Quantity] )
RETURN
StocK - QuantityBefore - QuantityOnAndBefore
User | Count |
---|---|
25 | |
12 | |
8 | |
6 | |
6 |
User | Count |
---|---|
26 | |
12 | |
12 | |
10 | |
6 |