Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
wini_R
Solution Supplier
Solution Supplier

DAX measure to calculate count of average values in virtual table

Hi Everyone,

I'd need to calculate the count of average values and find the mode as an ultimate goal. Let's consider this simple table as our source data:

deliv datetarget
2022011019
2022011456
2022011482
2022011544
2022012313
2022012325
2022020272
2022020572
2022020920
2022020973
2022020930
2022021072
2022021072
2022021921
2022022341
2022022319
2022030283
2022030562
2022030577
2022030577

 

1. As a first step I need to group data by date and calculate the average of 'target'. I can achieve this by using SUMMARIZE function:

wini_R_0-1709733873604.png

2. Then based on this virtual table I'd like to group by '@avg' column and get the count for each value. This should be the output:

wini_R_1-1709734117856.png

When I have this table it should be fairly simple to find the mode which is 72 in this case.

The issue is that I cannot calculate the count based on the virtual table from step 1 ('_aggTab'). I tried to use the same logic with summarize but you cannot use count on virtual columns and I couldn't get countx to give correct result:

wini_R_2-1709734641313.png

What would be the correct DAX count formula to get the output as in point 2?

1 ACCEPTED SOLUTION
wini_R
Solution Supplier
Solution Supplier

It's a very basic calculation, why would we need extra tables and complex logic to calculate it?

 

After some exploration I've foud 2 ways to calculate what I need:

1. using SUMMARIZE and COUNTX functions:

EVALUATE
VAR _tab1 =
ADDCOLUMNS(
    SUMMARIZE(tabB, tabB[deliv date])
	, "@avg", CALCULATE(AVERAGE(tabB[target]))
)
VAR _tab2 =
ADDCOLUMNS(
	SUMMARIZE(_tab1, [@avg]),
	"count of avg",
	COUNTX(
		FILTER(_tab1, [@avg] = EARLIER([@avg])),
		[@avg]
	)
)
RETURN _tab2

Output table:

wini_R_0-1709999055054.png

2. using GROUPBY and COUNTX functions:

EVALUATE
VAR _tab1 =
GROUPBY(
	tabB,tabB[deliv date],
	"@avg", AVERAGEX(CURRENTGROUP(), tabB[target])
)
VAR _tab2 =
GROUPBY(
	_tab1, [@avg],
	"count of avg", COUNTX(CURRENTGROUP(), [@avg])
)
RETURN _tab2

The output is same as above.

 

Then if we need to calculate mode we can do this using i.e. MINX and either INDEX or TOPN by adding another variable to one of the expressions above:

MINX(
	INDEX(1, _tab2, ORDERBY([count of avg], DESC)),
	[@avg]
)

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @wini_R ,

Based on your problems, here are my answers.

I create a table as you mentioned.

vyilongmsft_0-1709797530465.png

I write a new Measure that utilizes the COUNTX, SUMMARIZE and SUMX functions.

Measure =
SUMX (
    SUMMARIZE (
        'Table',
        'Table'[target],
        "Count", COUNTX ( 'Table', 'Table'[target] )
    ),
    [Count]
)

Finally, you will get what you want.

vyilongmsft_1-1709798278982.png

 

 

 

Best Regards

Yilong Zhou

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks @Anonymous for the reply. However it seems that your solution is missing one step - instead of calculating the count based on aggregated average values (see step 1 in the initial post), it  calculates the count on raw data which gives different result. 

Do you think it would be possible to modify your solution according to the rquirement?

Ahmedx
Super User
Super User

pls try this

Screenshot_2.pngScreenshot_3.pngScreenshot_4.png

wini_R
Solution Supplier
Solution Supplier

Thanks @Ahmedx for this solution. It appears to work fine, however it adds some complexity to the model by introducing additional table and I'd prefer to avoid it for that relatively simple task. I'm sure this can be done using just the existing table.

It won't work without an additional table

wini_R
Solution Supplier
Solution Supplier

It's a very basic calculation, why would we need extra tables and complex logic to calculate it?

 

After some exploration I've foud 2 ways to calculate what I need:

1. using SUMMARIZE and COUNTX functions:

EVALUATE
VAR _tab1 =
ADDCOLUMNS(
    SUMMARIZE(tabB, tabB[deliv date])
	, "@avg", CALCULATE(AVERAGE(tabB[target]))
)
VAR _tab2 =
ADDCOLUMNS(
	SUMMARIZE(_tab1, [@avg]),
	"count of avg",
	COUNTX(
		FILTER(_tab1, [@avg] = EARLIER([@avg])),
		[@avg]
	)
)
RETURN _tab2

Output table:

wini_R_0-1709999055054.png

2. using GROUPBY and COUNTX functions:

EVALUATE
VAR _tab1 =
GROUPBY(
	tabB,tabB[deliv date],
	"@avg", AVERAGEX(CURRENTGROUP(), tabB[target])
)
VAR _tab2 =
GROUPBY(
	_tab1, [@avg],
	"count of avg", COUNTX(CURRENTGROUP(), [@avg])
)
RETURN _tab2

The output is same as above.

 

Then if we need to calculate mode we can do this using i.e. MINX and either INDEX or TOPN by adding another variable to one of the expressions above:

MINX(
	INDEX(1, _tab2, ORDERBY([count of avg], DESC)),
	[@avg]
)

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors