Forum Discussion

Julia2023's avatar
Julia2023
Icon for Helper I rankHelper I
1 year ago
Solved

Buckets based on revenue

Hi all, I have 101 customers and want to divide them into 3 buckets, then calculate the sum of the revenue for each bucket. I created a rank measure and would like to use it to create these buckets. Any ideas on how to do this?  Thanks. 

Customer Rank = RANKX(ALL(Customers[Customer ID]), [Revenue], , DESC, Dense)

 

  • FreemanZ's avatar
    FreemanZ
    1 year ago

    hi Julia2023 ,

     

    Supposing you have a data table like:

     

    try to plot a visual with a sum of [sales] column and a calculated column like:

     

    Bucket = 
    VAR RankValue =
    RANKX(
        ALL(data[ID]),
        CALCULATE(SUM(data[sales]), ALLEXCEPT(data, data[ID])),
        ,DESC
    )
    VAR _totalrank = COUNTROWS(VALUES(data[ID]))
    VAR BucketSize = DIVIDE(_totalrank, 3, 1)  
    RETURN 
    SWITCH(     
        TRUE(),     
        RankValue <= BucketSize, "Bucket 1",     
        RankValue <= 2 * BucketSize, "Bucket 2",       
        RankValue > 2 * BucketSize, "Bucket 3"   
    )

     

     

    it worked like below:

     

    or directly write a calculated table like:

     

    BucketTotal = 
    VAR _table1 =
    ADDCOLUMNS(
        data,
        "RankValue",
        RANKX(
            ALL(data[ID]),
            CALCULATE(SUM(data[sales]), ALLEXCEPT(data, data[ID])),
            ,DESC
        )
    )
    VAR _totalrank = COUNTROWS(VALUES(data[ID]))
    VAR _bucketsize = DIVIDE(_totalrank, 3, 1)  
    VAR _table2 =
    ADDCOLUMNS(    
        _table1,
        "Bucket2",
        SWITCH(    
            TRUE(),    
            [RankValue] <= _bucketsize, "Bucket 1",    
            [RankValue] <= 2 * _bucketsize, "Bucket 2",      
            [RankValue] > 2 * _bucketsize, "Bucket 3"  
        )
    )
    VAR _table3 = 
    ADDCOLUMNS(    
        SUMMARIZE(_table2, [Bucket2]),    
        "Revenue", 
        VAR _bucket =  [Bucket2]
        RETURN
        SUMX(
            FILTER(
                _table2,
                [Bucket2] = _bucket
            ),
        data[Sales]
        )
    )
    RETURN _table3

     

     

    it worked like:

     

    please find more info in the attachment. 

     

    p.s. we can't treat measure value as categorical value and plot against other measures directly. In this case, bucket needs to be a calculated column.

6 Replies

  • hi Julia2023 ,

     

    try to add a calculated table like:

     

    Table =

    VAR _table1 =

    ADDCOLUMNS(

        ALL(Customers[Customer ID]),

     

       "Revenue2", [Revenue]

     

    )

    VAR _table2 =

    ADDCOLUMNS(

        _table1,

       "rank", 

        RANKX(

            ALL(Customers[Customer ID]), 

            [Revenue], , DESC, Dense

        )

     

    )

    VAR _table3 =

    ADDCOLUMNS(

        _table2,

        "Bucket"

     

         SWITCH(TRUE(), [rank]<=34, "1-34", [rank]>=68, "68-101", "35-67")

     

    )

    VAR _result =

    ADDCOLUMNS(

        SUMMARIZE(_table3, [Bucket]),

     

        "BucketRevenue",

     

        CALCULATE(SUM([Revenue2])

    )

    RETURN _result

    • Julia2023's avatar
      Julia2023
      Icon for Helper I rankHelper I

      FreemanZ , thanks. Actually, the table isn't working here, as I want to have a dynamic measure. I mean, if new customers come in, the buckets should be modified as well, based on the total count of customers.