Forum Discussion

vissvess's avatar
vissvess
Icon for Helper V rankHelper V
7 years ago
Solved

DAX/Group(Bins) for running buckets

Hi,

 

My data set has a column named "Time" with time stamp values.

When I right click the field "Time" & create new group, it provides me an way to create groups/bins/buckets of equal specifed durations. Apparently, it creates a column in the table with corresponding bucket value matching "Time" column. When I create a continuous bar chart/histogram, the bucktets falls in X axis and count / total for some other fields falls in Y axis. So far everyone aware.

 

My requirement is that I need the X axis to be a running bucket.

Instead of X axis value as follows,

00:00:00 AM, 3:00:00 AM, 6:00:00 AM, 9:00:00 AM, 12:00:00 PM, 3:00:00 PM, 6:00:00 PM, 9:00:00 PM &  12:00:00 AM.

 

I need the X axis value to be as follows.

00:00:00 AM - 03:00:00 AM, 01:00:00 AM - 04:00:00 AM, 02:00:00 AM - 05:00:00 AM, and so on till 09:00:00 PM - 12:00:00 AM. 

 

I aware a histogram cannot be created with such X axis. But I can create a clustered/stack column chart with the values as X axis.

 

Any workaround would help me a lot. 

 

Thanks

Vishvesh R

  • Anonymous's avatar
    Anonymous
    7 years ago

    This is a sample of the table again (simplified):

    TimeTime BucketBucketSortOrder
    01:10:0023-223
    01:10:000-30
    01:10:001-41
    06:20:224-74
    06:20:225-85
    06:20:226-96

    What you have to do is this:

    1. Create a table with all your buckets in it. It has to look like this:

    Time BucketStartInclusiveEndExclusiveBucketSortOrder
    00:00:00-03:00:0000:00:0003:00:000
    01:00:00-04:00:0001:00:0004:00:001
    02:00:00-05:00:0002:00:0005:00:002
    03:00:00-06:00:0003:00:0006:00:003
    04:00:00-07:00:0004:00:0007:00:004
    and so on… up to   
    23:00:00-02:00:0023:00:0002:00:0023

    2. Once you have it, you cross-join it with the table that stores your unique times. Cross-joining in PQ (M) is easy: just create a column and type "=Your_Table_Name".

    3. After that, you expand the table, so that the expanded columns appear.

    4. Then you add another column that will now return TRUE if

    [Time] >= [StartInclusive] and [Time] < [EndExclusive].

    5. Then you filter the table leaving only the rows where the above is TRUE.

    6. Then you remove all the columns apart from [Time], [Time Bucket] and [BucketSortOrder].

    7. Now join the [Time] to the original column with the times and do all the things I've explained before...

     

    Best

    Darek

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Mate, you have to create a table (in Power Query) of this structure:

     

    Time Buckets (table)

    Time|Time Bucket|BucketSortOrder

    01:10:00|00:00:00 AM - 03:00:00 AM|1

    01:10:00|01:00:00 AM - 04:00:00 AM|2

    ...

     

    where

        [Time] is a (hidden) column with all the times in your table,

        [Time Bucket] is the bucket into which the Time falls (one time will fall into many buckets),

        [BucketSortOrder] is a (hidden) column that you'll be sorting the buckets by.

     

    Remember that if a bucket A appears in the table many times, it has to have the same BucketSortOrder.

     

    Once this table's been created, you have to join it to your table on the [Time] column and the relationship will be M2M. Filtering will be one-way only, from [Time Buckets] to your table.

     

    Then you can take the [Time Bucket] column from the Time Buckets table, drop it onto your visual and add your measures. They should aggregate correctly for the time periods/categories.

     

    Best

    Darek

    • vissvess's avatar
      vissvess
      Icon for Helper V rankHelper V

      Anonymous ,

       

      Thanks for the inputs.

       

      As per your advise, I need to create a table as shown by you.

      For column 1 [Time], from my table I'll reference my existing query, remove other columns & remove duplicate in the specified column. 

      For column 2 & 3, I was working out the code.

       

      As I have 3 hours bucket, for every timestamp, I'll have three buckets, if I am not wrong.

      So If I create 3 columns with column name as sort order (1,23) so that I'll unpivot to create it as specified.

      But If I use conditional column, I was not able to achieve the result.

       

      Zubair_Muhammad & Anonymous ,

       

      Need your help for the power query stuff. 

       

      Thanks

      • Anonymous's avatar
        Anonymous
        Not applicable

        This is a sample of the table again (simplified):

        TimeTime BucketBucketSortOrder
        01:10:0023-223
        01:10:000-30
        01:10:001-41
        06:20:224-74
        06:20:225-85
        06:20:226-96

        What you have to do is this:

        1. Create a table with all your buckets in it. It has to look like this:

        Time BucketStartInclusiveEndExclusiveBucketSortOrder
        00:00:00-03:00:0000:00:0003:00:000
        01:00:00-04:00:0001:00:0004:00:001
        02:00:00-05:00:0002:00:0005:00:002
        03:00:00-06:00:0003:00:0006:00:003
        04:00:00-07:00:0004:00:0007:00:004
        and so on… up to   
        23:00:00-02:00:0023:00:0002:00:0023

        2. Once you have it, you cross-join it with the table that stores your unique times. Cross-joining in PQ (M) is easy: just create a column and type "=Your_Table_Name".

        3. After that, you expand the table, so that the expanded columns appear.

        4. Then you add another column that will now return TRUE if

        [Time] >= [StartInclusive] and [Time] < [EndExclusive].

        5. Then you filter the table leaving only the rows where the above is TRUE.

        6. Then you remove all the columns apart from [Time], [Time Bucket] and [BucketSortOrder].

        7. Now join the [Time] to the original column with the times and do all the things I've explained before...

         

        Best

        Darek