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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
carmae
Regular Visitor

Future inventory level

Hi everyone.

I need to create a report in PowerBI that shows the future evolution of the inventory level of a specific item, given the initial stock, forecasted sales, and planned purchase orders.

For simplicity, let's assume I have only one item to manage. I have:

  • A table with the initial stock as of today.
  • A table with the forecasted sales quantities for each future day.
  • A table with the quantities of incoming material from suppliers for each future day.

I combined these tables, treating the initial stock and purchases as positive movements and sales as negative movements.

The final table looks like this:

Date Item Movement Type Quantity

21/08/2024Item1Initial Stock10
22/08/2024Item1Forecasted Sales-3
23/08/2024Item1Forecasted Sales-4
24/08/2024Item1Purchase5
25/08/2024Item1Forecasted Sales-2
26/08/2024Item1Forecasted Sales-4
27/08/2024Item1Forecasted Sales-3
28/08/2024Item1Forecasted Sales-4
29/08/2024Item1Forecasted Sales-6
30/08/2024Item1Forecasted Sales-2
31/08/2024Item1Purchase20
01/09/2024Item1Forecasted Sales-2
02/09/2024Item1Forecasted Sales-2
03/09/2024Item1Forecasted Sales-3
04/09/2024Item1Purchase5
05/09/2024Item1Forecasted Sales-5
06/09/2024Item1Forecasted Sales-3
07/09/2024Item1Forecasted Sales-2
08/09/2024Item1Forecasted Sales-5
09/09/2024Item1Forecasted Sales-1

The simplest solution is to sum the quantity column day by day. The problem, as you might guess, occurs starting from 27/08/2024, when the forecasted stock goes negative because the sum of the negative movements up to that date exceeds the sum of the positive movements.

At this point, my requirement is to show a stock of zero and not a negative value. This is straightforward because it only requires a simple IF statement.

The problem I am unable to solve arises when the stock becomes positive again due to an incoming supply. Specifically, on 31/08/2024, the stock returns to 20 units. The next day it will be 18 (20-2), the day after that it will be 16 (20-2-2), then 13 (20-2-2-3), and so on.

Unfortunately, the calculated measure, as mentioned before, sums the theoretical sales on the days when the actual stock was zero, when it should instead ignore them.

Any suggestions on how I could solve this in DAX?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi, @carmae 

Yes, if you want to group and accumulate data, you only need to go one more step to group by Item.

vyaningymsft_0-1724742515408.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fdIxDsMgDIXhuzCnAmxCyBF6hihjx269v8ryiBKePaJPWGD9xxHev883hyWkFiVHSVL6IadwLncT2Esn02FlsgJbJ1rHNZmsOiM35ynNubcPq0/TZL9Fx1rksZY9pmzd6yaOqfWHbtbKOl0rm606IzfnKc2Zea0sDxOnFhirBcZqgZGvg1gtMFYLjNUCY7XAWC3i1AJjtYhTC4zVAmO1wKyVGbXAWC0wVguM1QK71XL+AQ==", BinaryEncoding.Base64), Compression.Deflate)), 
    let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, #"Date Item" = _t, Quantity = _t]),
    
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Date Item", type date}, {"Quantity", Int64.Type}}),
    FX = (values as list) as list =>
        let
            GRTList = List.Generate(
                () => [GRT = values{0}, i = 0],
                each [i] < List.Count(values),
                each 
                    let
                        nextGRT = if [GRT] > 0 then [GRT] + values{[i] + 1} else values{[i] + 1}
                    in
                        [GRT = nextGRT, i = [i] + 1],
                each [GRT]
            )
        in
            GRTList,
 
    // Group by Item and apply transformations
    Grouped = Table.Group(
        #"Changed Type",
        {"Item"},
        {
            {"Transformed", each 
                let
                    dateitemlist = List.Buffer([Date Item]),
                    quantitylist = List.Buffer([#"Quantity"]),
                    result = Table.FromColumns(
                        {dateitemlist, quantitylist, List.Transform(FX(quantitylist), each if _ < 0 then 0 else _)},
                        {"Date Item", "Quantity", "Output"}
                    )
                in
                    result
            }
        }
    ),
    #"Expanded Transformed" = Table.ExpandTableColumn(Grouped, "Transformed", {"Date Item", "Quantity", "Output"}, {"Date Item", "Quantity", "Output"})
in
    #"Expanded Transformed"

 

Best Regards,
Yang

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

8 REPLIES 8
gregorschuerer
New Member

I'm stuck with exactly the same problem. @carmae have you ever figured out how to do this in DAX? Thanks a lot in advance

carmae
Regular Visitor

Seems to work like magic! Unfortunately I'm not that good in M so I should study this piece of code.
Would you be so kind to help me go further? Since I will do this for more than one item how should I modify this to work with several items.
As an example I think you could add a column "Item code", duplicate rows and assign "Item1" to half of them and "Item2" to the remaining. 

Thanks again, you're helping me a lot.

C.

Anonymous
Not applicable

Hi, @carmae 

Thanks for Irwan reply. You can try the following M language in Power Query.

vyaningymsft_0-1724312640007.png

 

let
  Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc/LCcAwDAPQXXxOiT/5zlKy/xoNtFEg9fUhI+u+iVtUicqaKJAwjfCaLrsMZrAES8syKCOmsOKcVqeiObkOK8uM/x2GGfrN6JHlzE1Tx+z8Zdo5bdKetq04p9WpaM7tniY0xgM=", BinaryEncoding.Base64), Compression.Deflate)), 
  let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Date Item" = _t, Quantity = _t]),
  #"Changed Type" = Table.TransformColumnTypes(
    Source,
    {{"Date Item", Int64.Type}, {"Quantity", Int64.Type}}
  ),
  FX = (values as list) as list =>
    let
      GRTList = List.Generate(
        () => [GRT = values{0}, i = 0],
        each [i] < List.Count(values),
        each 
            let
                nextGRT = if [GRT] > 0 then [GRT] + values{[i] + 1} else values{[i] + 1}
            in
                [GRT = nextGRT, i = [i] + 1],
        each [GRT]
      )
    in
      GRTList,
  dateitemlist = List.Buffer(#"Changed Type"[Date Item]),
  quantitylist = List.Buffer(#"Changed Type"[Quantity]),
  result = Table.FromColumns(
    {Source[Date Item], Source[Quantity], List.Transform(FX(quantitylist), each if _ < 0 then 0 else _)},
    {"Date Item", "Quantity", "Cumulative value"}
  ),
    #"Changed Type1" = Table.TransformColumnTypes(result,{{"Date Item", type date}, {"Quantity", Int64.Type}, {"Cumulative value", Int64.Type}})
in
  #"Changed Type1"

 

Best Regards,
Yang

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Here's the table with the additional column to manage more than a single Item.

As said your solution works like a charm, I just need now to make it works with multiple items.

 

I suppose I should use something like Group rows, right?

Thanks again.

 

ItemDate ItemQuantity

Item108/21/202410
Item108/22/2024-3
Item108/23/2024-4
Item108/24/20245
Item108/25/2024-2
Item108/26/2024-4
Item108/27/2024-3
Item108/28/2024-4
Item108/29/2024-6
Item108/30/2024-2
Item108/31/202420
Item109/01/2024-2
Item109/02/2024-2
Item109/03/2024-3
Item109/04/20245
Item109/05/2024-5
Item109/06/2024-3
Item109/07/2024-2
Item109/08/2024-5
Item109/09/2024-1
Item208/21/202410
Item208/22/2024-3
Item208/23/2024-4
Item208/24/20245
Item208/25/2024-2
Item208/26/2024-4
Item208/27/2024-3
Item208/28/2024-4
Item208/29/2024-6
Item208/30/2024-2
Item208/31/202420
Item209/01/2024-2
Item209/02/2024-2
Item209/03/2024-3
Item209/04/20245
Item209/05/2024-5
Item209/06/2024-3
Item209/07/2024-2
Item209/08/2024-5
Item209/09/2024-1
Anonymous
Not applicable

Hi, @carmae 

Yes, if you want to group and accumulate data, you only need to go one more step to group by Item.

vyaningymsft_0-1724742515408.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fdIxDsMgDIXhuzCnAmxCyBF6hihjx269v8ryiBKePaJPWGD9xxHev883hyWkFiVHSVL6IadwLncT2Esn02FlsgJbJ1rHNZmsOiM35ynNubcPq0/TZL9Fx1rksZY9pmzd6yaOqfWHbtbKOl0rm606IzfnKc2Zea0sDxOnFhirBcZqgZGvg1gtMFYLjNUCY7XAWC3i1AJjtYhTC4zVAmO1wKyVGbXAWC0wVguM1QK71XL+AQ==", BinaryEncoding.Base64), Compression.Deflate)), 
    let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, #"Date Item" = _t, Quantity = _t]),
    
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Date Item", type date}, {"Quantity", Int64.Type}}),
    FX = (values as list) as list =>
        let
            GRTList = List.Generate(
                () => [GRT = values{0}, i = 0],
                each [i] < List.Count(values),
                each 
                    let
                        nextGRT = if [GRT] > 0 then [GRT] + values{[i] + 1} else values{[i] + 1}
                    in
                        [GRT = nextGRT, i = [i] + 1],
                each [GRT]
            )
        in
            GRTList,
 
    // Group by Item and apply transformations
    Grouped = Table.Group(
        #"Changed Type",
        {"Item"},
        {
            {"Transformed", each 
                let
                    dateitemlist = List.Buffer([Date Item]),
                    quantitylist = List.Buffer([#"Quantity"]),
                    result = Table.FromColumns(
                        {dateitemlist, quantitylist, List.Transform(FX(quantitylist), each if _ < 0 then 0 else _)},
                        {"Date Item", "Quantity", "Output"}
                    )
                in
                    result
            }
        }
    ),
    #"Expanded Transformed" = Table.ExpandTableColumn(Grouped, "Transformed", {"Date Item", "Quantity", "Output"}, {"Date Item", "Quantity", "Output"})
in
    #"Expanded Transformed"

 

Best Regards,
Yang

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know.
Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Working great, thank you veeeeeery much!!

Irwan
Super User
Super User

hello @carmae 

 

I might be misunderstood your need, but let me offer some ways to enlighten your problem.

 

if you need to show sum of each 'Type', then matrix visual might be your answer.

Irwan_0-1724291211407.png

 

besides that, what i got from your description, looks like you want to do cumulative sum of 'Quantity' but the value become zero if sum value is negative.

Cumulative =
var _Sum=
SUMX(
    FILTER(
        'Table',
        'Table'[Date Item]<=EARLIER('Table'[Date Item])
    ),
    'Table'[Quantity]
)
Return
IF(
    _Sum<0,
    0,
    _Sum
)

Irwan_1-1724291343036.png

 

Hope this will help.

Thank you.

Thanks for your reply. I'll try to better explain my issue: on 30/08 the quantity is zero. Then on 31/08 20 units get added to inventory. Then on 01/09 2 units are forecasted to be sold. So the quantity on 01/09 should be 18, not 5.

Maybe it's clearer if I explain that we do not keep back orders. So forecasted sales on days with zero inventory should not cumulate.

Hope I managed to explain myself, English is not my native tongue, as you can easily guess! 😄

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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