Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
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.
Solved! Go to 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] ) ) )
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] ) ) )
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
25 | |
18 | |
17 | |
17 | |
15 |
User | Count |
---|---|
28 | |
27 | |
18 | |
14 | |
13 |