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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Calculate Avg by year for a measure by Product

Hello Team, 

 

I have two tables as shown below - 

 

Table A: 

Month StartProduct NameSales Delta 
1/1/2020Prod A15
1/1/2020Prod B28
2/1/2020Prod A16
2/1/2020Prod B29
3/1/2020Prod A17
3/1/2020Prod B30
4/1/2020Prod A18
4/1/2020Prod B31
5/1/2020Prod A19
5/1/2020Prod B32
6/1/2020Prod A20
6/1/2020Prod B33
7/1/2020Prod A21
7/1/2020Prod B34
8/1/2020Prod A22
8/1/2020Prod B35
9/1/2020Prod A23
9/1/2020Prod B36
10/1/2020Prod A24
10/1/2020Prod B37
11/1/2020Prod A25
11/1/2020Prod B38
12/1/2020Prod A26
12/1/2020Prod B39

 

Table B: 

 

Month StartProduct NameSales Delta 
1/1/2020Prod C15
2/1/2020Prod C28
3/1/2020Prod C16
4/1/2020Prod C29
5/1/2020Prod C17
6/1/2020Prod C30
7/1/2020Prod C18
8/1/2020Prod C31
9/1/2020Prod C19
10/1/2020Prod C32
11/1/2020Prod C20
12/1/2020Prod C33

 

I have a relationship for these two tables with my Date table, which joins them based on Month Start as Key. 

 

After the join, if I create a table chart using fields from two tables - This is how it looks : 

 

Month StartProduct NameSales Delta Product Name (From Table B)Sales Delta (From Table B)Diff on Sales 
1/1/2020Prod A15Prod C150
1/1/2020Prod B28Prod C1513
2/1/2020Prod A16Prod C28-12
2/1/2020Prod B29Prod C281
3/1/2020Prod A17Prod C161
3/1/2020Prod B30Prod C1614
4/1/2020Prod A18Prod C29-11
4/1/2020Prod B31Prod C292
5/1/2020Prod A19Prod C172
5/1/2020Prod B32Prod C1715
6/1/2020Prod A20Prod C30-10
6/1/2020Prod B33Prod C303
7/1/2020Prod A21Prod C183
7/1/2020Prod B34Prod C1816
8/1/2020Prod A22Prod C31-9
8/1/2020Prod B35Prod C314
9/1/2020Prod A23Prod C194
9/1/2020Prod B36Prod C1917
10/1/2020Prod A24Prod C32-8
10/1/2020Prod B37Prod C325
11/1/2020Prod A25Prod C205
11/1/2020Prod B38Prod C2018
12/1/2020Prod A26Prod C33-7
12/1/2020Prod B39Prod C336

 

The sales delta on either of the tables are measures which calculate the delta value in sales. Now, I want to get the avg of the "Diff of sales" measure by Year. 

 

 I am planning to include Product Name from table A as a slicer on the visual. So when there is no selection then the measure "Sales Delta" (Which is shown as column in table above) sums values for Prod A and Prod B and compares with Prod C. 

 

This is how the data looks in that case - 

 

Month StartSales Delta from Table ASales Delta from Table BDiff on Sales
1/1/2020431528
2/1/2020452817
3/1/2020471631
4/1/2020492920
5/1/2020511734
6/1/2020533023
7/1/2020551837
8/1/2020573126
9/1/2020591940
10/1/2020613229
11/1/2020632043
12/1/2020653332

 

If you see, the sales delta from table A is the sum of both product A and Prod B. Now, I am using the below formula to calculate the average for the year - 

 

Delta Avg by Year = calculate(AverageX(SUMMARIZE(TABLE A,TABLEA[MonthStart], "Avg", [Diff in Sales]),[Avg]),
filter(ALLSELECTED(TABLEA),TABLEA[Year]=max(TABLEA[Year])))
 
However, the above formula is not working when the user filters by product name on Table A. The average calculated should be based on values for Prod A but instead it is averaging for both products included. 
 
Please let me know where I am wrong on this one. Thank you. 

 

 

 

3 REPLIES 3
nandic
Memorable Member
Memorable Member

Hi @Anonymous ,
If table A and table B have exactly the same structure, i would go with union.
Firstly create union in Power Query for tables A and B. Then set only one relationship between this new table and Date table.
Afterwards create 3 measures:

AB Amount = CALCULATE(SUM(Append1[Sales Delta ]),FILTER(Append1,Append1[Product Name] in {"Prod A","Prod B"})) 

C Amount = CALCULATE(SUM(Append1[Sales Delta ]),FILTER(Append1,Append1[Product Name] = "Prod C"))
 
Difference = [AB Amount] - [C Amount]
 
This is the result, attached also pbix file.
 
Append.PNG
 
Regards,
Nemanja Andic
 
Anonymous
Not applicable

Thank you, Nandic. One of my needs is to have a slicer available on the report for the user to pick either Prod A or Prod B to compare diff with Prod C. When we do union, then that would not be possible right ? Cause, when the user filters for prod A then we dont have measure based on prod C calculated properly as the data gets filtered based on slicer. Please bear with me as I am new to Power BI. Thanks for your help. 

Hi, attached new version.
Union table is still used, but for filtering i created disconnected table "Product". It has no relationship to date or Append table.
There are 2 measures in the file:
1) AorB = if user can select only one product and it is mandatory

AorB Amount =
var _selectedProduct = SELECTEDVALUE('Product'[Product],"Prod A") -- if filter is radio button and always one value will be selected, then no need to specify this alternate result ("Prod A")
RETURN
CALCULATE(SUM(Append1[Sales Delta ]),FILTER(Append1,Append1[Product Name] = _selectedProduct))


2) AandB = if user can select both Prod A and Prod B and to compare total (A+B) to Prod C

AandB Amount =
var _selectedProducts = values('Product'[Product])
RETURN
CALCULATE(SUM(Append1[Sales Delta ]),FILTER(Append1,Append1[Product Name] in _selectedProducts))



Regards,
Nemanja Andic

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors