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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.