Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
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
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
10 | |
9 | |
9 | |
8 | |
8 |
User | Count |
---|---|
14 | |
12 | |
11 | |
11 | |
8 |