Forum Discussion

karun_r's avatar
karun_r
Microsoft Employee
6 years ago
Solved

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_Deckler's avatar
    Greg_Deckler
    Community Champion
    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.
    • karun_r's avatar
      karun_r
      Microsoft 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_Deckler's avatar
        Greg_Deckler
        Community Champion

        karun_r

         

        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

  • karun_r 

    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_r's avatar
      karun_r
      Microsoft 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 ?

  • Hi,

    Here's what i would do:

    1. 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
    2. Drag time dimensions from the Calendar Table.
    3. 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_r's avatar
      karun_r
      Microsoft 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_Mathur's avatar
        Ashish_Mathur
        Super User

        Hi,

        Does this work?

        =COUNTROWS(FILTER(SUMMARIZE(Sales,Sales[Order Number],Calendar[Date],"Sale",[Total Sales]),[Sale]>0))