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
Anonymous
Not applicable

Need help please on DAX: Calculated Column

Hello all,

 

Posting here again as I'm still unable to get an answer, sincerely appreciate all insights.

 

I would like to have a formula that creates the column "Opening Stock After" for the current Week which is derived from subtracting the the previous Week's Demand from the previous Week's Opening Stock After and adding the previous Week's Proposed Shipment. (e.g. for G52314 Week 34 Opening Stock After -> 113-70+40 = 83) (e.g. for G52314 Week 37 Opening Stock After -> 0-40+100=60).

 

However for the smallest week number (Week 31), since there is no data for the previous week, the Opening Stock After is always equal to the Opening Stock.

 

The Opening Stock After should never be a negative value (e.g. if value is negative, return 0 instead). If the previous week's Opening Stock After is negative, 0 should be used in the calculation of the current week's Opening Stock After (e.g. X78321 Week 37 calculation should be 0-49+60= 11).

 

 

UniqueWeek NumberOpening StockProposed ShipmentDemandOpening Stock After
G5231431120035120
G5231432120704285
G52314331204070113
G52314341204013083
G5231435120801400
G5231436120100400
G5231437120403660
X7832131200057200
X78321322002048143
X783213320090120115
X78321342006015085
X7832135200601500
X783213620060490
X7832137200605211

 

how it looks like on excel formulas:

bluerooster_0-1661485014776.png

 

Once again, thank you for all the help.

 

1 ACCEPTED SOLUTION

 @Anonymous 
Please find attached modified sample file. Note that performance would be most probably bad.

1.png2.png

View solution in original post

14 REPLIES 14
tamerj1
Super User
Super User

Good morning @Anonymous 

Please refer to attached sample file with the solution. 

I have to mention that this solution is not perfect. There could be some cases where it might not provide correct results however, such cases are extremely rare. Anyway the solution is already complex and tying to improve further will result in even more complex and less efficient solution which I guess won't be necessary. Please let me know if you have any further questions.

1.png

 

Opening Stock After = 
VAR CurrentWeek1 = Data[Week Number]
VAR OpeningStock1 = Data[Opening Stock]
VAR CurrentUniqueTable = CALCULATETABLE ( Data, ALLEXCEPT ( Data, Data[Unique] ) )
VAR StockAfterTable = 
    ADDCOLUMNS ( 
        CurrentUniqueTable, 
        "@StockAfter", 
        VAR CurrentWeek2 = [Week Number]
        VAR TableBefore = FILTER ( CurrentUniqueTable, [Week Number] < CurrentWeek2 )
        VAR Value1 =
            SUMX ( 
                TableBefore, 
                [Proposed Shipment] - [Demand]
            ) 
        VAR Value2 = Value1 + OpeningStock1
        RETURN
            IF ( Value2 < 0, 0, Value2 )
    )
VAR CurrentRecord = FILTER ( StockAfterTable, [Week Number] = CurrentWeek1 )
VAR PreviousRecord = FILTER ( StockAfterTable, [Week Number] = CurrentWeek1 - 1 )
VAR OpeningStock2 = MINX ( CurrentRecord, [@StockAfter] )
VAR Result =
    IF ( OpeningStock2 = 0, SUMX ( PreviousRecord, [Proposed Shipment] - [Demand] ), OpeningStock2 )
RETURN
    IF ( Result < 0, 0, Result )

 

Anonymous
Not applicable

Good morning @tamerj1 

 

Thank you soooo much for your reply!! The code works almost perfectly, however I found that if i added more weeks after Week 37 the code doesn't return the right answers. I added in new data for Week 38 and Week 39 and experimented with the code. 

The desired outcome with Week 38 and 39 data:

 

UniqueWeek NumberOpening StockProposed ShipmentDemandOpening Stock After
G5231431120035120
G5231432120704285
G52314331204070113
G52314341204013083
G5231435120801400
G5231436120100400
G5231437120403660
G5231438120503864
G5231439120604076
X7832131200057200
X78321322002048143
X783213320090120115
X78321342006015085
X7832135200601500
X783213620060490
X7832137200605211
X7832138200503019
X7832139200602539

 

The result on PBI:

bluerooster_0-1661938583512.png

 

For Week 38 and 39 the code took the previous week's Proposed Shipment - Demand without adding in the previous week's Opening Stock After.

 

Thank you once again for your help thus far... 🙏

 

HI @Anonymous 
I was able to get correct numbers ofr the added rows. However, I went further and added even more rows and realized that cycling between positive to negative to positive more than one time will make the DAX solution impossible to achieve. What can be achieved with a simple function in excel cannot be achieved with DAX. In general DAX does not support recursive calculations but in some cases there are workarounds apparently not in this case.

1.png2.png

Anonymous
Not applicable

I see, thank you very much @tamerj1 for your generous help... If there is a work around, creating new measures or other calculated columns to achieve the desired result, I'm all ears... 

@Anonymous 

Isthere a maximum number of restart cycles?

Anonymous
Not applicable

@tamerj1 
No, it is random depending on the Demand/Proposed Shipment for the current week as well as the previous week's Opening Stock After..

@Anonymous 

I know its random but in general can we say this cannot happen for example more than 5 times or it  can be much more?

Anonymous
Not applicable

@tamerj1 
Actually yes, you could say the maximum restart cycles is the number of weeks that the current dataset spans over (e.g. Week 31-37, maximum restart cycles = 6).

 @Anonymous 
Please find attached modified sample file. Note that performance would be most probably bad.

1.png2.png

Anonymous
Not applicable

Good morning @tamerj1 
I've tried the code above and adjusted it accordingly to the maximum number of weeks that my dataset could span over and so far it works! I can't thank you enough for your big hearted help over the past few days... you made my week and I hope you have a great weekend ahead, thank you 😃

@Anonymous 

I know its random but in general can we say this cannot happen for example more than 5 times or it  can be much more?

Anonymous
Not applicable

@tamerj1 
Or you could say the maximum restart cycles is the number of weeks that the current dataset spans over (e.g. Week 31-37, maximum restart cycles = 6).

tamerj1
Super User
Super User

Hi @Anonymous 

when I reached the part when you said "If the previous week's Opening Stock After is negative, 0 should be used in the calculation of the current week's I was like 😅

I don't think it's going to be easy. However, I have something in mind, let me try apply it tomorrow morning. Anyway, this is not going to be efficient by any means so I hope your data is not that big. 

Anonymous
Not applicable

hi @tamerj1 

thank you for helping 🙏

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.