PowerBI.tips

Power BI Histogram with Custom Bins (DAX)

April 27, 2016 By Mike Carlo
Power BI Histogram with Custom Bins (DAX)

How do I build a histogram with custom bins in Power BI using DAX?

TL;DR. Use Enter Data to make a Buckets table (Name, Min, Max). A Bins measure counts US Census rows where 2010 Census falls between MIN(Buckets[Min]) and MAX(Buckets[Max]). Chart it in a Stacked Column Chart with Axis = Name, Value = Bins.

What data does the histogram tutorial use?

The tutorial uses state population data from the 2010 and 2015 Census, built in the earlier From Wikipedia to a Colorful Map tutorial. The goal is to see how many states fall in a population range, such as 4 million or less in 2010.

How do I create the bins table?

Click Enter Data on the Home ribbon, type in your bucket ranges, name the table Buckets, and click Load. The post shows its own bucket values only in a screenshot, so pick ranges that fit your data.

What does the Bins measure count?

It counts the rows in US Census where 2010 Census falls between the bucket’s Min and Max: Bins = CALCULATE(COUNTROWS(FILTER('US Census', AND([2010 Census] >= MIN(Buckets[Min]), [2010 Census] <= MAX(Buckets[Max]))))). In the 2016 post, you add it from the ellipsis next to US Census, then New Measure.

How do I turn the measure into a histogram chart?

Add a Stacked Column Chart with Axis = Name (from Buckets), Legend = Name, and Value = Bins. Then open the chart’s ellipsis, choose Sort By, and pick Bins to order the buckets by count, descending.

Can the same bins color a map?

Yes. On the map, remove % Change from Values, add Bins, and put Name from Buckets in Legend. Clicking a bar in the chart then filters both the table and the map.

Related: From Wikipedia to a Colorful Map, Manually Enter Data, and Fixing the Truncating Bar Chart.

Alright to start this Tutorial off right we are going to incorporate the new feature released this spring from Power BI, called publish to web. Below you can view last week’s tutorial and interact with the data. Feel free to click around to see how the visualization works (you can click the shaded states or on the state names at the bottom).

For this tutorial we will build upon the last tutorial, From Wikipedia to Colorful Map. If you want to follow along in this tutorial click on the link and complete the previous tutorial.

Materials

  • Power BI Desktop (I’m using the March 2016 version, 2.33.4337.281) download the latest version from Microsoft Here
  • Mapping PBIX file from last tutorial download Maps Tutorial to get a jump start

Understanding the Data

Picking up where we left off we have data by state with data from the 2010 Census and 2015 Census.

Data from Region Maps Tutorial

What we would like to identify is how many states are within a given population range. Say I wanted to see on the map, or in a table all the states that had 4 million or less in population in 2010.

Creating Bin Data

To do this we will create bins for our data. Enter custom data in this format. For the tutorial on entering custom data into Power BI Desktop check out this tutorial on Manually Enter Data. Click on the Enter Data button on the Home ribbon. Enter the data as following:

Enter Bucket Data

Note: Make sure you name the new table Buckets as shown in the image above.

Click Load to bring the data into the data model. Notice we now have a new table in the Field column on the right.

Buckets Table

Creating the DAX Measure

Next we will create a measure to evaluate the state level data into our newly created buckets. This will be produced using DAX (Data Analysis Expressions). DAX is an extremely powerful language which is used in SQL applications and Analysis Services. More information can be found on DAX here. Since DAX is so complex we won’t go into a full explanation here. However, we will have many more topics in the future working on and building DAX equations.

Click the Ellipsis next to the table labeled US Census. Then click the first item in the list labeled New Measure.

Note: Ellipsis is the term used for those triple dots found in newer Microsoft applications.

Example of Ellipsis

A formula bar opens up underneath the ribbons bar. Here is where we will name and type in the new measure. The equation we will need to add is the following:

Bins = CALCULATE(COUNTROWS(FILTER('US Census',and([2010 Census] >= min(Buckets[Min]),[2010 Census] <= MAX(Buckets[Max])))))

Press Enter to enter the measure into PowerBI.

Explanation of the Equation

All text before the equal sign is the name of the measure. All the data behind the equal sign is the DAX expression. Essentially this equation is calculating the number of rows where we have data between the Buckets “Min” value and Buckets “Max” value. This is the magic that is DAX. In this simple expression we can compare all our data against our buckets ranges we made earlier.

Finally our new Bin measure should look like the following:

Bin Measure Created

Modifying the Visuals

Now lets modify our visuals to incorporate the new Bins measure. Click on the existing map on the page. Remove the % Change item from the Values selection. Add the Bins Measure to the Values section. Notice the map changes color. Next, add the Name field from the table called Buckets into the Legend field. Our map should look similar to the following:

Map with Bins Added

Next Click on State, 2010 Census, Bins, and Name (from Buckets table) and make a table. It should look like the following:

Table of Bins Measure

Building the Bar Chart

Lastly, we will build a bar chart using our Bins Measure. Click on the Stacked Column Chart Visual and add the following items to the corresponding categories:

  • Axis = Name (from the Buckets table)
  • Legend = Name
  • Value = Bins (from US Census table)

This will yield the following visual:

Bins in Bar Chart

Click on the Ellipsis of the bar chart and then click Sort By, finally click Bins. This will order the items in descending order by the count of the items found in each bin.

Interactive Exploration

Now have fun with your new data. Click on each of the bars in the bar chart and watch your data transform between the table, and the map.

Selection Big in the Bar Chart

Credits

I have to give credit where credit is due. Below is the page from Power Pivot Pro that I used to create binning in the tutorial chart. The binning shown on PowerPivotPro is for Power Pivot but the functionality is the same. Enjoy.

http://www.powerpivotpro.com/2015/03/creating-a-histogram-with-a-user-defined-number-of-buckets/

Want to learn more about PowerBI and Using DAX? Check out this great book from Rob Collie talking the power of DAX. The book covers topics applicable for both PowerBI and Power Pivot inside excel. I’ve personally read it and Rob has a great way of interjecting some fun humor while teaching you the essentials of DAX.

Previous

From Wikipedia to a Colorful Map

More Posts

Nov 20, 2026

What is Shadow AI? - Ep.582

Mike Carlo and Tommy Puglia look at shadow AI, where employees use their own AI tools whether or not company policy allows it, and compare it to the early days of shadow IT and self-service Power BI. They discuss the risks, who owns prompts and skills built on personal subscriptions, and why organizations are better off giving people sanctioned tools.

Nov 18, 2026

Exploring Database Hub - Ep.581

Mike Carlo and Tommy Puglia explore Database Hub in Microsoft Fabric, the preview control plane for monitoring SQL, PostgreSQL and Cosmos DB databases across Fabric, Azure and on-premises. They debate who it is really for, and what it can and can't do today.

Nov 13, 2026

Making Data AI Ready - Ep.580

Mike Carlo and Tommy Puglia work through James Serra's series on making data AI-ready, from data quality and business definitions to where instructions for agents should live. They also cover CI/CD for Fabric Warehouse, OneLake as native storage for Azure Databricks, and a Chicago user group session on the Power BI Desktop Bridge.