Forum Discussion

Syndicate_Admin's avatar
Syndicate_Admin
Administrator
2 years ago
Solved

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.

Thank you very much in advance

  • Anonymous's avatar
    Anonymous
    1 year ago

    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.

7 Replies

  • Anonymous's avatar
    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.

    • Syndicate_Admin's avatar
      Syndicate_Admin
      Administrator
      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's avatar
        Anonymous
        Not applicable

        Hi Syndicate_Admin 

        First, create an index column in power query:

         

        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:

         

        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

         

         

         

         

         

         

         

         

        Best Regards,

        Jayleny

         

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

  • Hey, hey, hey, it's good.

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