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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DSchmndtk
Regular Visitor

measure with function like TRIMMEAN of excel

Hello I am looking for a code like the trimmean function in excel.
The previvous posts i checked didn't help me finding a solution.
Maybe someone can help me with an kind of sample code and what i have to replave inside of the code...

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @DSchmndtk ,
Here some steps that I want to share, you can check them if they suitable for your requirement.
Here is my test data:

vheqmsft_0-1714379494157.png

Create a Rank column

Rank = RANKX('Table',[Value],,ASC)

Create a measure

Trimmed Mean = 
VAR _percentage = 0.3
VAR _startRank = FLOOR(COUNTROWS('Table')*_percentage/2+1,1)
VAR _endRank = COUNTROWS('Table')*(1-FLOOR(_percentage/2,0.1))
VAR _sum = 
CALCULATE(
    SUM('Table'[Value]),
    FILTER(
        'Table',
        'Table'[Rank] <=_endRank && 'Table'[Rank] >= _startRank
    )
)
VAR _count = COUNTROWS('Table')*(1-FLOOR(_percentage/2,0.1)*2)
RETURN
_sum/_count

Final output

vheqmsft_1-1714379560261.png

Best regards,

Albert He

 

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

 

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @DSchmndtk ,
Here some steps that I want to share, you can check them if they suitable for your requirement.
Here is my test data:

vheqmsft_0-1714379494157.png

Create a Rank column

Rank = RANKX('Table',[Value],,ASC)

Create a measure

Trimmed Mean = 
VAR _percentage = 0.3
VAR _startRank = FLOOR(COUNTROWS('Table')*_percentage/2+1,1)
VAR _endRank = COUNTROWS('Table')*(1-FLOOR(_percentage/2,0.1))
VAR _sum = 
CALCULATE(
    SUM('Table'[Value]),
    FILTER(
        'Table',
        'Table'[Rank] <=_endRank && 'Table'[Rank] >= _startRank
    )
)
VAR _count = COUNTROWS('Table')*(1-FLOOR(_percentage/2,0.1)*2)
RETURN
_sum/_count

Final output

vheqmsft_1-1714379560261.png

Best regards,

Albert He

 

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

 

 

123abc
Community Champion
Community Champion

In Power BI, you can replicate the TRIMMEAN function using DAX (Data Analysis Expressions). Below is a DAX formula to calculate the trimmean:

 

TrimMean =
VAR Data = SUMMARIZE('Table', 'Table'[Value])
VAR Percent = 0.2 -- Change this to the desired percentage
VAR Count = COUNTROWS(Data)
VAR TrimCount = INT(Count * Percent)
RETURN
AVERAGEX(
TOPN(Count - 2 * TrimCount, Data, 'Table'[Value], ASC),
'Table'[Value]
)

 

Here's what you need to do:

  1. Replace 'Table' with the name of your table.
  2. Replace 'Table'[Value] with the column that contains the values you want to calculate the trimmean for.
  3. Change the Percent variable to the desired percentage.

Here's a step-by-step guide:

  1. Open your Power BI Desktop.
  2. In the "Modeling" tab, click on "New Measure".
  3. Paste the DAX formula provided above and adjust it according to your data.

For example, if you have a table named Sales and a column named Amount, your DAX formula would look like this:

 

TrimMean =
VAR Data = SUMMARIZE('Sales', 'Sales'[Amount])
VAR Percent = 0.2 -- Change this to the desired percentage
VAR Count = COUNTROWS(Data)
VAR TrimCount = INT(Count * Percent)
RETURN
AVERAGEX(
TOPN(Count - 2 * TrimCount, Data, 'Sales'[Amount], ASC),
'Sales'[Amount]
)

 

This DAX formula creates a new measure named TrimMean that calculates the trimmean of the Amount column in the Sales table, removing 20% of the data from the top and bottom tails.

You can then use this TrimMean measure in your reports and visuals just like any other measure.

 

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

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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