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

How to use DAX to Sum sales and take the average per transaction?

I would like to use DAX to find the average transaction sales for each grouping type.

 

This is how I would do it in SQL. However, I'd like to do this for each grouping_type. I'd have to re-run this query for each grouping type and compute the average. I'd rather not do that.

 

How would I implement this in DAX? Is this possible?

 

 

SELECT 
	transaction_key
	,SUM(sales) sum_of_sales
INTO #y
FROM #x x
WHERE x.transaction_key IN (SELECT DISTINCT transaction_key FROM #x x1 wHERE x1.grouping_type=6)
GROUP BY transaction_key

SELECT AVG(sum_of_sales)
FROM #y

 

 

period_typeStorestore_typetransaction_datetransaction_keydepartmentItemLookupCodegrouping_typesales
A1504P_STORE7/6/20171504-1567118BE120733613.49
A1504CONTROL6/10/20171504-1567118BE780549412.99
D1504CONTROL8/9/20171504-1567118BE788755615.99
A1536CONTROL6/17/20171536-947058SP67124.95
C1536P_STORE7/11/20181536-1073185BE6910971226.79
D1549P_STORE8/3/20171549-1073185SP110056125.95
A1515491CONTROL6/17/20171549-623376BE755990615.99
C187CONTROL5/24/2018187-347592BE6885493045.95
A187CONTROL6/14/2017187-347592BE73350012.99
A187CONTROL6/30/2017187-347592BE7905131222.99
C187P_STORE6/6/2018187-329148SP367672129.95
C5819CONTROL6/6/20185819-329148BE8022951229.99
C5819CONTROL6/12/20185819-264390BE732937619.49
C5819CONTROL5/24/20185819-264390WI773064111.99
C5912CONTROL5/18/20185912-264390BE7249521525.79
D7348P_STORE8/3/20187348-264390WI774932110.19
D7348P_STORE8/9/20187348-264390BE77618214.29
D7348CONTROL8/4/20187348-1113160BE7492832433.29
1 ACCEPTED SOLUTION
v-cherch-msft
Employee
Employee

Hi @Anonymous

 

You may try to use SUMMARIZE Function. For example:

Measure  =
VAR b =
    SUMMARIZE ( Table2, Table2[transaction_key], "a", SUM ( Table2[sales] ) )
RETURN
    AVERAGEX ( b, [a] )

Regards,

Cherie

Community Support Team _ Cherie Chen
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
v-cherch-msft
Employee
Employee

Hi @Anonymous

 

You may try to use SUMMARIZE Function. For example:

Measure  =
VAR b =
    SUMMARIZE ( Table2, Table2[transaction_key], "a", SUM ( Table2[sales] ) )
RETURN
    AVERAGEX ( b, [a] )

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

I would like to use DAX to find the average transaction sales for each grouping type.

 

This is how I would do it in SQL. However, I'd like to do this for each grouping_type. I'd have to re-run this query for each grouping type and compute the average. I'd rather not do that. The tricky part is that each transaction can contain multiple grouping_type and must be considered in the calculation.

 

How would I implement this in DAX? Is this possible?

 

 

SELECT 
	transaction_key
	,SUM(sales) sum_of_sales
INTO #y
FROM #x x
WHERE x.transaction_key IN (SELECT DISTINCT transaction_key FROM #x x1 wHERE x1.grouping_type=6)
GROUP BY transaction_key

SELECT AVG(sum_of_sales)
FROM #y

 

 

period_typeStorestore_typetransaction_datetransaction_keydepartmentItemLookupCodegrouping_typesales
A1504P_STORE7/6/20171504-1567118BE120733613.49
A1504CONTROL6/10/20171504-1567118BE780549412.99
D1504CONTROL8/9/20171504-1567118BE788755615.99
A1536CONTROL6/17/20171536-947058SP67124.95
C1536P_STORE7/11/20181536-1073185BE6910971226.79
D1549P_STORE8/3/20171549-1073185SP110056125.95
A1515491CONTROL6/17/20171549-623376BE755990615.99
C187CONTROL5/24/2018187-347592BE6885493045.95
A187CONTROL6/14/2017187-347592BE73350012.99
A187CONTROL6/30/2017187-347592BE7905131222.99
C187P_STORE6/6/2018187-329148SP367672129.95
C5819CONTROL6/6/20185819-329148BE8022951229.99
C5819CONTROL6/12/20185819-264390BE732937619.49
C5819CONTROL5/24/20185819-264390WI773064111.99
C5912CONTROL5/18/20185912-264390BE7249521525.79
D7348P_STORE8/3/20187348-264390WI774932110.19
D7348P_STORE8/9/20187348-264390BE77618214.29
D7348CONTROL8/4/20187348-1113160BE7492832433.29

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.