Forum Discussion
DISTINCT over SUMMARIZE
I have a measure that follows the pattern as mentioned below
Test Adds =
CALCULATE (
SUMX (
DISTINCT ( SUMMARIZE ( Sales, Sales[Order Date], Sales[Order Number] ) ),
IF ( Sales[Sales Amount] > 0, 1, 0 )
)
)
I was wondering if I really need the DISTINCT over a SUMMARIZE call as the latter would already return unique combinations of order date and order number. Also, is there a better way to calculate the total number of orders for each day for which the sales amount > 0 ?
- I do not see the need for DISTINCT, SUMMARIZE already makes things distinct.
Maybe, but don't know your data to be sure. Should just be able to use COUNTROWS and FILTER the table returned from SUMMARIZE but not sure that is more efficient or not.
12 Replies
- Greg_DecklerCommunity ChampionI do not see the need for DISTINCT, SUMMARIZE already makes things distinct.
Maybe, but don't know your data to be sure. Should just be able to use COUNTROWS and FILTER the table returned from SUMMARIZE but not sure that is more efficient or not.- karun_rMicrosoft Employee
Greg_Deckler Yes, that's what I thought. I removed DISTINCT from the measure definition and the numbers seem to match. I am just trying to think of a case where I would need a DISTINCT over SUMMARIZE. It's impossible for me to test all possible combinations considering the dimensions I have in my dataset. In your experience, was there ever a case where you had to apply DISTINCT over a SUMMARIZE statement ? I am yet to do a performance comparison between both the measures.
- Greg_DecklerCommunity Champion
No on DISTINCT with SUMMARIZE, because when you SUMMARIZE, whatever your are grouping on will be distinct. DISTINCT will make sure that entire rows are distinct in a table versus just a particular column, but the fact that you are using SUMMARIZE ensures that the table rows are distinct, it's built into the grouping.
Ashish_Mathur put into a formula what I was discussing with COUNTROWS and FILTER. Now, whether that is an improvment? You'd have to test it. See my 4 part series on Performance Tuning DAX: https://community.powerbi.com/t5/Community-Blog/Performance-Tuning-DAX-Part-1/ba-p/976275
- amitchandakSuper User
I doubt you need to summarize there. Also, the formula should one of the two as per need
Test Adds =
CALCULATE (
SUMX (
SUMMARIZE ( Sales, Sales[Order Date], Sales[Order Number],"_1",sumx ( Sales,if(Sales[Sales Amount] > 0, 1, 0 ) )),
[_1]
)
)
Test Adds =
CALCULATE (
SUMX (
SUMMARIZE ( Sales, Sales[Order Date], Sales[Order Number],"_1",sum ( Sales[Sales Amount]) ),
if([_1] > 0, 1, 0 )
)
)- karun_rMicrosoft Employee
amitchandak Would that have an performance improvement over my current metric definition ? If so, can you please help me understand how it would improve things ?
- Ashish_MathurSuper User
Hi,
Here's what i would do:
- Create a Calendar Table and build a relationship from the Order date column of your Sales Table to the Date column of your Calendar Table. Write calculated column formlas to extract various time dimensions such as Year, Month, Quarter
- Drag time dimensions from the Calendar Table.
- Write these measures
Total sales = SUM(Sales[Sales amount])
Orders which recorded sales = SUMX(FILTER(SUMMARIZE(Sales,Sales[Order Number],Calendar[Date],"Sale",[Total Sales]),[Sale]>0),[Sale])
Hope this helps.
- karun_rMicrosoft Employee
Ashish_MathurWhy would I want to do a sum of [Sales] measure ? I am just trying to get a count of number of orders for each day where sales > 0. I shouldn't have to do a sum on Sales , right ?
- Ashish_MathurSuper User
Hi,
Does this work?
=COUNTROWS(FILTER(SUMMARIZE(Sales,Sales[Order Number],Calendar[Date],"Sale",[Total Sales]),[Sale]>0))