cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Helper II

## Avoiding Circular Discrepancies for Col A = Col B (last row) and Col B = Col A (current row)?

My goal: To use DAX to calculate Ending Inventory as a function of MAX(0, Beginning Inventory + Receipts - Forecast), for every day and every SKU.

My Issue: I do not know how to lookup the previous day's Ending Inventory without PowerBI declaring a Circular Discrepancy.

Clarification: Because Ending Inventory rounds up to 0 at the end of each day if the formula is negative, I cannot collapse my inventory calculation into a single column and subtract all forecast up to a given date and add all receipts up to a given date like this:

``````(PseudoCode)
Invalid Projected Inventory =
Max(
0,
LOOKUPVALUE(Current Inventory)
+ CALCULATE(SUM(Receipts.QTY), Receipts.Date <= Date, Receipts.Item = Item)
- CALCULATE(SUM(Forecast.QTY), Forecast.Date <= Date, Forecast.Item = Item)
)``````

The reason I cannot use that single Projected Inventory column is to consider the following example:

• Inventory for today (Oct 14) is 100
• Total sum of Forecast thru Oct 20 is 200
• Receipt of 100 will arrive Oct 21
• Forecast on Oct 21, 2021 is 40

Desired Output: Projected Inventory on Oct 21 is 60

• Inventory at start of Oct 21 is 0 (100 current inventory - 200 forecast, rounded up to 0)
• Receipt of 100 on Oct 21 - Forecast of 40 on Oct 21 = Projected Inventory of 60

Output of Invalid Formula: Projected Inventory = 0

• Current Inventory of 100 - forecast thru Oct 21 of 240 + receipts of 100 = -40
• Projected Inventory = Max(0, -40) = 0

Here is a screenshot of all the data I'm using in my test model (text data at the bottom), including my desired input which I can get working in Excel.

And here is the same setup in PowerBI:

But when I try to use DAX to calculate the Beginning and Ending inventory of each day, I get a circular discrepancy. Is there any way around this within DAX?

Here are my formulas for the calculated table:

Table Source:

``Daily Inventory = GENERATe(distinct('Date Table'[Date]),  DISTINCT(Items[Item ID]))``

Beginning Inventory:

``````Beginning Inventory =
if('Daily Inventory'[Date]=today(),
calculate(sum('Current Inventory'[Current Inventory]),filter('Current Inventory', 'Current Inventory'[Item ID]=[Item ID])),
LOOKUPVALUE('Daily Inventory'[Ending Inventory],'Daily Inventory'[Item ID], [Item ID], 'Daily Inventory'[Date], [Date]-1)
)``````

Receipts:

``Receipts = CALCULATE(sum(Receipts[QTY]), filter(Receipts, Receipts[Date]=earlier([Date])), filter(Receipts,Receipts[Item ID]=earlier([Item ID])))``

Forecast:

``Forecast = CALCULATE(sum(Forecast[QTY]), filter(Forecast, Forecast[Date]=earlier([Date])), filter(Forecast,Forecast[Item ID]=earlier([Item ID])))``

Ending Inventory:

``Ending Inventory = max([Beginning Inventory] + [Receipts] - [Forecast],0)``

And here are my data tables:

Date Table

 Date 10/14/2021 10/15/2021 10/16/2021 10/17/2021 10/18/2021

Items

 Item ID Item Name 1 TestSKU1 2 TestSKU2

Current Inventory

 Item ID Current Inventory 1 100 2 100

Forecast

 Item ID Date QTY 1 10/14/2021 14 1 10/15/2021 12 1 10/16/2021 3 1 10/17/2021 24 1 10/18/2021 16 2 10/14/2021 43 2 10/15/2021 46 2 10/16/2021 7 2 10/17/2021 22 2 10/18/2021 32

Receipts

 Item ID Date QTY 1 10/17/2021 200 2 10/17/2021 200
15 REPLIES 15
Frequent Visitor

See if the picture below is the similar result you want, realize the end of the previous period, and put it at the beginning of the next period.

https://demo.jiaopengzi.com/pbi/150-full.html

https://jiaopengzi.com/video/video-101

Frequent Visitor

Here's the information you're looking for.

1. In the future, please upload an attachment instead; I spent quite a while copying from it.
2. The difficult part is writing the measure of "01_Beginning Inventory". I have provided compatibility for various time dimensions (daily, weekly, monthly, quarterly, semi-annually, and annually) of the measure.
3. Note that your "Current Inventory" table should include a date dimension.
4. Essentially, there are only inbound and outbound operations. Initial, final, and inventory values are derived from these, so the model should be designed with this logic in mind.

``````01_Beginning Inventory =
VAR DATE_START0 =
CALCULATE ( FIRSTDATE ( 'Calendar'[dates] ), ALL ( 'Calendar' ) )
VAR DATE_END0 =
LASTDATE ( 'Calendar'[dates] )
VAR TF0 =
HASONEVALUE ( 'Calendar'[dates] ) //日
VAR TF1 =
HASONEVALUE ( 'Calendar'[YearWeek] ) //周
VAR TF2 =
HASONEVALUE ( 'Calendar'[YearMonth] ) //月
VAR TF3 =
HASONEVALUE ( 'Calendar'[YearQuarter] ) //季度
VAR TF4 =
HASONEVALUE ( 'Calendar'[YearHalf] ) //半年度
VAR TF5 =
HASONEVALUE ( 'Calendar'[FY00] ) //年度
VAR N0 =
SWITCH (
TRUE (),
TF0, 0,
TF1,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfWeek] ),
LASTDATE ( 'Calendar'[EndOfWeek] ),
DAY
),
TF2,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfMonth] ),
LASTDATE ( 'Calendar'[EndOfMonth] ),
DAY
),
TF3,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfQuarter] ),
LASTDATE ( 'Calendar'[EndOfQuarter] ),
DAY
),
TF4,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfHalfYear] ),
LASTDATE ( 'Calendar'[EndOfHalfYear] ),
DAY
),
TF5,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfYear] ),
LASTDATE ( 'Calendar'[EndOfYear] ),
DAY
),
0
) + 1
VAR DATE_END1 =
SWITCH (
TRUE (),
TF0, DATEADD ( DATE_END0, - N0, DAY ),
TF1, DATEADD ( LASTDATE ( 'Calendar'[EndOfWeek] ), - N0, DAY ),
TF2, DATEADD ( LASTDATE ( 'Calendar'[EndOfMonth] ), - N0, DAY ),
TF3, DATEADD ( LASTDATE ( 'Calendar'[EndOfQuarter] ), - N0, DAY ),
TF4, DATEADD ( LASTDATE ( 'Calendar'[EndOfHalfYear] ), - N0, DAY ),
TF5, DATEADD ( LASTDATE ( 'Calendar'[EndOfYear] ), - N0, DAY ),
DATE_START0 //无筛选的时，默认日期表期初。
)
VAR DATE_END2 =
IF ( ISBLANK ( DATE_END1 ), DATE_START0, DATE_END1 ) //兼容 dateadd 后的空值，注意日期表的两个端点。
VAR DATE_TABLE0 =
DATESBETWEEN ( 'Calendar'[dates], DATE_START0, DATE_END2 )
VAR IN0 =
CALCULATE ( [02_Receipts], DATE_TABLE0 )
VAR OUT0 =
CALCULATE ( [03_Forecast], DATE_TABLE0 )
RETURN
IF ( [03_Forecast], IN0 - OUT0, BLANK () )``````

``````02_Receipts =
SUM ( 'Current Inventory'[Current Inventory] ) + SUM ( 'Receipts'[QTY] ) + 0``````

``````02_Receipts_Display =
IF ( [03_Forecast], [02_Receipts], BLANK () )``````

``````03_Forecast =
SUM ( Forecast[QTY] )``````

``````04_Ending Inventory =
VAR DATE_START0 =
CALCULATE ( FIRSTDATE ( 'Calendar'[dates] ), ALL ( 'Calendar' ) )
VAR DATE_END0 =
LASTDATE ( 'Calendar'[dates] )
VAR DATE_TABLE0 =
DATESBETWEEN ( 'Calendar'[dates], DATE_START0, DATE_END0 )
VAR IN0 =
CALCULATE ( [02_Receipts], DATE_TABLE0 )
VAR OUT0 =
CALCULATE ( [03_Forecast], DATE_TABLE0 )
RETURN
IF ( [03_Forecast], IN0 - OUT0, BLANK () )``````

Frequent Visitor

this？

``````01_Beginning Inventory_Display =
VAR DATE_START0 =
CALCULATE ( FIRSTDATE ( 'Calendar'[dates] ), ALL ( 'Calendar' ) )
VAR DATE_END0 =
LASTDATE ( 'Calendar'[dates] )
VAR TF0 =
HASONEVALUE ( 'Calendar'[dates] ) //日
VAR TF1 =
HASONEVALUE ( 'Calendar'[YearWeek] ) //周
VAR TF2 =
HASONEVALUE ( 'Calendar'[YearMonth] ) //月
VAR TF3 =
HASONEVALUE ( 'Calendar'[YearQuarter] ) //季度
VAR TF4 =
HASONEVALUE ( 'Calendar'[YearHalf] ) //半年度
VAR TF5 =
HASONEVALUE ( 'Calendar'[FY00] ) //年度
VAR N0 =
SWITCH (
TRUE (),
TF0, 0,
TF1,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfWeek] ),
LASTDATE ( 'Calendar'[EndOfWeek] ),
DAY
),
TF2,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfMonth] ),
LASTDATE ( 'Calendar'[EndOfMonth] ),
DAY
),
TF3,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfQuarter] ),
LASTDATE ( 'Calendar'[EndOfQuarter] ),
DAY
),
TF4,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfHalfYear] ),
LASTDATE ( 'Calendar'[EndOfHalfYear] ),
DAY
),
TF5,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfYear] ),
LASTDATE ( 'Calendar'[EndOfYear] ),
DAY
),
0
) + 1
VAR DATE_END1 =
SWITCH (
TRUE (),
TF0, DATEADD ( DATE_END0, - N0, DAY ),
TF1, DATEADD ( LASTDATE ( 'Calendar'[EndOfWeek] ), - N0, DAY ),
TF2, DATEADD ( LASTDATE ( 'Calendar'[EndOfMonth] ), - N0, DAY ),
TF3, DATEADD ( LASTDATE ( 'Calendar'[EndOfQuarter] ), - N0, DAY ),
TF4, DATEADD ( LASTDATE ( 'Calendar'[EndOfHalfYear] ), - N0, DAY ),
TF5, DATEADD ( LASTDATE ( 'Calendar'[EndOfYear] ), - N0, DAY ),
DATE_START0 //无筛选的时，默认日期表期初。
)
VAR DATE_END2 =
IF ( ISBLANK ( DATE_END1 ), DATE_START0, DATE_END1 ) //兼容 dateadd 后的空值，注意日期表的两个端点。
VAR DATE_TABLE0 =
DATESBETWEEN ( 'Calendar'[dates], DATE_START0, DATE_END2 )
VAR _Begin =
CALCULATE ( [04_Ending Inventory_Display], DATE_TABLE0 )
VAR IN0 =
CALCULATE ( [02_Receipts], DATE_TABLE0 )
VAR OUT0 =
CALCULATE ( [03_Forecast], DATE_TABLE0 )
RETURN
IF ( [03_Forecast], COALESCE( _Begin,IN0-OUT0), BLANK () )``````

``````04_Ending Inventory_Display =
VAR ItemID_AC = SELECTEDVALUE('Items'[Item ID])
VAR DATE_AC = LASTDATE('Calendar'[Dates])
VAR T0 =
FILTER (
CROSSJOIN ( ALL(  'Calendar'[Dates] ), ALL( 'Items'[Item ID] ) ),
"@END0", 'Measure'[04_Ending Inventory]
),
NOT ( ISBLANK ( [@END0] ) )
)
VAR T1 =
T0,
"@Index",
COUNTROWS (
WINDOW (
1,
ABS,
0,
REL,
T0,
ORDERBY ( 'Calendar'[Dates], ASC, Items[Item ID], ASC ),
PARTITIONBY ( Items[Item ID] )
)
)
)
VAR T2 =
T1,
"@Acc",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T1, [Item ID] = ItemID  && [@Index] <= R )
VAR Acc = SUMX ( T, [@END0] )
RETURN
Acc
)
VAR T3 =
T2,
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T2,   [Item ID] = ItemID && [@Index] <= R )
RETURN
MINX ( T, [@Acc] )
)
VAR T4 =
T3,
"@minIndex",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T3, [Item ID] = ItemID && [@Index] <= R )
VAR MIN0 = MINX ( T, [@Acc] )
VAR TT = FILTER ( T, [@isAdd] = MIN0 )
VAR I = MINX ( TT, [@Index] )
RETURN
I,
"@Diff",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T3, [Item ID] = ItemID && [@Index] <= R )
VAR MIN0 = MINX ( T, [@Acc] )
VAR TT = FILTER ( T, [@isAdd] = MIN0 )
VAR I = MINX ( TT, [@Index] )
VAR DIFF = SUMX ( FILTER ( T, [@Index] = I ), [@END0] )
RETURN
DIFF
)
VAR T5 =
T4,
"@END1",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T4, [Item ID] = ItemID && [@Index] <= R )
VAR X =
SWITCH (
TRUE (),
R >= [@minIndex] && [@Diff] < 0, [@END0] - [@Diff],
R < [@minIndex] && [@Diff] < 0, 0,
[@END0]
)
RETURN
X
)
VAR T6 = FILTER ( T5, [Dates] = DATE_AC && [Item ID] = ItemID_AC )
VAR RESULT = SUMX ( T6, [@END1] )
RETURN
RESULT``````

Frequent Visitor

Hi @jiaopengzi .. it was working like a charm but noticed the index restarts everytime the 04 Ending inventory is 0 right? In sample below, for Aug'23, previous ending inventory plus receipts (2642 + 2206) is greater than forecast (4556) so we expect a positive ending inventory display but the measure gives 0. Any magical fixes you would suggest?

 Month Product Forecast Receipts 04_Ending Inventory 04_Ending Inventory_Display 01-05-2023 AB 5278 3009 -2269 0 01-06-2023 AB 4686 7202 247 2515 01-07-2023 AB 3708 3835 373 2642 01-08-2023 AB 4556 2206 -1977 0 01-09-2023 AB 4083 6177 118 2094 01-10-2023 AB 1051 1169 3145
Frequent Visitor

``````04_Ending Inventory_Display =
VAR ItemID_AC = SELECTEDVALUE('Items'[Item ID])
VAR DATE_AC = LASTDATE('Calendar'[Dates])
VAR T0 =
FILTER (
CROSSJOIN ( ALL(  'Calendar'[Dates] ), ALL( 'Items'[Item ID] ) ),
"@IN",[02_Receipts_Display],
"@OUT",[03_Forecast],
"@END0", [04_Ending Inventory]
),
NOT ( ISBLANK ( [@END0] ) )
)
VAR T1 =
T0,
"@Index",
COUNTROWS (
WINDOW (
1,
ABS,
0,
REL,
T0,
ORDERBY ( 'Calendar'[Dates], ASC, Items[Item ID], ASC ),
PARTITIONBY ( Items[Item ID] )
)
)
)
VAR T2 =
T1,
"@Acc",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T1, [Item ID] = ItemID  && [@Index] <= R )
VAR Acc = SUMX ( T, [@END0] )
RETURN
Acc
)
VAR T3 =
T2,
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T2,   [Item ID] = ItemID && [@Index] <= R )
RETURN
MINX ( T, [@Acc] )
)
VAR T4 =
T3,
"@minIndex",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T3, [Item ID] = ItemID && [@Index] <= R )
VAR MIN0 = MINX ( T, [@Acc] )
VAR TT = FILTER ( T, [@isAdd] = MIN0 )
VAR I = MINX ( TT, [@Index] )
RETURN
I,
"@Diff",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T3, [Item ID] = ItemID && [@Index] <= R )
VAR MIN0 = MINX ( T, [@Acc] )
VAR TT = FILTER ( T, [@isAdd] = MIN0 )
VAR I = MINX ( TT, [@Index] )
VAR DIFF = SUMX ( FILTER ( T, [@Index] = I ), [@END0] )
RETURN
DIFF
)
VAR T5 =
T4,
"@END1",
VAR R = [@Index] - 1
VAR ItemID = [Item ID]
VAR T = FILTER ( T4, [Item ID] = ItemID && [@Index] = R )
VAR END0_Previous = SUMX ( T, [@END0] )
VAR X =
SWITCH (
TRUE (),
[@END0] < 0 && [@IN] - [@OUT] > 0, [@IN] - [@OUT],
R >= [@minIndex] && [@Diff] < 0, [@END0] - [@Diff],
R < [@minIndex] && [@Diff] < 0, 0,
END0_Previous < 0, [@END0] - END0_Previous,
[@END0]
)
RETURN
X
)
VAR T6 =
T5,
"@BEGIN1",
VAR R = [@Index] - 1
VAR ItemID = [Item ID]
VAR T = FILTER ( T5, [Item ID] = ItemID && [@Index] = R )
VAR END1_Previous = SUMX ( T, [@END1] )
VAR BEGIN1 = IF ( END1_Previous < 0 || END1_Previous = BLANK(), 0, END1_Previous )
RETURN
BEGIN1
)
VAR T7 =
T6,
"@END2",
SWITCH (
TRUE (),
[@BEGIN1] + [@IN] - [@OUT] > 0, [@BEGIN1] + [@IN] - [@OUT],
[@END0]<0 && [@BEGIN1] + [@IN] - [@OUT] <= 0, 0,
[@END0]
)

)
VAR T8 = FILTER ( T7, [Dates] = DATE_AC && [Item ID] = ItemID_AC )
VAR RESULT = SUMX ( T8, [@END2] )
RETURN
RESULT``````

``````01_Beginning Inventory_Display =
VAR DATE_START0 =
CALCULATE ( FIRSTDATE ( 'Calendar'[dates] ), ALL ( 'Calendar' ) )
VAR DATE_END0 =
LASTDATE ( 'Calendar'[dates] )
VAR TF0 =
HASONEVALUE ( 'Calendar'[dates] ) //日
VAR TF1 =
HASONEVALUE ( 'Calendar'[YearWeek] ) //周
VAR TF2 =
HASONEVALUE ( 'Calendar'[YearMonth] ) //月
VAR TF3 =
HASONEVALUE ( 'Calendar'[YearQuarter] ) //季度
VAR TF4 =
HASONEVALUE ( 'Calendar'[YearHalf] ) //半年度
VAR TF5 =
HASONEVALUE ( 'Calendar'[FY00] ) //年度
VAR N0 =
SWITCH (
TRUE (),
TF0, 0,
TF1,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfWeek] ),
LASTDATE ( 'Calendar'[EndOfWeek] ),
DAY
),
TF2,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfMonth] ),
LASTDATE ( 'Calendar'[EndOfMonth] ),
DAY
),
TF3,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfQuarter] ),
LASTDATE ( 'Calendar'[EndOfQuarter] ),
DAY
),
TF4,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfHalfYear] ),
LASTDATE ( 'Calendar'[EndOfHalfYear] ),
DAY
),
TF5,
DATEDIFF (
FIRSTDATE ( 'Calendar'[StartOfYear] ),
LASTDATE ( 'Calendar'[EndOfYear] ),
DAY
),
0
) + 1
VAR DATE_END1 =
SWITCH (
TRUE (),
TF0, DATEADD ( DATE_END0, - N0, DAY ),
TF1, DATEADD ( LASTDATE ( 'Calendar'[EndOfWeek] ), - N0, DAY ),
TF2, DATEADD ( LASTDATE ( 'Calendar'[EndOfMonth] ), - N0, DAY ),
TF3, DATEADD ( LASTDATE ( 'Calendar'[EndOfQuarter] ), - N0, DAY ),
TF4, DATEADD ( LASTDATE ( 'Calendar'[EndOfHalfYear] ), - N0, DAY ),
TF5, DATEADD ( LASTDATE ( 'Calendar'[EndOfYear] ), - N0, DAY ),
DATE_START0 //无筛选的时，默认日期表期初。
)
VAR DATE_END2 =
IF ( ISBLANK ( DATE_END1 ), DATE_START0, DATE_END1 ) //兼容 dateadd 后的空值，注意日期表的两个端点。
VAR DATE_TABLE0 =
DATESBETWEEN ( 'Calendar'[dates], DATE_START0, DATE_END2 )
VAR _Begin =
CALCULATE ( [04_Ending Inventory_Display], DATE_TABLE0 )
VAR IN0 =
CALCULATE ( [02_Receipts], DATE_TABLE0 )
VAR OUT0 =
CALCULATE ( [03_Forecast], DATE_TABLE0 )
RETURN
IF ( [03_Forecast], COALESCE( _Begin,IN0-OUT0), BLANK () )``````

Frequent Visitor

Thanks @jiaopengzi .. it is one step closer.. but for September it is not carrying forward the August ending on hand. it should be (293+6617)-4803 but it is calculating 6617-4803 = 1814.

Am trying to replicate your logic in excel to understand it better but am struggling to recreate it after Acc.. Do you mind sharing a table as to what are intended values for Index, Acc, isadd, minIndex, Diff, End1 you are using in your measure logic for my sample data?

Frequent Visitor

It's me who overcomplicated your question. The conventional invoicing idea should not be used, and the conventional invoicing should not have negative numbers.
Then change the way of thinking and solve the problem.

``````01_Beginning Inventory_Display =
VAR ItemID_AC = SELECTEDVALUE('Items'[Item ID])
VAR DATE_AC = LASTDATE('Calendar'[Dates])
VAR T0 =
FILTER (
CROSSJOIN ( ALL(  'Calendar'[Dates] ), ALL( 'Items'[Item ID] ) ),
"@IN",[02_Receipts_Display],
"@OUT",[03_Forecast],
"@END0", [02_Receipts_Display] - [03_Forecast]
),
NOT ( ISBLANK ( [@END0] ) )
)

VAR T1 =
T0,
"@Index",
COUNTROWS (
WINDOW (
1,
ABS,
0,
REL,
T0,
ORDERBY ( 'Calendar'[Dates], ASC, Items[Item ID], ASC ),
PARTITIONBY ( Items[Item ID] )
)
)
)
VAR T2 =
T1,
"@Acc",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T1, [Item ID] = ItemID && [@Index] <= R )
VAR Acc = SUMX ( T, [@END0] )
RETURN
Acc
)

VAR T3 =
T2,
"@END1",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T2, [Item ID] = ItemID && [@Index] <= R )
VAR minOfSum = MIN ( 0, MINX ( T, [@acc] ) )
RETURN
[@acc] - minOfSum

)

VAR T4 =
T3,
"@BEGIN1",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T3, [Item ID] = ItemID && [@Index] = R - 1)
VAR END1_Previous = SUMX ( T, [@END1] )
VAR BEGIN1 = IF ( END1_Previous < 0 || END1_Previous = BLANK(), 0, END1_Previous )
RETURN
BEGIN1
)
VAR T5 =
T4,
"@END2",
IF([@BEGIN1] + [@IN] - [@OUT] > 0, [@BEGIN1] + [@IN] - [@OUT],0)
)
VAR T6 = FILTER ( T5, [Dates] = DATE_AC && [Item ID] = ItemID_AC )
VAR RESULT = SUMX ( T6, [@BEGIN1] )
RETURN
RESULT``````

``````04_Ending Inventory_Display =
VAR ItemID_AC = SELECTEDVALUE('Items'[Item ID])
VAR DATE_AC = LASTDATE('Calendar'[Dates])
VAR T0 =
FILTER (
CROSSJOIN ( ALL(  'Calendar'[Dates] ), ALL( 'Items'[Item ID] ) ),
"@IN",[02_Receipts_Display],
"@OUT",[03_Forecast],
"@END0", [02_Receipts_Display] - [03_Forecast]
),
NOT ( ISBLANK ( [@END0] ) )
)

VAR T1 =
T0,
"@Index",
COUNTROWS (
WINDOW (
1,
ABS,
0,
REL,
T0,
ORDERBY ( 'Calendar'[Dates], ASC, Items[Item ID], ASC ),
PARTITIONBY ( Items[Item ID] )
)
)
)
VAR T2 =
T1,
"@Acc",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T1, [Item ID] = ItemID && [@Index] <= R )
VAR Acc = SUMX ( T, [@END0] )
RETURN
Acc
)

VAR T3 =
T2,
"@END1",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T2, [Item ID] = ItemID && [@Index] <= R )
VAR minOfSum = MIN ( 0, MINX ( T, [@acc] ) )
RETURN
[@acc] - minOfSum

)

VAR T4 =
T3,
"@BEGIN1",
VAR R = [@Index]
VAR ItemID = [Item ID]
VAR T = FILTER ( T3, [Item ID] = ItemID && [@Index] = R - 1)
VAR END1_Previous = SUMX ( T, [@END1] )
VAR BEGIN1 = IF ( END1_Previous < 0 || END1_Previous = BLANK(), 0, END1_Previous )
RETURN
BEGIN1
)
VAR T5 =
T4,
"@END2",
IF([@BEGIN1] + [@IN] - [@OUT] > 0, [@BEGIN1] + [@IN] - [@OUT],0)
)
VAR T6 = FILTER ( T5, [Dates] = DATE_AC && [Item ID] = ItemID_AC )
VAR RESULT = SUMX ( T6, [@END2] )
RETURN
RESULT``````

Frequent Visitor

Thanks a ton.. will try it out in my dataset

Frequent Visitor

thank you very much for sharing it.. but in your data example your forecast is always less than (Begining) + Receipts. Lets say your first Forecast is 114 instead of 14. Then the ending will be negative, how can we ensure we dont carry forward the negatives?

Helper II

 Beginning Inv Demand Receipts Ending Inventory 400 1000 0 0 0 1000 2000 1000

My obstacle is this edge case where Ending Inventory wants to go negative, in which case I need it to stay 0. For my purposes, we don't have a backlog, so "Negative Inventory" is just "Lost Sales", not "Backlog".

If I round up negative values to 0, then the numbers are still off because the demand and receipts are rolling. In other words, with the above case:

Inv = 400

Rolling Demand = 2000

Rolling Receipts = 2000

Ending Inventory = 400 + 2000 - 2000 = 400 (wrong, should be 1000).

Frequent Visitor

did you find a solution? Am struggling with a similar scenario..

Helper II

Sorry, I gave up trying to find a solution that would allow me to forecast going to 0 and receiving the full amount in the future. Anything I could do in one function can only take me to 0, or negative, in which case the cumulative receipts don't add up properly.

I ended up settling for just keeping the visibility of when I'd go out of stock, and anything past that becomes an unknown.

Frequent Visitor

Oh ok.. am still struggling to just use the previous week balance into next week blance calculation. How did you achieve that? Can you share the file or the measure logic please?

Community Champion

See if this explanation from the gurus at SQLBI helps:

In doing so, you are also helping me. Thank you!

Proud to be a Super User!

Helper II

Thank you for the video. Unfortunately it does not appear to solve my issue (I tried all 3 solutions to make sure). The video seems focused on finding more indirect-hidden circular depencies, whereas my issue could be classified as a "direct-visible circular discrepancy".

In other words, my calculated table has Column A referencing Column B, and Column B references Column A. But what's frustrating is that PowerBI seems to flag this as an issue, despite my code filtering out true discrepancies by making the reference sequential - by that I mean that Column A references column B from the previous row, and Column B references Column A from the current row.

Here's an example in Excel, to help illustrate what I mean:

Excel handles this just fine when I set up a test example. I need a way to achieve the same goal within PowerBI, because all of the fields I need to use for the calculation is generated in PowerBI.

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

#### Power BI Monthly Update - June 2024

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

#### Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors
Top Kudoed Authors