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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Destrogiro
Regular Visitor

EARLIER Function

Hi,

I'm new to Power BI and I'm having trouble with EARLIER function. Basically what I need is:

 

ValorTotal = SWITCH(TCBLBI_Peso_Empresas_Tradadas[OrdemGrupo];

                    2; EARLIER([Valor];1);

                    5; EARLIER([Valor];1);

                    7; EARLIER([Valor];1);

                    3; EARLIER([Valor];2);

                    6; EARLIER([Valor];2);

                    9; EARLIER([Valor];2); 0)

 

But I'm getting this error message: EARLIER/EARLIEST refers to an earlier row context which doesn't exist.

 

Is there a way to do this?

 

Thank you.

1 ACCEPTED SOLUTION

Since the introduction of VAR, there's no actual need for EARLIER any longer.  So you might want to do it in a different way, perhaps easier to follow conceptually, and forget about EARLIER altogether (which is actually recommended nowadays):

Value2 =
SWITCH (
    TRUE (),
    Table1[GroupOrder] IN { 1, 4, 7 }, 0,
    Table1[GroupOrder] IN { 2, 5, 8 },
    VAR PreviousGroupOrder_ = Table1[GroupOrder] - 1
    RETURN
        CALCULATE (
            DISTINCT ( Table1[Value 1] ),
            Table1[GroupOrder] = PreviousGroupOrder_,
            ALLEXCEPT ( Table1, Table1[Date] )
        ),
    Table1[GroupOrder] IN { 3, 6, 9 },
    VAR PreviousGroupOrder2_ = Table1[GroupOrder] - 2
    RETURN
        CALCULATE (
            DISTINCT ( Table1[Value 1] ),
            Table1[GroupOrder] = PreviousGroupOrder2_,
            ALLEXCEPT ( Table1, Table1[Date] )
        )
)

View solution in original post

5 REPLIES 5
AlB
Super User
Super User

Hi @Destrogiro 

It's not clear what you need. What is the code supposed to do? Is it a measure, a calc column? Can you show a sample of your data with an example explaining the expected result?

 

Please always show your sample data in text-tabular format in addition to (or instead of) the screen captures. A screen cap doesn't allow people to readily copy the data and run a quick test and thus decreases the likelihood of your question being answered. Just use 'Copy table' in Power BI and paste it here. Or, ideally, share the pbix (beware of confidential data).

Here is an example of what I need:

 

Date       GroupOrder    Value 1               Value 2

---------------------------------------------------------------------------------------------------------------------

20121231   1             322762272703.0400     0.0000
20121231   2             504993049586.4000     322762272703.0400
20121231   3             117421034783.7100     322762272703.0400
20121231   4             725451720271.6600     0.0000
20121231   5             1527350829053.5900    725451720271.6600
20121231   6             90925867161.1700      725451720271.6600
20121231   7             405811.0000           0.0000
20121231   8             105.0000              405811.0000
20121231   9             27.0000               405811.0000

 

When GroupOrder is 1,4,7 Value 2 should be 0

When GroupOrder is (2,5,8) Value 2 should have EARLIER(Value 1,1)

When GroupOrder is (3,6,9) Value 2 should have EARLIER(Value 1,2)

 

I was able to do it in SQL Server using LAG function. I don't know if it helps, but here is the SQL code:

 

SELECT DataReferencia, OrdemGrupo, GrupoInformacao, TipoInformacao, Valor AS ValorAtual,
CASE WHEN OrdemGrupo IN (2,5,8) THEN LAG(Valor,1,0) OVER (ORDER BY DataReferencia, OrdemGrupo, GrupoInformacao, TipoInformacao)
WHEN OrdemGrupo IN (3,6,9) THEN LAG(Valor,2,0) OVER (ORDER BY DataReferencia, OrdemGrupo, GrupoInformacao, TipoInformacao)
ELSE 0
END AS ValorTotal
FROM TCBLBI_Peso_Empresas_Tradadas
WHERE DataReferencia = 20121231

 

I hope it helps.

Hi @Destrogiro 

EARLIER works in a completely different way from what you seem to be assuming. It gives you access to the outer row context, not to the previous row.  Try this for your calculated column, where I'm assuming you need this done by date:

 

Value2 =
SWITCH (
    TRUE (),
    Table1[GroupOrder] IN { 1, 4, 7 }, 0,
    Table1[GroupOrder] IN { 2, 5, 8 }, CALCULATE (
        DISTINCT ( Table1[Value 1] ),
        Table1[GroupOrder]
            = ( EARLIER ( Table1[GroupOrder] ) - 1 ),
        ALLEXCEPT ( Table1, Table1[Date] )
    ),
    Table1[GroupOrder] IN { 3, 6, 9 }, CALCULATE (
        DISTINCT ( Table1[Value 1] ),
        Table1[GroupOrder]
            = ( EARLIER ( Table1[GroupOrder] ) - 2 ),
        ALLEXCEPT ( Table1, Table1[Date] )
    )
)

 

   

 

Since the introduction of VAR, there's no actual need for EARLIER any longer.  So you might want to do it in a different way, perhaps easier to follow conceptually, and forget about EARLIER altogether (which is actually recommended nowadays):

Value2 =
SWITCH (
    TRUE (),
    Table1[GroupOrder] IN { 1, 4, 7 }, 0,
    Table1[GroupOrder] IN { 2, 5, 8 },
    VAR PreviousGroupOrder_ = Table1[GroupOrder] - 1
    RETURN
        CALCULATE (
            DISTINCT ( Table1[Value 1] ),
            Table1[GroupOrder] = PreviousGroupOrder_,
            ALLEXCEPT ( Table1, Table1[Date] )
        ),
    Table1[GroupOrder] IN { 3, 6, 9 },
    VAR PreviousGroupOrder2_ = Table1[GroupOrder] - 2
    RETURN
        CALCULATE (
            DISTINCT ( Table1[Value 1] ),
            Table1[GroupOrder] = PreviousGroupOrder2_,
            ALLEXCEPT ( Table1, Table1[Date] )
        )
)

Thank you very much, @AlB !

It solved my problem!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors