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

Join 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.

Reply
PatrickS-DK
Regular Visitor

DAX Running total in Direct Query, but reset to zero each day, if running below zero

Hi Power BI Community,

I have an issue, where I want to calculate a running total in a measure, so I'm able to get the primo and ultimo value of the stock quantity. This is a forecast of the stcok 14 days ahead of time, and to be sure that we won't run below zero on a given day. In the event of the running total going below zero, I need it to reset and use the 0 as the new starting point.


Backstory

I need to use Direct Query as the data source connection. I have therefore limitations that I cannot use a calculated column, since I cannot use CALCULATE in calculated columns due to Direct Query. I have no possibility to throw the calculation out of DAX and Measures and into Power Query, again due to Direct Query limitations. I don't have a possibility to put the calculation outside of DAX, so I need to find a solution within measures, that I can use.

 

Where I'm at right now

My data looks a bit like this:

ItemDatePrimoSalesPurchaseUltimo
116-08-20241.4297710658
117-08-202465811360-1136
118-08-202404384380
119-08-20243805940-594
120-08-202406300-630
121-08-202406590-659
122-08-202408210-821
123-08-202401670-167
124-08-2024040-4
125-08-2024030-3
126-08-20240000
127-08-20240000
128-08-20240000
129-08-20240000

 

I have tried to put everything into my ultimo calculation, but I can also see that that is the source of the error.

 

I have 3 measures that's just a sum of the data in the table:

 

 

 

WarehouseflowSales = SUM(warehouseflow[sales])
WarehouseflowPurchase = SUM(warehouseflow[purchase])
WarehouseflowInventory = SUM(warehouseflow[inventory])

 

 

 


Then I have the measure for the Ultimo column:

 

 

 

WarehouseflowUltimo =
VAR MaxDate = MAX(warehouseflow[date])

VAR UltimoYesterday =
    CALCULATE(
        -[WarehouseflowSales] + [WarehouseflowPurchase],
        ALLEXCEPT(warehouseflow, warehouseflow[itemno]),
        warehouseflow[date] < MaxDate
    )
    +
    [WarehouseflowInventory]

RETURN

    IF(
        UltimoYesterday < 0,
        0,
        UltimoYesterday + [WarehouseflowPurchase] - [WarehouseflowSales]
    )

 

 

 

 

The measure for the Primo column is just the ultimo value from the day before:

 

 

 

WarehouseflowPrimo =

VAR MaxDate = MAX(warehouseflow[date])

VAR UltimoYesterday =
    CALCULATE(
        [WarehouseflowUltimo],
        ALLEXCEPT(warehouseflow, warehouseflow[itemno]),
        warehouseflow[date] = MaxDate - 1
    )

RETURN

    IF(
        MaxDate = TODAY(),
        [LagerflowInventory],
        IF(
            UltimoYesterday < 0,
            0,
            UltimoYesterday
        )
    )

 

 

 

 

I have tried to edit the measure to use a somewhat circular reference, but whereas I handle the calculation for the WarehouseflowUltimo measure by date. Meaning that if the the date is today, then I should add the WarehouseflowPurchase and subtract the WarehouseflowSales to the WarehouseflowInventory. If it is not today, then I should use the WarehouseflowPrimo instead of the WarehouseflowInventory.

 

Do you have any suggestions as to how I can handle this?

 

Possible solution I can see myself

I have thought of a solution to create 14 different measures, since I need to handle 14 days within my forecast. Then I should be able to handle the issue day by day. I'm just a bit tied on hands and legs due to performance on this issue as well. I haven't tried it yet, but would like to hear your suggestions first 😊

2 ACCEPTED SOLUTIONS

Spoiler alert: DAX does not support conditional accumulations.  You need to use Power Query's List.Accumulate for that.

View solution in original post

That's what I figured. List.Accumulate is not a possibility, due to Direct Query mode. I need the data to be as close to real time as possible, and we therefore went for Direct Query mode. It has a lot of limitations in the Power Query functionality and also in terms of calculated columns.

 

We'll try to see if we can do something manually to handle these steps instead. Although it would've been awesome to handle it in DAX.

View solution in original post

5 REPLIES 5
PatrickS-DK
Regular Visitor

Thank you for your try @Rupak_bi.

I have tried to implement it, but in another scenario I get another error.

I have tried to create an Excel-spreadsheet and a PBIX file ontop of this Excel-spreadsheet, to have a couple of different scenarios that could happen 🙂

 

Right now it seems like if the previous day is greater than 0 and the next day should be as well, then it doesn't get the closing stock from the previous day.

 

PatrickSDK_0-1724140783996.png

 

The underlying dataset you can put into Power Query with this statement:

let
    Source = #table(type table[Item = text, Date = date, Inventory = number, Sales = number, Purchase = number], {{"1",45523,1429,771,0}, {"1",45524,0,1136,0}, {"1",45525,0,4,384}, {"1",45526,0,594,0}, {"1",45527,0,630,0}, {"1",45528,0,659,0}, {"1",45529,0,821,0}, {"1",45530,0,167,0}, {"1",45531,0,4,0}, {"1",45532,0,3,0}, {"1",45533,0,0,0}, {"1",45534,0,0,0}, {"1",45535,0,0,0}, {"1",45536,0,0,0}, {"2",45523,500,400,0}, {"2",45524,0,250,500}, {"2",45525,0,200,0}, {"2",45526,0,400,0}, {"2",45527,0,300,0}, {"2",45528,0,200,0}, {"2",45529,0,100,700}, {"2",45530,0,250,0}, {"2",45531,0,350,0}, {"2",45532,0,0,200}, {"2",45533,0,350,0}, {"2",45534,0,0,0}, {"2",45535,0,0,0}, {"2",45536,0,0,0}, {"3",45523,500,600,0}, {"3",45524,0,100,250}, {"3",45525,0,500,300}, {"3",45526,0,200,500}, {"3",45527,0,750,0}, {"3",45528,0,250,0}, {"3",45529,0,300,0}, {"3",45530,0,150,0}, {"3",45531,0,100,0}, {"3",45532,0,50,2500}, {"3",45533,0,650,0}, {"3",45534,0,750,0}, {"3",45535,0,300,0}, {"3",45536,0,0,0}}),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
in
    #"Changed Type"

 

The Measure for Primo (based of the one you provided), is here:

Primo = 
var min_date = calculate(min('Data'[Date]),all(Data[Date]))
var prev = calculate(sum(Data[Purchase])-sum(Data[Sales]) + SUM(Data[Inventory]),all(Data[Date]),PREVIOUSDAY(Data[Date]))
var prev1= SUM(Data[Inventory]) + prev

RETURN

    max(0,prev1)

Spoiler alert: DAX does not support conditional accumulations.  You need to use Power Query's List.Accumulate for that.

That's what I figured. List.Accumulate is not a possibility, due to Direct Query mode. I need the data to be as close to real time as possible, and we therefore went for Direct Query mode. It has a lot of limitations in the Power Query functionality and also in terms of calculated columns.

 

We'll try to see if we can do something manually to handle these steps instead. Although it would've been awesome to handle it in DAX.

Rupak_bi
Solution Sage
Solution Sage

Hi,

Here is My solution. Please let me know if this works.

Rupak_bi_0-1723882958716.png

I have created two measures as follows

Opening Stock =
var min_date = calculate(min('Table'[Date]),all('Table'[Date]))
var prev = calculate(sum('Table'[Purchase])-sum('Table'[Sales]),all('Table'),PREVIOUSDAY('Table'[Date]))
var prev1= if(max('Table'[Date])=min_date+1,prev+1429,prev)

RETURN
max(0,prev1)
 
Closing Stock =
Var min_date = calculate(min('Table'[Date]),all('Table'[Date]))
var closing = [Opening Stock]+sum('Table'[Purchase])-sum('Table'[Sales])

RETURN
if(max('Table'[Date])=min_date,1429+closing,closing)





Regards
Rupak
FOLLOW ME : https://www.linkedin.com/in/rupaksar/

Hello,

I see your post 

Opening Stock Opening Stock =
VAR min_date = CALCULATE(MIN('Table'[Date]), ALL('Table'[Date]))
VAR prev = CALCULATE(SUM('Table'[Purchase]) - SUM('Table'[Sales]), ALL('Table'), PREVIOUSDAY('Table'[Date]))
VAR prev1 = IF(MAX('Table'[Date]) = min_date + 1, prev + 1429, prev)
RETURN MAX(0, prev1) 
Closing Stock

Closing Stock =
VAR min_date = CALCULATE(MIN('Table'[Date]), ALL('Table'[Date]))
VAR closing = [Opening Stock] + SUM('Table'[Purchase]) - SUM('Table'[Sales])
RETURN IF(MAX('Table'[Date]) = min_date, 1429 + closing, closing) These versions should be more streamlined while maintaining the same logic. VanillaGiftCardBalance

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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