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
danielsouza
Helper III
Helper III

IF and SWITCH is not working

I'm trying to build a measure using IF or SWITCH, but DAX is not allowing me to load the tables:

 

if.JPG

 

Thanks a lot

Daniel

 

 

2 ACCEPTED SOLUTIONS

The number 1 means first element?

 

May I use this too?

 

 

 

IF(
	CONTAINS ( 'Fluxo de caixa', 'Fluxo de caixa'[Tipo], "Entrada" );
	SUM('Fluxo de caixa'[Valor]) + SUM('Fluxo de caixa'[Saldo final de caixa]);
	SUM('Fluxo de caixa'[Valor]) - SUM('Fluxo de caixa'[Saldo final de caixa])
)

 

View solution in original post

The second argument of FIRSTNONBLANK is rarely used. 1 returns a value of "True" and allows the function to work without an expression there. I think the second argument of FIRSTNONBLANK ought to be optional but I didn't write the language.

 

CONTAINS won't work the way you're expecting. Does the way I wrote it not give you the results you're looking for?

 

Try this instead:

 

CALCULATE(
	SUM('Fluxo de caixa'[Valor]) + SUM('Fluxo de caixa'[Saldo final de caixa]);
	FILTER(
		'Fluxo de caixa';
		'Fluxo de caixa'[Tipo] = "Entrada"
	)
) +
CALCULATE(
	SUM('Fluxo de caixa'[Valor]) - SUM('Fluxo de caixa'[Saldo final de caixa]);
	FILTER(
		'Fluxo de caixa';
		'Fluxo de caixa'[Tipo] <> "Entrada"
	)
)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

8 REPLIES 8
KHorseman
Community Champion
Community Champion

You can't refer to naked tables or columns in measures, you have to aggregate the data (sum, min, max, firstnonblank, etc.).





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




I see.

 

But I would like to work with string:

 

Saldo final = IF('Fluxo de caixa'[Tipo]="Entrada";SUM('Fluxo de caixa'[Valor])+SUM('Fluxo de caixa'[Saldo final de caixa]);sum('Fluxo de caixa'[Valor])-sum('Fluxo de caixa'[Saldo final de caixa]))

 

Fluxo de caixa[Tipo] only has text.

 

How can I aggregate the data?

 

Firstnonblank





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Saldo final = IF(FIRSTNONBLANK('Fluxo de caixa'[Tipo];'Fluxo de caixa'[Tipo]="Entrada");SUM('Fluxo de caixa'[Valor])+SUM('Fluxo de caixa'[Saldo final de caixa]);sum('Fluxo de caixa'[Valor])-sum('Fluxo de caixa'[Saldo final de caixa]))

 

Is not working, IF column Tipo contains "Entrada", sum value in valor and value in Saldo final de caixa...

 

Any clue?

Your syntax for FIRSTNONBLANK is slightly wrong.

 

IF(
	FIRSTNONBLANK('Fluxo de caixa'[Tipo]; 1) = "Entrada";
	SUM('Fluxo de caixa'[Valor]) + SUM('Fluxo de caixa'[Saldo final de caixa]);
	SUM('Fluxo de caixa'[Valor]) - SUM('Fluxo de caixa'[Saldo final de caixa])
)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




The number 1 means first element?

 

May I use this too?

 

 

 

IF(
	CONTAINS ( 'Fluxo de caixa', 'Fluxo de caixa'[Tipo], "Entrada" );
	SUM('Fluxo de caixa'[Valor]) + SUM('Fluxo de caixa'[Saldo final de caixa]);
	SUM('Fluxo de caixa'[Valor]) - SUM('Fluxo de caixa'[Saldo final de caixa])
)

 

The second argument of FIRSTNONBLANK is rarely used. 1 returns a value of "True" and allows the function to work without an expression there. I think the second argument of FIRSTNONBLANK ought to be optional but I didn't write the language.

 

CONTAINS won't work the way you're expecting. Does the way I wrote it not give you the results you're looking for?

 

Try this instead:

 

CALCULATE(
	SUM('Fluxo de caixa'[Valor]) + SUM('Fluxo de caixa'[Saldo final de caixa]);
	FILTER(
		'Fluxo de caixa';
		'Fluxo de caixa'[Tipo] = "Entrada"
	)
) +
CALCULATE(
	SUM('Fluxo de caixa'[Valor]) - SUM('Fluxo de caixa'[Saldo final de caixa]);
	FILTER(
		'Fluxo de caixa';
		'Fluxo de caixa'[Tipo] <> "Entrada"
	)
)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Yes, worked very well! Now I know how to use the IF statement.

 

Nice line of thought, I gonna use this last suggestion!

 

Thanks a lot!

Daniel

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.