Forum Discussion

Oscar_Mtz_V's avatar
Oscar_Mtz_V
Kudo Commander
6 years ago

Previous non-continuous date value

I have a table from where I want to get the value from the previous available date for the specific Country-Indicator. The previous available date can be a day, month or quarter before.

I am available to get the inmediate previous date for the specified "Country-Indicator Key", and this is the formula I need support with twiking.

 

Previous Date = 
    CALCULATE(
        MAX(Indicators[Date]),
        ALLEXCEPT(Indicators,Indicators[Country-Indicator]),
        Indicators[Date]<EARLIER(Indicators[Date])
    )

 

 

 What I get from the above DAX is a column that shows the previous available date for the specified "Country-Indicator":

Currently I have a work arround that creates a "Previous Key" and then looks for this value on the table, thus bringing the previous value, this can be either calculated column by column or in a single column:

 

Previous Value (not Fancy) = 
VAR
PreviousDate = 
    CALCULATE(
        MAX(Indicators[Date]),
        ALLEXCEPT(Indicators,Indicators[Country-Indicator]),
        Indicators[Date]<EARLIER(Indicators[Date])
    )

VAR
PreviousKey = 
FORMAT(PreviousDate,"yyyy-mm-dd")
&"-"&
Indicators[Country-Indicator]

RETURN

LOOKUPVALUE(
    Indicators[Value_Rev],
    Indicators[key],
    PreviousKey
)

 

As mentioned I am looking for a way of modifying the "Previous Date" formulato bring the previous value instead of the previous date. I am just looking for a more elegant solution which I am confident it exists.

 

In the link below you can download the files.

 

https://1drv.ms/u/s!Aj1t-UWaJ-akgbRQhZIcveNiSLfYuQ?e=NE5q2Z

 

In advance thanks for your support.

6 Replies

  • Oscar_Mtz_V 
    If the below screenshot is your expected result , please try the below code, you don't need to combine the Country and Indicator as a key, don't need to add an index column in Power Query. Just use DAX code.

    PreviPreviousValue = 
    VAR _CurrentDate =
        SELECTEDVALUE ( Sales[Date] )
    VAR _PreviousDate =
        CALCULATE (
            MAX ( 'Sales'[Date] ),
            ALLEXCEPT ( Sales, Sales[Country], Sales[Indicator] ),
            KEEPFILTERS ( 'Sales'[Date] < _CurrentDate )
        )
    VAR _Result =
        CALCULATE (
            SUM ( Sales[Value_Rev] ),
            'Sales'[Date] = _PreviousDate,
            REMOVEFILTERS ( Sales[Date] )
        )
    RETURN
        _Result
  • az38's avatar
    az38
    Community Champion

    Hi Oscar_Mtz_V 

    I think a good solution would be to create an Index column via Power QUery Editor mode (Add column pane -> Index column)

    Then you will be able to do absolutely the same workaround as for date, but for Index column, like

    Previous Value = 
        CALCULATE(
            MAX(Indicators[IndexColumn]),
            ALLEXCEPT(Indicators,Indicators[Country-Indicator]),
            Indicators[IndexColumn]<EARLIER(Indicators[IndexColumn])
        )

    do not hesitate to give a kudo to useful posts and mark solutions as solution

    LinkedIn

    • Chris_Botha's avatar
      Chris_Botha
      Helper I

      Why do I keep getting a earlier/earliest refers to earlier row contex that does not exist error??  Please help

  • Anonymous's avatar
    Anonymous
    Not applicable

    Did you solve this problem, if so how?