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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Syndicate_Admin
Administrator
Administrator

Cumulative Sum by Line

Hello, we have a table where all the movements are by item/date/types of movement and we need to calculate the accumulated stock to later calculate the average cost.

This is the formula we have:

Ending Balance2 =

VAR DateEnd=MAX(Movements[DateIssue])

Total VAR= CALCULATE(sum(Movements[Mov Existence]),FILTER(ALLSELECTED(Movements[IssueDate]),Movements[IssueDate]<=Enddate))

RETURN total

But the accumulated is being given to us by type of movement, how could the formula be modified so that the total is taken out for each line without considering the movement?

An index cannot be included in the table as such because there are many items and they have different movements on different dates.

OGARCIAMX_0-1724265324805.png

Thank you very much in advance

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Syndicate_Admin 

Please try the following Dax:

PreviousMonthOpeningBalance =
VAR PreviousMonthBalance = 
    CALCULATE(
        [WarehouseBalance], 
        FILTER(
            ALL(Calendar), 
            Calendar[Date] < MIN(Calendar[Date])
        )
    )
RETURN PreviousMonthBalance

 

EndingBalance222 =
VAR _index = SELECTEDVALUE(Movements[Last Change])
VAR _artical = SELECTEDVALUE(Movements[Item])
VAR PreviousMonthBalance = [PreviousMonthOpeningBalance]
VAR Total = 
    CALCULATE(
        [WarehouseBalance], 
        FILTER(
            ALL(Movements), 
            Movements[Last Change] <= _index && Movements[Item] = _artical
        )
    )
RETURN 
    IF(
        _index = MINX(ALL(Movements), Movements[Last Change]), 
        PreviousMonthBalance + Total, 
        Total
    )


If the above dax can't help you solve your problem, can you provide detailed sample pbix file and the results you expect.So that I can help you better. Please remove any sensitive data in advance.

 

 

 

 

 

 

 

 

Best Regards,

Jayleny

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
Syndicate_Admin
Administrator
Administrator

Hey, hey, hey, it's good.

Thank you very much, from which option can I attach the PBIX file?

Anonymous
Not applicable

Hi @Syndicate_Admin 

Please provide sample data that fully covers your issue(in the form of pbix file or table) and the expected outcome based on the sample data you provided.So that I can help you better. Please remove any sensitive data in advance.

 

 

 

 

 

 

Best Regards,

Jayleny

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

ArticleMovLastChangeMov ExistenceIDDateIssue
01 59302.1 Invoice04/06/2024 16:45-2018421404/06/2024
01 59302.1 Invoice06/06/2024 15:49-4018419506/06/2024
01 59305.3 Miscellaneous Input10/07/2024 14:364010546510/07/2024
01 59302.1 Invoice10/07/2024 14:38-6018517810/07/2024
01 59303.2 Import Entry30/07/2024 13:009802640330/07/2024
01 59305.2 Transfer30/07/2024 14:25-2010584930/07/2024
01 59305.2 Transfer30/07/2024 14:252010584930/07/2024
01 59303.2 Import Entry30/07/2024 15:3614002640530/07/2024
01 59302.1 Invoice02/08/2024 13:43-10018586402/08/2024
01 59302.1 Invoice05/08/2024 16:24-8018591705/08/2024
01 59302.1 Invoice06/08/2024 14:55-16018593406/08/2024
01 59305.2 Transfer06/08/2024 16:39-20010598106/08/2024
01 59305.2 Transfer06/08/2024 16:3920010598106/08/2024
01 59302.1 Invoice07/08/2024 15:00-20018596807/08/2024
02 00922.1 Invoice03/06/2024 09:08-36018412903/06/2024
02 00922.1 Invoice03/06/2024 09:08-64018412903/06/2024
02 00922.1 Invoice03/06/2024 13:14-6018412503/06/2024
02 00925.1 Material consumption04/06/2024 09:20-205.510489004/06/2024
02 00922.1 Invoice05/06/2024 15:07-150018417405/06/2024
02 00922.1 Invoice06/06/2024 13:57-60018425106/06/2024
02 00922.1 Invoice06/06/2024 14:27-100018424906/06/2024
02 00925.1 Material consumption07/06/2024 10:01-320.610497607/06/2024
02 00922.1 Invoice07/06/2024 10:57-164018431907/06/2024
02 00922.1 Invoice07/06/2024 11:04-200018432207/06/2024
02 00922.1 Invoice01/07/2024 09:17-100018488901/07/2024
02 00925.2 Transfer01/07/2024 14:43-2010532301/07/2024
02 00925.2 Transfer01/07/2024 14:43-10010532301/07/2024
02 00922.1 Invoice02/07/2024 11:03-200018493902/07/2024
02 00923.2 Import Entry02/07/2024 16:2356002632202/07/2024
02 00923.2 Import Entry02/07/2024 16:23154002632202/07/2024
02 00922.1 Invoice01/08/2024 11:31-100018579101/08/2024
02 00922.1 Invoice01/08/2024 14:08-80018579501/08/2024
02 00922.1 Invoice01/08/2024 14:13-100018581001/08/2024
02 00925.4 Miscellaneous Output01/08/2024 14:15-2010593701/08/2024
02 00922.1 Invoice02/08/2024 13:25-100018586102/08/2024
02 00922.1 Invoice05/08/2024 11:50-350018586905/08/2024
02 00922.1 Invoice05/08/2024 16:15-300018587705/08/2024
02 00923.2 Import Entry06/08/2024 11:5528002643206/08/2024
02 00923.2 Import Entry06/08/2024 11:5570002643206/08/2024
02 00922.1 Invoice06/08/2024 13:14-150018594006/08/2024
02 00925.2 Transfer06/08/2024 14:16-420010598005/08/2024
02 00925.2 Transfer06/08/2024 14:16420010598005/08/2024
02 00922.1 Invoice06/08/2024 15:03-10018590006/08/2024
02 00922.1 Invoice06/08/2024 15:10-200018591306/08/2024
02 00922.1 Invoice07/08/2024 13:18-200018596407/08/2024
02 00925.1 Material consumption14/08/2024 10:11-68510607514/08/2024
02 00925.1 Material consumption14/08/2024 15:10-14767.510610514/08/2024
02 00925.1 Material consumption14/08/2024 15:10-527.510610514/08/2024
02 00922.1 Invoice16/08/2024 11:46-150018620316/08/2024
02 00925.2 Transfer16/08/2024 17:04-2010616016/08/2024
02 00925.2 Transfer16/08/2024 17:042010616016/08/2024
02 00922.1 Invoice19/08/2024 14:15-40018624619/08/2024
02 00922.1 Invoice19/08/2024 14:43-4018624919/08/2024

Anonymous
Not applicable

Hi @Syndicate_Admin 

First, create an index column in power query:

vjialongymsft_0-1724379774871.png

 

Then, you can try the following dax to get the result you want:

Ending Balance2 = 
VAR _index = SELECTEDVALUE(Movements[Index])
VAR total= CALCULATE(SUM(Movements[Mov Existence]),FILTER(ALL(Movements),'Movements'[Index] <= _index))

RETURN total

 

Result:

vjialongymsft_1-1724379834975.png

 

If you want to categorize by article, you can try the following dax:

Ending Balance22 = 
VAR _index = SELECTEDVALUE(Movements[Index])
VAR _artical = SELECTEDVALUE(Movements[Article])
VAR total= CALCULATE(SUM(Movements[Mov Existence]),FILTER(ALL(Movements),'Movements'[Index] <= _index && 'Movements'[Article] = _artical))

RETURN total

vjialongymsft_2-1724380081398.png

 

 

 

 

 

 

 

 

Best Regards,

Jayleny

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you very much for your support, I took the information you sent as a basis and it worked 🙂 . I only made a modification in the formula of Ending Balance 22 so that it would take the value you selected in the segmenter.

OGARCIAMX_1-1724430543810.png

_Ending Balance222 =
VAR _index = SELECTEDVALUE(Movements[Last Change])
VAR _artical = SELECTEDVALUE(Movements[Item])
Total VAR= CALCULATE([WarehouseBalance],FILTER(ALL(Movements),Movements[Last Change] <= _index && Movements[Item] = _artical))
Total RETURN

An additional detail, as you can see in the image when selecting the month of July, the initial balance for that item is 160 (which is the end of the month of June).... with the formula _Ending Balance222, If the values are correct because it calculates over the entire table, the detail is that the process was very very slow.

The formula with which the Balance of the previous month is calculated is as follows:
Previous Month Opening Balance =
var inventory = CALCULATE([WarehouseBalance], FILTER(ALL(Calendar), Calendar[Date]<MIN(Calendar[Date])))
RETURN inventario
Will it be possible to put the result of this last formula as a constant value in the Ending Balance 222 formula, so that only the balance of the previous month is added in the first line and from the second line onwards it continues with the accumulated balance in a normal way?
Anonymous
Not applicable

Hi @Syndicate_Admin 

Please try the following Dax:

PreviousMonthOpeningBalance =
VAR PreviousMonthBalance = 
    CALCULATE(
        [WarehouseBalance], 
        FILTER(
            ALL(Calendar), 
            Calendar[Date] < MIN(Calendar[Date])
        )
    )
RETURN PreviousMonthBalance

 

EndingBalance222 =
VAR _index = SELECTEDVALUE(Movements[Last Change])
VAR _artical = SELECTEDVALUE(Movements[Item])
VAR PreviousMonthBalance = [PreviousMonthOpeningBalance]
VAR Total = 
    CALCULATE(
        [WarehouseBalance], 
        FILTER(
            ALL(Movements), 
            Movements[Last Change] <= _index && Movements[Item] = _artical
        )
    )
RETURN 
    IF(
        _index = MINX(ALL(Movements), Movements[Last Change]), 
        PreviousMonthBalance + Total, 
        Total
    )


If the above dax can't help you solve your problem, can you provide detailed sample pbix file and the results you expect.So that I can help you better. Please remove any sensitive data in advance.

 

 

 

 

 

 

 

 

Best Regards,

Jayleny

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Good day, thank you very much, if it worked for me. 👍

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.