Forum Discussion
Destrogiro
7 years agoRegular 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([Va...
- 7 years ago
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] ) ) )
AlB
Community Champion
7 years agoHi 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] )
)
)
AlB
Community Champion
7 years agoSince 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] )
)
)