Forum Discussion
Dax Average with Distinct Filter
Hi All,
I'm trying to filter a table (have provided a mock one below) where I filter by Distinct and a result. As you can see, the Cycle time will be the same for every ID but I only want to Average each ID once. If I Average the entire table below, the result is 21.45. If I Average only one instance of each ID, the result is 19. 19 is the value I need.
My Distinct filter doesn't seem to be doing anything. I have tried multiple variations of my below query but this is the one that makes the most sense to me but i just can;t work out why it's not giving me what i want.
Cycle Time Average:=CALCULATE(AVERAGE('Cycle Time'[CycleTime]),DISTINCT('Cycle Time'[ID]),'Cycle Time'[Result]<>"N/A")
Table is Called "Cycle Time"
| ID | CycleTime | Result |
| 1 | 10 | N/A |
| 1 | 10 | Pass |
| 1 | 10 | Fail |
| 1 | 10 | N/A |
| 2 | 34 | N/A |
| 2 | 34 | Pass |
| 2 | 34 | Fail |
| 2 | 34 | Fail |
| 2 | 34 | Fail |
| 3 | 13 | Fail |
| 3 | 13 | N/A |
Thank you
- Anonymous6 years ago
Hi Erik_Debono -
Try the following:
Cycle Time Average = var _table = DISTINCT( SELECTCOLUMNS( FILTER('Cycle Time', [Result]<>"N/A"), "ID", [ID], "CycleTime", [CycleTime] ) ) return AVERAGEX(_table, [CycleTime])
7 Replies
- AnonymousNot applicable
Hi Erik_Debono -
Try the following:
Cycle Time Average = var _table = DISTINCT( SELECTCOLUMNS( FILTER('Cycle Time', [Result]<>"N/A"), "ID", [ID], "CycleTime", [CycleTime] ) ) return AVERAGEX(_table, [CycleTime])- Erik_DebonoAdvocate II
Hi Anonymous
Thank you for that. I tried your formula and i'm getting underline errors for the following:
- Name 2 [ID] on line 5
- Name 3 [CycleTime] on line 6 and
- Expression [CycleTime] on the last line.
The overall error is "Function SELECTCOLUMNS expects a column name as argument number 4"
- Nathaniel_CCommunity Champion
Hi Erik_Debono , Anonymous ,
I built a table using copy and paste from your numbers, and it works for me.
Nathaniel
- Erik_DebonoAdvocate II
Hi Anonymous Nathaniel_C
Error was self inflicted, your solution worked great, thank you for that. Have marked as Solved. Are you able to explain what's happening here? i'm not following how it works.
amitchandak Thanks for your solution. I didn't get around to checking if it would work in this case.
- AnonymousNot applicable
Sure, it is basically 2 steps:
- Create a table of the distinct values:
- FILTER limits table to eliminate N/A
- SELECTCOLUMNS chooses the 2 columns we need to run the distinct on.
- DISTINCT returns the distinct values
var _table = DISTINCT( SELECTCOLUMNS( FILTER('Cycle Time', [Result]<>"N/A"), "ID", [ID], "CycleTime", [CycleTime] ) )
- Get the average of the CycleTimes from the distinct table.
- Use AVERAGEX to work with Table Variables.
return AVERAGEX(_table, [CycleTime]) - Create a table of the distinct values:
- amitchandakSuper User
This seems like very similar to sum of Averages problem
Refer
https://community.powerbi.com/t5/Desktop/SUM-of-AVERAGE/td-p/197013
In the calculation, you can replace Average with Max or Average. replace sum with Average
Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.
My Recent Blog - https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601