Forum Discussion
Replicating TRIMMEAN in DAX
- 6 years ago
So, for the record, I actually solved this finally.
https://community.powerbi.com/t5/Quick-Measures-Gallery/TRIMMEAN/m-p/1074075
I stumbled across this old thread when posting that Quick Measure! 🙂
ImkeF - You might like this as I believe it resolves the issues with ties, which is what drove me nuts trying to solve!!
Thanks! In fact your answer made me realise that I was misunderstanding how TRIMMEAN works, I thought it was excluding all results that sit outside a certain range, whereas it's just excluding a percentage of the results. That hopefully makes things simpler. I'll try with RANK and see how I get on. I'll probably be posting a follow up question!
Since this post is one of the top results when searching for "TRIMMEAN in DAX" ... and hasn't yet been resolved, I wanted to point out this blog post that helps you get RANKX() to keep the filter context. I believe this is necessary to get a relative TRIMMEAN in DAX working. I strugged with this for awhile, but this blog post helps a lot.
https://ayadshammout.com/2013/02/19/dax-rankx-function-scenarios/
Once the RANKX is working (it was odd me at first that it is a measure, but if you test the measure using a pivot table, you can verify that yours is working).... I then plan to roughly follow the methodology in the next link. I'm not actually sure if it will get the job done. I'll try and report back.
- Greg_Deckler6 years ago
Community Champion
So, for the record, I actually solved this finally.
https://community.powerbi.com/t5/Quick-Measures-Gallery/TRIMMEAN/m-p/1074075
I stumbled across this old thread when posting that Quick Measure! 🙂
ImkeF - You might like this as I believe it resolves the issues with ties, which is what drove me nuts trying to solve!!
- ImkeF6 years ago
Community Champion
Hats off on that one Greg_Deckler !
Just to make you jealous how easy it would have been on the bright side 😉 :
// TRIMMEAN on the bright side :) let Source = Table.Buffer( Table.Sort(Array,{{"Value", Order.Ascending}}) ), CountOfTotalRows = Table.RowCount( Source ), CutOffAtEachSide = Number.RoundDown((percentage * CountOfTotalRows) / 2), RelevantRange = Table.Range( Source, CutOffAtEachSide, CountOfTotalRows - CutOffAtEachSide * 2), Result = List.Average( RelevantRange [Value] ) in Resultor the functionized version that accepts the same arguments as the Excel-function ( an array (list) and a percentage (scalar) :
(ListOfValues, Percentage) => let Source = List.Buffer( List.Sort(ListOfValues, Order.Ascending) ), CountOfTotalRows = List.Count( Source ), CutOffAtEachSide = Number.RoundDown((Percentage * CountOfTotalRows) / 2), RelevantRange = List.Range( Source, CutOffAtEachSide, CountOfTotalRows - CutOffAtEachSide * 2), Result = List.Average( RelevantRange) in Result- Greg_Deckler6 years ago
Community Champion
Very nice ImkeF ! Maybe you should do a blog series on Excel to M Translation?? 🙂
- Kat_Finra2 years agoFrequent Visitor
Hi Greg, Thank yu for posting this! I have a table with repeating values at the top and bottom that PBI might be droping out of the set. When I compate the 10% TRIMMEAN from exce to PBI version I get different results 😕