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

DAX: Create Column/Measure that returns value based on other columns

Hello there,

 

I have a table that consists of the columns below, 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 G52314CHA53385 Week 34 Opening Stock After -> 113-38+40 = 115). 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. Lastly, the Opening Stock After should never be a negative value (e.g. if value is negative, return 0 instead).

 

UniqueWeek NumberOpening StockProposed ShipmentDemandOpening Stock AfterLead Time (Week)
G52314CHA53385311200351201
G52314CHA53385321207042851
G52314CHA533853312040381131
G52314CHA533853412040371151
G52314CHA533853512040411181
G52314CHA533853612040401171
G52314CHA533853712040361171
G78321PLE51190312000572002
G78321PLE51190322000481432
G78321PLE51190332009045952
G78321PLE511903420060531402
G78321PLE511903520060511472
G78321PLE511903620060491562
G78321PLE511903720060521672

 

Would greatly appreciate some insights into this please, thank you!

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new column.

 

Picture5.png

 

Opening Stock after CC =
VAR _startweeknumber =
    MINX (
        FILTER ( Data, Data[Unique] = EARLIER ( Data[Unique] ) ),
        Data[Week Number]
    )
VAR _openingstock =
    MAXX (
        FILTER (
            Data,
            Data[Unique] = EARLIER ( Data[Unique] )
                && Data[Week Number] = _startweeknumber
        ),
        Data[Opening Stock]
    )
VAR _currentproposedshipmentcumulate =
    SUMX (
        FILTER (
            Data,
            Data[Unique] = EARLIER ( Data[Unique] )
                && Data[Week Number] < EARLIER ( Data[Week Number] )
        ),
        Data[Proposed Shipment]
    )
VAR _previousdemandcumulate =
    SUMX (
        FILTER (
            Data,
            Data[Unique] = EARLIER ( Data[Unique] )
                && Data[Week Number] < EARLIER ( Data[Week Number] )
        ),
        Data[Demand]
    )
RETURN
    IF (
        Data[Week Number] = _startweeknumber,
        _openingstock,
        _openingstock + _currentproposedshipmentcumulate - _previousdemandcumulate
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new column.

 

Picture5.png

 

Opening Stock after CC =
VAR _startweeknumber =
    MINX (
        FILTER ( Data, Data[Unique] = EARLIER ( Data[Unique] ) ),
        Data[Week Number]
    )
VAR _openingstock =
    MAXX (
        FILTER (
            Data,
            Data[Unique] = EARLIER ( Data[Unique] )
                && Data[Week Number] = _startweeknumber
        ),
        Data[Opening Stock]
    )
VAR _currentproposedshipmentcumulate =
    SUMX (
        FILTER (
            Data,
            Data[Unique] = EARLIER ( Data[Unique] )
                && Data[Week Number] < EARLIER ( Data[Week Number] )
        ),
        Data[Proposed Shipment]
    )
VAR _previousdemandcumulate =
    SUMX (
        FILTER (
            Data,
            Data[Unique] = EARLIER ( Data[Unique] )
                && Data[Week Number] < EARLIER ( Data[Week Number] )
        ),
        Data[Demand]
    )
RETURN
    IF (
        Data[Week Number] = _startweeknumber,
        _openingstock,
        _openingstock + _currentproposedshipmentcumulate - _previousdemandcumulate
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Anonymous
Not applicable

hello @Jihwan_Kim i encountered a small issue where i want the Opening Stock after CC to return 0 if the calculation returns a negative value. In calculating the next week's Opening Stock After CC it should use 0 instead of a negative value (previous week's Opening Stock After CC).

Currently, the formula uses the previous week's Opening Stock After CC value even if it's a negative value to calculate the current week's Opening Stock After CC.

Thank you once again for the help so far, very much appreicated!!

Anonymous
Not applicable

thank you so much Jihwan, it works wonderfully!

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.