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


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
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
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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