cancel
Showing results for
Did you mean:
Regular Visitor

## EARLIER Function

Hi,

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

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
Super User

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] )
)
)
```
5 REPLIES 5
Super User

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

Regular Visitor

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
WHERE DataReferencia = 20121231

I hope it helps.

Super User

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] )
)
)
```

Super User

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] )
)
)
```
Regular Visitor

Thank you very much, @AlB !

It solved my problem!

Announcements

#### Join Arun Ulag at MPPC23

Get a sneak peek into this year's Power Platform Conference Keynote.

#### Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

#### Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

#### Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Top Solution Authors
Top Kudoed Authors