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

AVERAGEX() not working properly on a VAR Table

Hi, 

 

I've created a measure using GENERATE() and ROW() to make a VAR table that has the outputs I want to average/retrieve in the RETURN section.

The resulting var table looks like this in DAX Studio:

 

BachDinh_2-1676621666243.png

And this is the full measure:

 

 

Range Index = 
var Year_now = YEAR(Today())     
var base_table =

SUMMARIZE(
	CALCULATETABLE(
		'Daily Sales'
		,'Date'[Year] = Year_now)
    ,Kiosks[Country]
    ,Kiosks[Region]
	,Kiosks[Kiosk]
	,'Date'[Date]
	,'Date'[DayName]
)
//below generates a table with desired kpi values
var tbl = 
GENERATE(
	base_table, 
	VAR cluster_type = 
		CALCULATE(
		        SWITCH(
		            TRUE()
		            ,[Total Sales Budget] > 0 && [Total Sales Budget] < 250000, "1"
		            ,[Total Sales Budget] > 250000 && [Total Sales Budget] < 350000, "2"
		            ,[Total Sales Budget] > 350000 && [Total Sales Budget] < 450000, "3"
		            ,[Total Sales Budget] > 450000 && [Total Sales Budget] < 550000, "4"
		            ,[Total Sales Budget] > 550000 && [Total Sales Budget] < 800000, "5"
		            ,[Total Sales Budget] > 800000 && [Total Sales Budget] < 2000000, "6"
		        )
		       ,'Date'[Year] = Year_now
		       ,ALL('Date')
		)
	var cluster_averages = 
        CALCULATE(
		    SWITCH(
		        TRUE()
		        ,cluster_type = "1", 175000
		        ,cluster_type = "2", 300000
		        ,cluster_type = "3", 400000
		        ,cluster_type = "4", 500000
		        ,cluster_type = "5", 675000
		        ,cluster_type = "6", 1400000
		    )
		)
	var cluster_target_weekday = 
		CALCULATE(
		    SWITCH(
		        TRUE()
		        ,cluster_type = "1", 35
		        ,cluster_type = "2", 45
		        ,cluster_type = "3", 55
		        ,cluster_type = "4", 55
		        ,cluster_type = "5", 60
		        ,cluster_type = "6", 60
		    )
		)
	var cluster_target_weekend = 
		CALCULATE(
		    SWITCH(
		        TRUE()
		        ,cluster_type = "1", 40
		        ,cluster_type = "2", 50
		        ,cluster_type = "3", 60
		        ,cluster_type = "4", 60
		        ,cluster_type = "5", 65
		        ,cluster_type = "6", 70
		    )
		)
		
	var product_count = 
    CALCULATE(
        DISTINCTCOUNT('Daily Sales'[Products_SK])
        ,'Daily Sales'[Qty - Production] > 0
        ,'Date'[Year] = SELECTEDVALUE('Date'[Year])
        
    )
    var cluster_target = 
	    IF(
			'Date'[DayName] IN {"Sunday","Monday","Tuesday","Wednesday","Thursday"}
			,cluster_target_weekday
			,cluster_target_weekend
			)
			
    var range_vs_benchmark =
		IF(
		    DIVIDE(
		        product_count
		        ,
		        cluster_target
		    ) > 1
		    ,1
		    ,DIVIDE(
		        product_count
		        ,
		        cluster_target
		    )
		)
    
	RETURN
	ROW(
	"cluster", 
		cluster_type,
	"target", 
		cluster_target,
	"production",
		product_count,
	"range index",
		range_vs_benchmark
	)
)
RETURN
AVERAGEX(
    tbl
    ,[range index]
)

 

 

However,  the output is incorrect - sometimes the values are right, sometimes they are wrong and sometimes they don't appear at all - by swaping the [expression] part of AVERAGEX(), I figured that this is because of the [target] column of the VAR table. 

Trying to use AVERAGEX(tbl, [target[) returns the below:

BachDinh_3-1676621972351.png

What could be the reason for this? 

 

1 REPLY 1
lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely.
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

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.