Analysis Services · Azure Analysis Services · Cube Formulas · Excel · Power BI · PowerPivot

Excel Cube Functions, Dynamic Arrays And Lambdas, Part 3: Grouping And Histograms

In the last post in this series I showed how you can use Excel’s new Lambda helper functions to return tables. In this post I’ll show you how you can use them to return a dynamic array of CubeSet functions which can be used to build a histogram and do the kind of ABC-type analysis that can be difficult to do in a regular Power BI report.

For the examples in this post I added some rows to the Excel Data Model table that I’m using to hold my source data:

The aim here is to put these products into an arbitrary number of groups, or buckets, based on their sales. To define these buckets I created another Excel table called Buckets that has three columns: the name of the bucket, and the lower bound and the upper bound of the sales amount that determines whether a product should fall into the bucket:

I then created two dyanmic array formulas using the new Map function. In cell G2 I added this formula:

 Buckets[Bucket Name], 
 Buckets[Lower Bound], 
 Buckets[Upper Bound], 
   "FILTER([Sales].[Product].[Product].MEMBERS, [Measures].[Sales Amount]>=" & l & 
   " AND [Measures].[Sales Amount]<=" & u & ")", 

And in cell H2 I added this formula:

    "[Measures].[Sales Amount]"),

Here’s what these two formulas return:

The formula in G2 takes three arrays – the values from the three columns in the Buckets table – and then loops over the values in those columns and uses the CubeSet function to return a set of the Products whose sales are between the lower and upper bounds. Since there are two rows in the Buckets table, this formula returns two sets. The formula in H2 uses the CubeValue function to return the aggregated sales amount for each set.

Last of all I created a column chart bound to the values in G2 and H2. This was a bit tricky to do, but I found the answer in this video from Leila Gharani – you need to create names that return the contents of the ranges G2# and H2# and then use the names in the chart definitions.

The beauty of all this is what when you edit the ranges in the Buckets table in the top left of the worksheet, edit the names of the buckets or add new buckets, the table and chart update automatically.

After doing all this I realised there was another, probably easier way to achieve the same result without using the Map function. All I needed to do was to add new calculated columns to the bucket table to return the sets and values:

Here’s the formula for the Set column in the table above:

"FILTER([Sales].[Product].[Product].MEMBERS, [Measures].[Sales Amount]>=" & 
[@[Lower Bound]] & 
"AND  [Measures].[Sales Amount]<=" & 
[@[Upper Bound]] & 
[@[Bucket Name]] & 
" set"

…and here’s the formula for the Sales column in that table:

= IF(
"[Measures].[Sales Amount]"

I think this second approach should work with any version of Excel since the introduction of tables and cube formulas.

3 thoughts on “Excel Cube Functions, Dynamic Arrays And Lambdas, Part 3: Grouping And Histograms

  1. Hi Chris, Thanks for your sample! Awesome! I am not succeeding in following use case. Is this really not possible? In the first table you show Product and Sales (Column B and C). Assuming the Products are classified by a Category (Fruits and Vegetable) and you want to Display if it’s fruit or vegetable in Column C, based on what you show in column B how would the Excel Cube Function look like? It should dynamically change when you change the value of column B. (like a lookup)

    1. Chris Webb – My name is Chris Webb, and I work on the Fabric CAT team at Microsoft. I blog about Power BI, Power Query, SQL Server Analysis Services, Azure Analysis Services and Excel.
      Chris Webb says:

      The first thing to do is add Category as a column in a table in the Excel Data Model (aka Power Pivot). After that… well I was trying to do something similar this weekend and I haven’t succeeded yet – I saw some strange errors that I think may be bugs.

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.