Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Calculating short using row context.

Hi All,

 

I have been struggling to get this measure done. I badly need help with this.

So the use case here is I need to calculate the Short measure. Coming to the actual struggle. As you can see in the table for the same Product and location the dates and the End_inv is different. Whenever the end_inv is negative the shortage is Yes and positive No. I need to calculate the Short in the following manner:

Short = when shortage is No it is 0 and the next row after 0 should be 1 i.e. if there is a Yes after a No then it should return 1 but all the Shortage= Yes should not be 1.

ProductLocationDateEnd_invShortage Short 
400269111008-10-202230No 0
400269111008-11-2022-2Yes1
400269111008/13/202210No 0
400269111008/15/20223No 0
400270111008/14/2022-10Yes1
400270111008/15/2022-5YEs0
400270111008/16/20225No 0
400270111008/18/20226No 0

Hope you understood the query.
Note: End_inv, Shortage are measures.
Thanks in advance.
tamerj1 JihaiXia Henrykong_ 

  • "Hi Anonymous 

    assuming [Sortage] is a measure, please try

     

    Short =
    VAR CurrentDate =
        SELECTEDVALUE ( 'Date'[Date] )
    VAR CurrentShortage = [Shortage]
    VAR T1 =
        CALCULATETABLE (
            TableName,
            ALLEXCEPT ( TableName, TableName[Product], TableName[Location] )
        )
    VAR T2 =
        FILTER ( T1, TableName[Date] < CurrentDate )
    VAR PreviousDate =
        MAXX ( T2, TableName[Date] )
    VAR PreviousShortage =
        CALCULATE ( [Shortage], 'Date'[Date] = PreviousDate, T1 )
    RETURN
        IF ( CurrentShortage = "Yes" && PreviousShortage <> "Yes", 1, 0 )

1 Reply

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    "Hi Anonymous 

    assuming [Sortage] is a measure, please try

     

    Short =
    VAR CurrentDate =
        SELECTEDVALUE ( 'Date'[Date] )
    VAR CurrentShortage = [Shortage]
    VAR T1 =
        CALCULATETABLE (
            TableName,
            ALLEXCEPT ( TableName, TableName[Product], TableName[Location] )
        )
    VAR T2 =
        FILTER ( T1, TableName[Date] < CurrentDate )
    VAR PreviousDate =
        MAXX ( T2, TableName[Date] )
    VAR PreviousShortage =
        CALCULATE ( [Shortage], 'Date'[Date] = PreviousDate, T1 )
    RETURN
        IF ( CurrentShortage = "Yes" && PreviousShortage <> "Yes", 1, 0 )