Histograms in Power BI with bins and DAX

How to make a histogram in Power BI, which has no histogram visual: bins with groups, bins of a measure with a DAX table, choosing the bin size, and reading the shape of a distribution.

Short answer: Power BI has no histogram visual, so a histogram is a column chart of bins. For a column, right-click it in the Data pane, New group, Group type: Bin, set the bin size (or the number of bins), then put the new bin field on the X-axis and a count on the Y-axis. For a measure (order value per customer, days to deliver per order), build the bins with a small DAX table and a count measure.

The chart, part by part: What is a histogram? draws its parts and shows how to read one, its kinds and the mistakes to avoid.

An average delivery time of 3.1 days sounds fine. A histogram shows that most orders arrive in two days and one in ten takes over a week: the average hid a long tail that customers feel.

What does a histogram show?

02004006008001400-15201-26102-32803-41204-5705-6606-71607+
Orders by delivery time, one-day bins

The shape is the answer: a peak at two to three days, then a tail, and a bump at seven days and more that needs looking into (one carrier, one region, one warehouse).

How do you bin a column?

For a numeric or date column in the model, for example Orders[Delivery Days]:

  1. In the Data pane, right-click the column and pick New group.
  2. Group type: Bin. Choose Size of bins (for example 1) or Number of bins.
  3. Power BI adds a column Delivery Days (bins). Drag it to the X-axis of a column chart and a count, for example Orders[Order ID] as Count, to the Y-axis.
  4. Set the X-axis Type to Categorical so every bin gets a column, including empty ones in the middle, and remove the gaps between columns (Columns, Spacing) if you want the classic histogram look.

Bins on a column are fixed at refresh. That suits facts that do not change with the report's filters, such as each order's delivery days.

How do you bin a measure?

When the number to bin is a measure (each customer's revenue in the selected period), the bins must react to slicers, so they are a table:

Revenue Bins =
GENERATESERIES ( 0, 2000, 250 )

This makes a one-column table Value with 0, 250, 500 ... 2000. Rename the column Bin Start, then count the customers whose revenue falls in each bin:

Customers in Bin =
VAR BinStart = SELECTEDVALUE ( 'Revenue Bins'[Bin Start] )
VAR BinEnd = BinStart + 250
RETURN
    COUNTROWS (
        FILTER (
            VALUES ( Customer[Customer ID] ),
            VAR R = [Revenue]
            RETURN NOT ISBLANK ( R ) && R >= BinStart && ( R < BinEnd || BinStart = 2000 )
        )
    )

Revenue Bins has no relationship to the model: it is a disconnected table that only gives the X-axis its values. The last bin (2000) takes everything above it, and NOT ISBLANK keeps customers who bought nothing in the period out of the first bin (a blank compares like 0).

Bin StartCustomers in Bin
0812
250534
500301
750166
100094

With a "Customer: Region" slicer on the page, the bins recount for that region.

How big should the bins be?

Too few bins and the shape disappears; too many and it is noise. Start with round sizes people understand (1 day, 50 euros, 5 minutes) and about 10 to 20 bins. Try two sizes: if the shape changes a lot, the data is thin and the histogram should say so.

ShapeMeansExample
One peak, symmetricvalues spread evenly around a typical valueweights of a packed product
One peak, long right tailmost are small, a few are bigorder values, delivery times
Two peakstwo groups mixed in onetwo warehouses, two customer types
A spike at the edgea cap or a default value"7+" bucket, a timeout
Reading the shape

What goes wrong?

  • Bins as a continuous axis. On a continuous axis, empty bins vanish and the columns spread unevenly. Use categorical.
  • Averages instead of a count. A histogram counts items per bin; summing or averaging a measure on the Y-axis makes a different chart.
  • A fixed bin column for a measure. A calculated column is computed once and ignores slicers; use a bin table and a measure.
  • The tail cut off. A last bin "2000+" collects the outliers; without it, they disappear from the picture.

Where can you check this?

Where do you start?

Bin a column with New group for a first look, then build a bin table when the number is a measure. The scatter chart shows two numbers against each other, and the Analytics pane adds percentile lines.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.