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.