Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Oscar_Mtz_V
Kudo Commander
Kudo Commander

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.

Annotation 2019-12-24 131038.png

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":

Annotation 2019-12-24 131038.pngCurrently 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 6
jameszhang0805
Resolver IV
Resolver IV

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

jameszhang0805_0-1613829666622.png

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
Anonymous
Not applicable

Did you solve this problem, if so how?

az38
Community Champion
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


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

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

Did you use measure to create this expression? If so, you cannot use earlier/ealiest function

 

I am trying to do this in Power Pivot.

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors