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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply

Running Total With Reset

I have a problem regarding stock management, the product is a liquid.


At the beginning of the year there is a physical measurement and after that, through the year, is done for example weekly, not mandatory in this period.

This measurement is done because of any loss that might occur between the process of stocking purchased product and sold product.


The intention is after the weekly real measurement enters the running total of the stock should “reset” and ignore previous movements and accumulate the new ones after the date this should be done by store.

For the store I created a dimension to use as a filter in order to show the complete running total of both stores or for each individual one.

Is there any way to do this in DAX or Power Query? I tried to use Power Query with conditional subcategory running total without any luck. Since I am newbie probably did something wrong.

However I found Running totals by subcategory in Power Query by Curbal and this helped me to the subcategory but not with the occasional real time measurement that “resets” everything before that. Also this seems to be very heavy, maybe that’s why she says that we shouldn’t do running totals in Power Query.

What I did at the beginning was the following measure:

Quantity =

CALCULATE(

    SUM( Table1[Amount] ),

    FILTER(

        ALLSELECTED( Dates ),

        Dates[Date] <= MAX( Dates[Date] )

    )

)

and I used the date dimension table and the store dimension table to filter. However this is only global running total without reset

Tried a calculated column that I took from a post in the forum:


Running Total Measure =

VAR _rowDate = table1[Date]

 

VAR initialDate =

    CALCULATE(

        MAX( table1 [Date] ),

        FILTER(

            table1,

            table1 [Date] < _rowDate

                &&

            table1 [resetCode] = 0

        )

    )

 

RETURN

IF(

    table1 [resetCode] = 0,

    table1 [Amount],

    SUMX(

        FILTER(

            table1,

            table1 [Date] <= _rowDate

                &&

            table1 [Date] > initialDate

        ),

        table1 [Amount]

    )

)
but didn’t work, it didn't followed the time. On my if statment table1 [Quantity] with a resetCode 0 is the quantity to reset at that moment. Is a new real measurement and after that it should start a new running total treating 00_realQuantity as a initial stock from that moment on.

 

1.PNG


DateStoreType of movementAmountRunning TotalReset Code
01/01/2020A00_initialStock5005000
01/01/2020B00_initialStock3003000
01/01/2020Bsale-1002001
02/01/2020Bpurchase503502
02/01/2020Bpurchase403902
03/01/2020Asale-804201
04/01/2020Bsale-703201
05/01/2020Asale-204001
06/01/2020Apurchase504502
07/01/2020A00_realQuantity3503500
07/01/2020B00_realQuantity3003000
07/01/2020Apurchase504002
08/01/2020Bpurchase203202
 

 


This in Excel would be easier but I am transitioning to Power Bi  and maybe I am experimenting unnecessary steps hope that I can find help.

Thank you in advance.

1 ACCEPTED SOLUTION

3 REPLIES 3

Alberto Ferrari from SQLBi answered this on the following vídeo:

Implementing running total from arbitrary dates in DAX - Unplugged #34 - YouTube

amitchandak
Super User
Super User

@FilipeSantos_pt , not very clear. But if it always reset at year start then you can use YTD

 

example

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

I know it's confusing. The stock doesn't reset only at the  year start. Through the year new measurements are made and the stock at that moment is corrected to the real one.

The running total at this moment should stop and restart treating this entry 00_realMeasurement as a new initial stock. I corrected the initial post a bit. I hope it's more clear

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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