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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
nr_m
Frequent Visitor

Calculate Average sales for First Three occurrences by date and the difference b/w Avg - SUM(sales)

Hi Community,
I'm trying to calculate Avg. sales for first three occurences by date and the diffrence between Avg Sales of First Three occurences, Sum(Sales).
In the below table I'm trying to calculate Baseline(Top 3 Avg)., ie., (30+50+20)/3=33.3 Also trying to get Baseline - Sales for each row.
Please help me to solve this issue. Thanks in advance.

ID DateSalesBaseline (Top3 Avg)(Baseline - Sales)
11-Jan-223033.33.3
210-Feb-225033.316.6
313-Mar-222033.3-13.3
44-Apr-224033.3-6.7
58-Sep-225033.316.6
Total 19033.316.5

 

Regards
NR

Please help

@amitchandak @Greg_Deckler 

 

 

 

2 ACCEPTED SOLUTIONS

Hi @nr_m 

 

try the following code:

Baseline - Sales Positive = 
SUMX( FILTER(ADDCOLUMNS(Sales,  "base", [Baseline]), Sales[Sales] <= [base]), [base] - Sales[Sales])

 

MFelix_0-1668157807347.png

If you want to show the others has 0 then redo the formula to:

Baseline - Sales Positive = 
SUMX( FILTER(ADDCOLUMNS(Sales,  "base", [Baseline]), Sales[Sales] <= [base]), [base] - Sales[Sales] )+0

 

MFelix_1-1668157849235.png

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

Hi @nr_m ,

 

To what I can understand you want to pick up the values based on the selected categories is that it?

 

Try the following code:

Baseline = 
VAR temptable =
    TOPN ( 3,  ALLSELECTED(  'Sales'[Date], Sales[Sales], Sales[ID], Sales[Category] ), 'Sales'[Date], ASC )
VAR _result =
    AVERAGEX ( temptable, 'Sales'[Sales] )
RETURN
    _result

MFelix_0-1668517699787.pngMFelix_1-1668517707799.pngMFelix_2-1668517721261.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

5 REPLIES 5
MFelix
Super User
Super User

Hi @nr_m ,

 

Create the following two metrics:

Baseline = 
VAR temptable =
    TOPN ( 3,  ALL ( 'Sales'[Date], Sales[Sales], Sales[ID ] ), 'Sales'[Date], ASC )
VAR _result =
    AVERAGEX ( temptable, 'Sales'[Sales] )
RETURN
    _result


Baseline - Sales = 
SUMX( ADDCOLUMNS(Sales,  "base", [Baseline]), [base] - Sales[Sales])

 

Final result below:

MFelix_1-1668116310870.png

Believe that you have some misscalculation on your example on the last rows.

 

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



nr_m
Frequent Visitor

Thank you for the Solution @MFelix 
It worked perfectly.
Also I want to display a KPI Score card having Total of only Positive values from Baseline - Sales 
I used If condition and wrote below formulae but it's showing Total as 0.
Can you please check and correct the formulae

Thanks in advance. 
Appriciate your help!

 

nr_m_0-1668152745713.png

 

 

Hi @nr_m 

 

try the following code:

Baseline - Sales Positive = 
SUMX( FILTER(ADDCOLUMNS(Sales,  "base", [Baseline]), Sales[Sales] <= [base]), [base] - Sales[Sales])

 

MFelix_0-1668157807347.png

If you want to show the others has 0 then redo the formula to:

Baseline - Sales Positive = 
SUMX( FILTER(ADDCOLUMNS(Sales,  "base", [Baseline]), Sales[Sales] <= [base]), [base] - Sales[Sales] )+0

 

MFelix_1-1668157849235.png

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



nr_m
Frequent Visitor

Hi @MFelix 
Sorry to bother you again, Above solution is working fine on test data but when I applied it on the original data I'm not gettig desired output(baseline). Also have to use category as filter.
I can able to achieve baseline(Top3 Avg) by Group by,

Baseline =
VAR temptable =
    TOPN ( 3, GROUPBY('Table', 'Table'[Date],'Table'[Sales]), 'Table'[Date], ASC )
VAR _result =
    AVERAGEX ( temptable, 'Table'[Sales] )
RETURN
    _result
 
But showing 0 for baseline-sales

Please find the below screenshot for your refrence also sharing the table data.
Thank you in adavance, Appricaite your support.

nr_m_0-1668426312965.png

 

IDDateSalesCategory
Key-11/1/202210A
Key-61/1/202260C
Key-71/1/202270C
Key-22/10/202220A
Key-82/10/202280D
Key-92/10/202290D
Key-102/10/202210B
Key-132/10/202240A
Key-33/13/202230B
Key-113/13/202220D
Key-143/13/202250B
Key-44/4/202240B
Key-59/8/202250C
key-129/8/202230A

Regards,

NR

Hi @nr_m ,

 

To what I can understand you want to pick up the values based on the selected categories is that it?

 

Try the following code:

Baseline = 
VAR temptable =
    TOPN ( 3,  ALLSELECTED(  'Sales'[Date], Sales[Sales], Sales[ID], Sales[Category] ), 'Sales'[Date], ASC )
VAR _result =
    AVERAGEX ( temptable, 'Sales'[Sales] )
RETURN
    _result

MFelix_0-1668517699787.pngMFelix_1-1668517707799.pngMFelix_2-1668517721261.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
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.