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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Kevboy_telford
Frequent Visitor

Filtered average

Morning

 

I am new to Power BI but trying to build a Top 10 Margin Rank (both good and bad) however I want to only include those products where we have sold at least £100 in the selected time period (to try and get rid of any noise of single items being sold as a lost lead)

 

Main table has SKU, margin value and sales values so currently using the following measures (which isnt working as expected...)


total sales ex vat   = sum('solditems'[sale_value])
total margin ex vat  = sum('solditems'[margin_value])
Filtered avg margin  = CALCULATE(DIVIDE([total margin ex vat],[total sales ex vat]),FILTER('solditems',[total sales ex vat]>100))
Margin rank sales Good  = RANKX(ALLSELECTED('solditems'[SKU]),([Filtered avg margin]),,desc,Dense)
Margin rank sales Bad  = RANKX(ALLSELECTED('solditems'[SKU]),([Filtered avg margin]),,asc,Dense)

 

All I get back is 2 items and I expected at least 100!

 

NB to do it in SQL is
select
 SKU,
 avg(margin_value/sale_value) as margin_ratio,
 sum(sale_value)     as total_sales,
 count(*)      as amount
from solditems
where sale_value>0
group by SKU
having sum(sale_value)>100

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Kevboy_telford,

In this scenario, just reserve your measures posted in your orginal post, just change the DAX formula of Filtered avg margin into:

Filtered avg margin = IF([total sales ex vat]>100, [total margin ex vat]/[total sales ex vat],BLANK())

And create a Date column using the following DAX and create a slicer using the Date field.
Date = DATE(YEAR(solditems[SoldDate]),MONTH(solditems[SoldDate]),DAY(solditems[SoldDate]))

At last, set Filtered avg margin in Visual level filters to "not blank".

Thanks,
Lydia Zhang

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @Kevboy_telford

I am not sure what is your expected result. I make a simple test using the following sample data.
1.PNG

Firstly, create two new tables using DAX below.

Table = SUMMARIZE(solditems,solditems[SKU],"sumMari",SUM(solditems[margin_value]),"sumsales",SUM(solditems[sale_value]))
Table2 = CALCULATETABLE('Table','Table'[sumsales]>100)

Secondly, create new columns in Table2 using the DAX below.

filter avg margin = 'Table2'[sumMari]/'Table2'[sumsales]
Margin rank sales Good = RANKX('Table2',(Table2[filter avg margin]),,desc,Dense)
Margin rank sales Bad = RANKX(Table2,(Table2[filter avg margin]),,asc,Dense)

If the above DAX formulas don't help, please share sample data of your table and post expected result  here.


Thanks,
Lydia Zhang

Thanks Lydia

 

Its really close to what I want  --  just need to be able to filter on a date (which I realised I didnt mention at the beginning) - basically letting users slice a date to find out which items were best/worse performing (but only showing those that sold over £100 in that period)

Outcome.JPG

example data

 

Test_data.JPG

 

thanks for your time on this

 

cheers


Kevin

Anonymous
Not applicable

Hi @Kevboy_telford,

In this scenario, just reserve your measures posted in your orginal post, just change the DAX formula of Filtered avg margin into:

Filtered avg margin = IF([total sales ex vat]>100, [total margin ex vat]/[total sales ex vat],BLANK())

And create a Date column using the following DAX and create a slicer using the Date field.
Date = DATE(YEAR(solditems[SoldDate]),MONTH(solditems[SoldDate]),DAY(solditems[SoldDate]))

At last, set Filtered avg margin in Visual level filters to "not blank".

Thanks,
Lydia Zhang

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors