Forum Discussion

hernandezguzman's avatar
hernandezguzman
Regular Visitor
5 years ago
Solved

Bin dynamically group by sets

Hi,

 

I would like to know if it is possible to create a bin chart where the bings are dinamic set by X number,

example if I have 1252 Customers and want to split them around 10  bings then each bin wouldbe group by (1252 / 10) = 125, but I change my date slicer then the customer total is now 1850 so the new bins will be group by (1850 / 10) = 185.

Is this possible to peform ,

I really appreaciate any help,


Regards,

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi hernandezguzman,

    I succeed to compress these fields and now you can just use two fields to get correspond values.

    bin num = 
    VAR _bin =
        SQRT ( COUNTROWS ( Customer ) - 1 ) - 1
    VAR _size =
        CEILING (
            DIVIDE ( MAX ( Customer[CustomerId] ) - MIN ( Customer[CustomerId] ), _bin ),
            1
        )
    RETURN
        FLOOR ( DIVIDE ( Customer[CustomerId], _size ), 1 ) + 1
    
    Bin Names = 
    VAR _bin =
        SQRT ( COUNTROWS ( Customer ) - 1 ) - 1
    VAR _size =
        CEILING (
            DIVIDE ( MAX ( Customer[CustomerId] ) - MIN ( Customer[CustomerId] ), _bin ),
            1
        )
    RETURN
         ( [bin num] - 1 ) * _size & " ~ " & [bin num] * _size

    As I said, data view level slicer/filter are host ton the child table that generated from data model table which calculated columns/table host so you can't interact them to get dynamic results.
    For this scenario, I'd like to suggest use query parameters, you can create two date type query aptamer and they allow you to input values.  You can create a blank query table to store the query parameters values.(let's named it as 'filter range' table)

    Creating Tables In Power BI/Power Query M Code Using #table() 

    After these stpes, you can create a calculated table to filter raw table records based on the 'filter range' table and add custom fields with calculated column expressions which I pasted above and you will get a table based on query parameter and dynamic bin ranges. (it will changes every time you modify the query parameter and apply changes)

    Fitlered =
    ADDCOLUMNS (
        ADDCOLUMNS (
            FILTER (
                Customer,
                [DateEntered] >= MIN ( 'Filter range'[Start] )
                    && [DateEntered] <= MAX ( 'Filter range'[End] )
            ),
            "bin num",
                VAR _bin =
                    SQRT ( COUNTROWS ( Customer ) - 1 ) - 1
                VAR _size =
                    CEILING (
                        DIVIDE ( MAX ( Customer[CustomerId] ) - MIN ( Customer[CustomerId] ), _bin ),
                        1
                    )
                RETURN
                    FLOOR ( DIVIDE ( Customer[CustomerId], _size ), 1 ) + 1
        ),
        "Bin Names",
            VAR _bin =
                SQRT ( COUNTROWS ( Customer ) - 1 ) - 1
            VAR _size =
                CEILING (
                    DIVIDE ( MAX ( Customer[CustomerId] ) - MIN ( Customer[CustomerId] ), _bin ),
                    1
                )
            RETURN
                 ( [bin num] - 1 ) * _size & " ~ " & [bin num] * _size
    )
    

    Regards,

    Xiaoxin Sheng

11 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi hernandezguzman,

    In fact, current power bi not able to create dynamic calculate columns based on filter/slicer.

    You can try to create a measure based on the customs and slicers but it can't be used as the category/legend on your charts.
    Regards,

    Xiaoxin Sheng

    • hernandezguzman's avatar
      hernandezguzman
      Regular Visitor

      thanks so much Xiaoxin Sheng for your answer, then to you consider it is not possible to create a bin chart that have always a maximun number of 10 bins and with equality quntity of customers on it ?

      this is basically what I am trying to do,  I need to have a bin chart with not more than 10 bins bars and that the quantity of customers gets balaced distributed between the bins.

      Thanks

      Regards,

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi hernandezguzman,

        The static version is possible, we can create calculated column formula to calculate the correct bin range based on table records.  The hard part is make it dynamic based on slicer/filter.

        In fact, calculated column and filter/slicer are host on the different data levels, you can't use child level to effect its parent.

        Notice: the data level of power bi.
        Database(external) -> query table(query, custom function, query parameters) -> data model table(table, calculate column/table) -> data view wiht virtual tables(measure, visual, filter, slicer)

        Regards,

        Xiaoxin Sheng