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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
bibro5
Regular Visitor

Help with DAX for Running Total Subtractions

I'm working with two tables, **Inventory** and **Transactions**, and I'm trying to create a DAX measure that provides a running total of subtractions. Here's the scenario:

Inventory  
IDTypeQuantity
1apples5
2bananas3
3oranges13
4grapes7
transactions 
DATEIDQuantity sold
1/1/202522
1/3/202523
1/4/202511
1/5/202534

In the example, there are 5 bananas sold over the first two days. Since the inventory quantity is 3, I need to show 2 bananas sold on 1/1/2025 and 1 banana sold on 1/3/2025. The remaining transaction on 1/3/2025 should not be counted since there would have only been 3 bananas in inventory.

My goal is to stop counting item transactions once the inventory runs out. How can I write the DAX measures to achieve this?

Thanks in advance for your help!

 

1 ACCEPTED SOLUTION

Hello @bibro5 

To obtain the correct row totals, we modify the formulas as follows:

 

Modify the "Available Quantity" formula as follows:

Available Quantity =
IF ( [Total Quantity Sold] <> BLANK (), SUM ( Inventory[Quantity] ) )

Modify the "Running Quantity Sold" formula as follows:

Running Quantity Sold = 
VAR _remainingQty =
    CALCULATE (
        [Available Quantity] - [Total Quantity Sold],
        FILTER (
            ALL ( Transactions ),
            Transactions[ID] = MAX ( Transactions[ID] )
                && Transactions[DATE] <= MAX ( Transactions[DATE] )
        )
    )
VAR _runningQuntity =
    IF (
        _remainingQty >= 0,
        [Total Quantity Sold],
        [Total Quantity Sold] + _remainingQty
    )
VAR _handlingOversale =
    IF (
        _runningQuntity <= 0
            && [Total Quantity Sold] <> BLANK (),
        0,
        _runningQuntity
    )

RETURN
    IF(ISINSCOPE(Transactions[DATE]), _handlingOversale, [Available Quantity])

 

In the following animation, you can observe that we are achieving the desired results at both the total level and the individual ID/Type leve. While I understand you do not wish any slicer, please note that the formula is robust to handle it should you decide to utilize one in the future.

 

running_substraction.gif

 

I am also attaching the Power BI file for your reference.

 

I hope have answered your question correctly. However, if there is still any doubt, please do not hesitate to let me know.

 

Best Regards,
Udit

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
Visit My Linktree: LinkTree

Proud to be a Super User

View solution in original post

3 REPLIES 3
bibro5
Regular Visitor

@quantumudit thank you with your reply and you are correct. I could have been more descriptive on what I wanted the outcome to be.

 

In my example, I don't want to use a slicer for each individual item. I want it to look like my screenshot. The only issue is the total for running quantity sold is greater than available quantity. can you help me fix this?

bibro5_0-1740350201789.png

 

Hello @bibro5 

To obtain the correct row totals, we modify the formulas as follows:

 

Modify the "Available Quantity" formula as follows:

Available Quantity =
IF ( [Total Quantity Sold] <> BLANK (), SUM ( Inventory[Quantity] ) )

Modify the "Running Quantity Sold" formula as follows:

Running Quantity Sold = 
VAR _remainingQty =
    CALCULATE (
        [Available Quantity] - [Total Quantity Sold],
        FILTER (
            ALL ( Transactions ),
            Transactions[ID] = MAX ( Transactions[ID] )
                && Transactions[DATE] <= MAX ( Transactions[DATE] )
        )
    )
VAR _runningQuntity =
    IF (
        _remainingQty >= 0,
        [Total Quantity Sold],
        [Total Quantity Sold] + _remainingQty
    )
VAR _handlingOversale =
    IF (
        _runningQuntity <= 0
            && [Total Quantity Sold] <> BLANK (),
        0,
        _runningQuntity
    )

RETURN
    IF(ISINSCOPE(Transactions[DATE]), _handlingOversale, [Available Quantity])

 

In the following animation, you can observe that we are achieving the desired results at both the total level and the individual ID/Type leve. While I understand you do not wish any slicer, please note that the formula is robust to handle it should you decide to utilize one in the future.

 

running_substraction.gif

 

I am also attaching the Power BI file for your reference.

 

I hope have answered your question correctly. However, if there is still any doubt, please do not hesitate to let me know.

 

Best Regards,
Udit

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
Visit My Linktree: LinkTree

Proud to be a Super User

quantumudit
Super User
Super User

Hello @bibro5 

Thank you for providing the sample data. It would have been beneficial to increase the number of rows in the "Transactions" table and include an expected outcome based that data. Nevertheless, I added additional data to the "Transactions" table to validate edge cases.

 

The "Transaction" table I created is as follows:

DATE ID Quantity sold
2025-01-01 2 2
2025-01-03 2 3
2025-01-04 1 1
2025-01-05 3 4
2025-01-06 3 5
2025-01-07 3 3
2025-01-08 4 7
2025-01-09 4 3
2025-01-10 3 7
2025-01-11 1 2
2025-01-12 1 3
2025-01-13 1 4
2025-01-14 3 8
2025-01-15 3 3

 

The relationship between the 2 tables are as follows:

quantumudit_1-1740297642406.png

 

Here are the two initial simple measures I have developed:

 

To calculate the total quantity sold from the "Transactions" table:

Total Quantity Sold =
SUM ( Transactions[Quantity sold] )

 

To calculate/reference the available quantity from the "Inventory" table: 

Available Quantity =
IF ( [Total Quantity Sold] <> BLANK (), MAX ( Inventory[Quantity] ) )

 

Here is the measure I have created that calculated the "Running Subtraction" of quantity based on their availability in the inventory:

Running Quantity Sold = 
VAR _remainingQty =
    CALCULATE (
        [Available Quantity] - [Total Quantity Sold],
        FILTER (
            ALL ( Transactions ),
            Transactions[ID] = MAX ( Transactions[ID] )
                && Transactions[DATE] <= MAX ( Transactions[DATE] )
        )
    )
VAR _runningQuntity =
    IF (
        _remainingQty >= 0,
        [Total Quantity Sold],
        [Total Quantity Sold] + _remainingQty
    )
VAR _handlingOversale =
    IF (
        _runningQuntity <= 0
            && [Total Quantity Sold] <> BLANK (),
        0,
        _runningQuntity
    )
RETURN
    _handlingOversale

Here is the screenshot of the measure along with the result:

quantumudit_0-1740297312014.png

 

As you see, for "oranges," on January 10th, the quantity sold is 7, but in Inventory, only 1 item remains. Therefore, it shows 1 instead of 7 (ignores the remaining 6). Additionally, if any sales for "oranges" are found afterward, since we have none in inventory it shows 0.

I am attaching the Power BI file for your reference. Please review the results and, and inform me if you have any further queries.

 

Best Regards,
Udit

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudo 👍

🚀 Let's Connect: LinkedIn || YouTube || Medium || GitHub
Visit My Linktree: LinkTree

Proud to be a Super User

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.

Top Kudoed Authors