Forum Discussion

G_CR_23's avatar
G_CR_23
Icon for Helper I rankHelper I
1 year ago
Solved

HELP WITH A MEASURE: First Date Running Total is 0

Hi Everyone, i am struggling with a dax measure. I need to calculate the first date where the stock for a product is equal to 0. I have 3 tables: - Purchase Table - Inbound Table - Date Table ...
  • bhanu_gautam's avatar
    1 year ago

    G_CR_23 

    First, create the measures for cumulative quantities:
    Qty Purchase = SUM('Purchase Table'[Qty purchase])

    Qty Inbound = SUM('Inbound Table'[Quantity Inbound])

    **bleep** Qty Purchase = CALCULATE([Qty Purchase], FILTER(ALL('Date Table'), 'Date Table'[Date] <= MAX('Date Table'[Date])))

    **bleep** Qty Inbound = CALCULATE([Qty Inbound], FILTER(ALL('Date Table'), 'Date Table'[Date] <= MAX('Date Table'[Date])))


    Create the measure for the actual stock:
    Actual Stock = [**bleep** Qty Inbound] - [**bleep** Qty Purchase]


    Create the measure to find the first date where the actual stock is 0:
    First Date 0 Stock =
    CALCULATE(
    MIN('Date Table'[Date]),
    FILTER(
    ALL('Date Table'),
    [Actual Stock] = 0
    )
    )


    This measure will return the first date where the actual stock is 0 by iterating over all dates and checking the condition.

     

    If you need to ensure that the date is after the first purchase date, you can modify the measure to include that condition:
    First Date 0 Stock =
    VAR FirstPurchaseDate = CALCULATE(MIN('Date Table'[Date]), FILTER(ALL('Date Table'), [Qty Purchase] > 0))
    RETURN
    CALCULATE(
    MIN('Date Table'[Date]),
    FILTER(
    ALL('Date Table'),
    [Actual Stock] = 0 && 'Date Table'[Date] >= FirstPurchaseDate

     

    This measure first calculates the first purchase date and then finds the first date where the actual stock is 0 and the date is on or after the first purchase date.