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
Pfandgiraffe
Regular Visitor

IF combined with a CALCULATE function

Hey,

 

i want to combine a IF-question with a CALCULATE function.

First i want to compare two values. Depend on the result i want to use the one or the other formular with the calculate function. It will work well but in some cases the result isn't what i espected and i don't understand why.

 

Goal is to calculate the inventory development. If the target number is bigger than the sales order, use the target number. Otherwise use the summary of al sales orders to calculate the inventory.

 

Bestandsentwicklung (Absatzplan/VK Aufträge) = if (sum('SCM ProdForecastEntry'[Absatzplanung]) > sum('VK OffeneAuftraege'[Restmenge])

 

 

 

Here is the hole code:

Bestandsentwicklung (Absatzplan/VK Aufträge) = 
if (sum('SCM ProdForecastEntry'[Absatzplanung]) > sum('VK OffeneAuftraege'[Restmenge]), 

CALCULATE(sum('SCM Artikelbestand'[Menge])+sum('SCM FA-Zeile'[Restmenge])-sum('SCM ProdForecastEntry'[Absatzplanung]),
	FILTER(
		ALLSELECTED('Datumstabelle'[Date]),
		ISONORAFTER('Datumstabelle'[Date], MAX('Datumstabelle'[Date]), DESC)
	)), 

CALCULATE(sum('SCM Artikelbestand'[Menge])+sum('SCM FA-Zeile'[Restmenge])-sum('VK OffeneAuftraege'[Restmenge]),
	FILTER(
		ALLSELECTED('Datumstabelle'[Date]),
		ISONORAFTER('Datumstabelle'[Date], MAX('Datumstabelle'[Date]), DESC)
	))
)

 

 

As i told, result is fine in this example:

Pfandgiraffe_0-1673615430159.png

 

 

But for some other products it will look like this:

Result for october makes no sence...

The only different i can see is that sum('SCM ProdForecastEntry'[Absatzplanung]) is the same like sum('VK OffeneAuftraege'[Restmenge]). (both values are 65)

Pfandgiraffe_1-1673615497670.png

 

Hope somebody have an idea whats going wrong in my syntax.

If i use the CALCULATE Syntax for his own (without the if-comparison) everything works great.

 

 

 

Thanks in advance!

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

refactor your measure to isolate the individual computations.  

 

Bestandsentwicklung (Absatzplan/VK Aufträge) = 
var f = FILTER(
		ALLSELECTED('Datumstabelle'[Date]),
		ISONORAFTER('Datumstabelle'[Date], MAX('Datumstabelle'[Date]), DESC)
	)
var a = CALCULATE(sum('SCM Artikelbestand'[Menge])+sum('SCM FA-Zeile'[Restmenge])-sum('SCM ProdForecastEntry'[Absatzplanung]),f)
var b = CALCULATE(sum('SCM Artikelbestand'[Menge])+sum('SCM FA-Zeile'[Restmenge])-sum('VK OffeneAuftraege'[Restmenge]),f)
return if (sum('SCM ProdForecastEntry'[Absatzplanung]) > sum('VK OffeneAuftraege'[Restmenge]),a,b) 

 

That way it is easier to troubleshoot. 

Also - read about EVALUATEANDLOG - it's a game changer.

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

refactor your measure to isolate the individual computations.  

 

Bestandsentwicklung (Absatzplan/VK Aufträge) = 
var f = FILTER(
		ALLSELECTED('Datumstabelle'[Date]),
		ISONORAFTER('Datumstabelle'[Date], MAX('Datumstabelle'[Date]), DESC)
	)
var a = CALCULATE(sum('SCM Artikelbestand'[Menge])+sum('SCM FA-Zeile'[Restmenge])-sum('SCM ProdForecastEntry'[Absatzplanung]),f)
var b = CALCULATE(sum('SCM Artikelbestand'[Menge])+sum('SCM FA-Zeile'[Restmenge])-sum('VK OffeneAuftraege'[Restmenge]),f)
return if (sum('SCM ProdForecastEntry'[Absatzplanung]) > sum('VK OffeneAuftraege'[Restmenge]),a,b) 

 

That way it is easier to troubleshoot. 

Also - read about EVALUATEANDLOG - it's a game changer.

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.

Top Solution Authors