cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Regular Visitor

## problem with calculating the average using variables defining a range of values

Hello
I am a beginner DAX user and I have a serious problem with calculating the average using variables defining a range of values.
The problem is as follows:

Table: 'public Measurement'

1. average value for the entire range (10 results) as a separate column - works well

AV_N =

CALCULATE (

AVERAGE ( 'public Measurement'[result]);

FILTER (

ALLSELECTED ('public Measurement');

'public Measurement'[characteristicId] = MAX ( 'public Measurement'[characteristicId])

)

)

The result of the measure is as follows

2. average value for 5 results as a separate column - it seems to work well but the value range is entered "manually"

AV5 =
CALCULATE (
AVERAGEX('public Measurement';  'public Measurement'[result]);
FILTER (
ALLSELECTED ('public Measurement');
'public Measurement'[characteristicId] = MAX ( 'public Measurement'[characteristicId])
);
'public Measurement'[number] >= 1 ; 'public Measurement'[number] <= 5
)

average value from a range using variables (the measure doesn't work, why?)

Instead of constant values defining the average calculation range (1 and 5 ), I need to use variables.
the variable defining the final range of the value must be calculated, for example:
var stop = count('public measurement'[number]) (instead of the value 5)
How to use variables in such a measure?

1 ACCEPTED SOLUTION
Regular Visitor

welcome back
Thank you for your suggestions, it allowed me to locate the problem.
Instead of count() I used the countx() function and it works very well
thank you again

var stop_av = CALCULATE(
COUNTX('public Measurement';'public Measurement'[number]);
FILTER(
ALLSELECTED('public Measurement');
'public Measurement'[characteristicId] = MAX('public Measurement'[characteristicId])
)
)
5 REPLIES 5
Regular Visitor

welcome back
Thank you for your suggestions, it allowed me to locate the problem.
Instead of count() I used the countx() function and it works very well
thank you again

var stop_av = CALCULATE(
COUNTX('public Measurement';'public Measurement'[number]);
FILTER(
ALLSELECTED('public Measurement');
'public Measurement'[characteristicId] = MAX('public Measurement'[characteristicId])
)
)
Super User

There shouldn't be any difference between COUNT and COUNTX. But your new solution made me understand that the number-Characteristicid combination in the source table is unique.

Also COUNT/COUNTX should work properly only if [number] restarts from 1 for each characteristicid.
note: the 'public Measurement'[number] >= start_av a gument is not needed in all cases.

Super User

Hi @Zbig62

as part of the debugging process you can create a measure Test = count('public measurement'[number]) and see if returns what you've expected it to return.

Regular Visitor

Hello
Thanks for fast reaction.

This is where I have a problem, see the results in the table where the stop_av variable should have the value 10.
As a result, it obtains a bad average value

AV =
var stop_av = count('public Measurement'[number])
var  start_av = 1
var av =
CALCULATE (
AVERAGEX('public Measurement';  'public Measurement'[result]);
FILTER (
ALLSELECTED ('public Measurement');
'public Measurement'[characteristicId] = MAX ( 'public Measurement'[characteristicId])
);
'public Measurement'[number] >= start_av ; 'public Measurement'[number] <= stop_av
)
RETURN
av

Super User

Thank you for clarifying your problem one more time. That was clear to me already. My point was that it doesn't seem that you are aware of what var stop_av = count('public Measurement'[number]) returns. You only have an expectation that it should always return 10 while this is not true. In fact this expression will be evaluated within the filter context of evaluation. It n other words it might return different number for each number-characteristicid combination depending how many times this combination is repeated in the Source data table which I personally don't know for lack of information. But I can say it won't be 10 for all combinations unless all combinations have actually 10 rows. If you want to return the maximum [number] for each [caharastaristid] then you can try

var stop_av = CALCULATE ( MAX ( 'public Measurement'[number] ), FILTER (  ALLSELECTED ( 'public Measurement',

'public Measurement'[characteristicId]= MAX ( 'public Measurement'[characteristicId])
)

However, that might not be it as you might face issues with AUTOEXIST. Along with ALLSELECTED, AUTOEXIST might produce problems given that [Result] column is a value column that is part of the filter context.
If you can provide a sample file, it would be much easier to propose a totally different solution perhaps utilizing a NO CALCULATE approach that proofs to he much more reliably in such scenarios.

Announcements

#### Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.