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
Hermes
Helper I
Helper I

Wrong Grand Totals - SUMX ( VALUE ( ) ) ; does not help

Hi there,

 

I've come across situation I have trouble resolving myself. If you could take a look at my code.

 

Bussiness applications is like this; 

Our client has a quarter bonus paid based on sales value in quarter, increasing when certian treshold is achived. 

He will get 7% for every sales belowe 3000 / per quarter + 10% for sales above 3000. 

 

You can see my measure so far. It works fine on quarted level, but when it comes to grand total, sum is off.

 

Could you please point me to mistake I've made so I get proper sum for grand total?

 

 

retroProg.:=
VAR __WartZaOkr = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR __RetroProgi = 
	IF( __WartZaOkr <= 3000 ; 
		__WartZaOkr * 0,07 ; 
		( 3000 * 0,07 ) + ( ( __WartZaOkr - 3000 ) * 0,1 ) 
	) 

RETURN
__RetroProgi

 

 

1.PNG

 

2 ACCEPTED SOLUTIONS
tex628
Community Champion
Community Champion

This should give you the correct total when you use it in the matrix together with the Quarter dimension. It's long and messy but it should work. If you want something more dynamic you will need to implement the weights (0,07 & 0,1) as calculated columns. But i cant really propose any solution there without seeing more of your datamodel. 

 

retroProg.:=
VAR __WartZaOkr = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR __RetroProgi = 
	IF( __WartZaOkr <= 3000 ; 
		__WartZaOkr * 0,07 ; 
		( 3000 * 0,07 ) + ( ( __WartZaOkr - 3000 ) * 0,1 ) 
	)

VAR Q1 =
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 1" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Q2 = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 2" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Q3 = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 3" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Q4 =  
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 4" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Total = 
	IF( Q1 <= 3000 ; 
		Q1 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q1 - 3000 ) * 0,1 ) 
	) +

	IF( Q2 <= 3000 ; 
		Q2 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q2 - 3000 ) * 0,1 ) 
	) +

	IF( Q3 <= 3000 ; 
		Q3 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q3 - 3000 ) * 0,1 ) 
	) +

	IF( Q4 <= 3000 ; 
		Q4 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q4 - 3000 ) * 0,1 ) 
	)

RETURN
IF( SELECTEDVALUE(Kalendarz[Kwartał roku]) = BLANK() ;
Total ;
__RetroProgi

 


Connect on LinkedIn

View solution in original post

tex628
Community Champion
Community Champion

Try this and see if it works:

retroProg.:=
VAR __WartZaOkr = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		IF(
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) <= 3000 ;
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) * 0,07 ;
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) * 0,1
		)
	)
Return
__WartZaOkr


The reason the iteration isnt working is because we are iterating and summarizing first and then applying the multiplication after! I don't know if the code im providing works but it's worth a try! 

Br,
J


Connect on LinkedIn

View solution in original post

6 REPLIES 6
tex628
Community Champion
Community Champion

Hello @Hermes,

When the application tries to calculate the "Total" it's not adding Q1 + Q2 +Q3 etc. Instead what it's actually doing is simply running the same calculation but without any filters on quarter. 

As an example, when the measure is calculated on the first row you can pretty much imagine that there is a "Q1" filter covering the entire measure. 

When the calculation for the total is occuring there is no quarter filter, therefore the total amount in the "__WartZaOkr" is way over 3000 and the entire amount will be multiplied with 0,1 instead of a combination of 0,07 & 0,1. 

In terms of how to solve this it kind of depends on how your report looks. Is retroProg. always displayed together with quarter?

Br,
J


Connect on LinkedIn

Hello, 

 

There are many ways this raport could be displayed and I would like not to restrain myself. This is why I'v included ALL on clients and products tables in first VAR. In fact, this is only part of calculation, because in next step I calculate de facto bonus percentage no matter the filters (i.e. on product level) and calculate bonus from this filtered sales * obtained percentage. 

 

As I've said, it works greate on any level I've chcecked except for sums for more than one quarter.

tex628
Community Champion
Community Champion

This should give you the correct total when you use it in the matrix together with the Quarter dimension. It's long and messy but it should work. If you want something more dynamic you will need to implement the weights (0,07 & 0,1) as calculated columns. But i cant really propose any solution there without seeing more of your datamodel. 

 

retroProg.:=
VAR __WartZaOkr = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR __RetroProgi = 
	IF( __WartZaOkr <= 3000 ; 
		__WartZaOkr * 0,07 ; 
		( 3000 * 0,07 ) + ( ( __WartZaOkr - 3000 ) * 0,1 ) 
	)

VAR Q1 =
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 1" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Q2 = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 2" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Q3 = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 3" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Q4 =  
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			Kalendarz ; Kalendarz[Kwartał roku] = "kwart. 4" ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) 
	)

VAR Total = 
	IF( Q1 <= 3000 ; 
		Q1 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q1 - 3000 ) * 0,1 ) 
	) +

	IF( Q2 <= 3000 ; 
		Q2 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q2 - 3000 ) * 0,1 ) 
	) +

	IF( Q3 <= 3000 ; 
		Q3 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q3 - 3000 ) * 0,1 ) 
	) +

	IF( Q4 <= 3000 ; 
		Q4 * 0,07 ; 
		( 3000 * 0,07 ) + ( ( Q4 - 3000 ) * 0,1 ) 
	)

RETURN
IF( SELECTEDVALUE(Kalendarz[Kwartał roku]) = BLANK() ;
Total ;
__RetroProgi

 


Connect on LinkedIn

Heh, I've market this solution as valid - and so it is 🙂 ... but I've realise that there are still wrong grand totals calculated whenever where is more than one year selected in filter. Each calculation for quarters become invalid, because in two years there are 8 quarters and for all of theme there should be separate calculation made - and right now there is one for each quarted combined times numeber of years displayed. Yet, there shoud be calculation for each year-quarter combination made separatly. But I have no idea how to go about it, and I still don't get why I can virtualy iterate over year-quarted with SUMX ( VALUES ( [year-quarter] ) )?

tex628
Community Champion
Community Champion

Try this and see if it works:

retroProg.:=
VAR __WartZaOkr = 
	SUMX( 
		VALUES( Kalendarz[Kwartał roku] ) ; 
		IF(
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) <= 3000 ;
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) * 0,07 ;
		CALCULATE( 
			SUMX( 
				FILTER( 'Sprzedaż │ Elementy' ; RELATED( 'Kontrahenci │ Wymiary'[Grupa] ) = "CLIENTX" ) ; 
				[Sprzedaż | Wartość] 
				) ; 
			ALLEXCEPT( Kalendarz ; Kalendarz[Kwartał roku] ) ; 
			ALL( 'Kontrahenci │ Wymiary' ) ; 
			ALL( 'Towary │ Wymiary' ) 
		) * 0,1
		)
	)
Return
__WartZaOkr


The reason the iteration isnt working is because we are iterating and summarizing first and then applying the multiplication after! I don't know if the code im providing works but it's worth a try! 

Br,
J


Connect on LinkedIn

This did the trick! Thank you very much. 🙂

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.