# 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:

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

And in cell H2 I added this formula:

```MAP(
G2#,
LAMBDA(
s,
IF(
CUBESETCOUNT(s)>0,
CUBEVALUE(
"ThisWorkbookDataModel",
s,
"[Measures].[Sales Amount]"),
0)
)
)
```

Here’s what these two formulas return:

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:

```=CUBESET(
"ThisWorkbookDataModel",
"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(
CUBESETCOUNT(
[@Set])>0,
CUBEVALUE(
"ThisWorkbookDataModel",
[@Set],
"[Measures].[Sales Amount]"
),
0
)
```

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 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.

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