Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Calculated column making reference to another row

Hello i need help with the next issue: I need to calculate the difference between 2 dates in the subsequent row if two conditions are achieved the number one is the column [Evento] = "BAJA-DEFINITIVA...
  • Mariusz's avatar
    6 years ago

    HI Anonymous 

     

    Sure, try something like this.

    Column = 
    IF( 
        'Table'[Evento] = "BAJA - DEFINITIVA", 
        VAR __date = 'Table'[Fecha del evento] 
        VAR __nextDate =
            CALCULATE(
                MIN( 'Table'[Fecha del evento]  ),
                ALLEXCEPT( 'Table', 'Table'[Plaza Unica] ),
                'Table'[Fecha del evento] > __date
            )
        RETURN 
        IF( NOT ISBLANK( __nextDate ), __nextDate - __date )
    )

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.
  • AnthonyTilley's avatar
    6 years ago

    In M add a index column 

     

     

    you will give you a row number that can be used to help create the calulated colunm

     

    next go back to your main report and crete a calulated colunm 

     

    Column = 
    -- get the row number for the current row
    var I = 'Table'[Index]
    -- get the evento for the current row
    var e = 'Table'[Evento]
    --get the plaza unica for the current row
    var p = 'Table'[Plaza Unica]
    -- get the next row number
    var i2 = I+1
    -- get the plaza unica for the next row
    var P2 = CALCULATE(Max('Table'[Plaza Unica]),all('Table'),'Table'[Index] = i2)
    --get the current date
    var d = 'Table'[Fecha del evento]
    -- get date of next row 
    var d2 = CALCULATE(Max('Table'[Fecha del evento]),all('Table'),'Table'[Index] = i2)
    --calculate no of days between the two dates in dates
    var DD = DATEDIFF(d,d2,DAY)
    -- check if the value in evento is BAJA 
    var check = IF(e = "BAJA - DEFINITIVA",TRUE,FALSE)
    -- check if the next row is the same plaza unica
    var check2 = if(p = p2, TRUE,FALSE)
    -- check if both are true
    Var F_Check = if(AND(check,check2),TRUE,FALSE)
    
    --calculate the return value
    Var ret = if(F_Check,DD,blank())
    
    --REturn the value
    Return ret

     

    Example below

     

    as you can see above this colunm should meet your criteria 

    only if = BAJA - DEFINITIVA

    the last entry does not calculate becasue there is no follwoing row 

    calcualtes the number of days between the date in the current row and the date in the next row if criteria is met

     

    hope this helps