Forum Discussion
Top n vs. Others in Excel
- Feb 04, 2026
Thank you very much for providing the full context. Looking at the chart I better understand what Your challenge was 🤔
The attached option uses the same approach we've been discussing these last days. In summary:
- TableSource is filtered by the Slicer(s)
- PIVOTBY calc. the %age by [Year] & [Brand] relative to the Row Total. Data are sorted DESC according to [Brand] "best Total"
- The formula (below):
- Takes the PIVOTBY resulting array to Group [Brand] above - if any - the TOPN parameter
- Padds the [Brand] left with the appropriate number of Zero Width Space chars. to ensure [Brand] will be ordered according to there rank in the PivotTable & Chart, with [Grouped Brand] always as the last [Brand] whatever its rank is
- Unpivots the data as a single array that's used as PivotTable Data Source
PivotTable
- [Brand] must be sorted A-Z
- Until MSFT releases the PivotTable Auto Refresh functionality, any change on the Slicer(s) requires a Refresh of the PivotTable
Side effect of the Zero Width Spaces
- None on screen
- According to my researches none on printing - to be checked though...
- [Brand] on PivotTable/Chart <> TableSource[Brand]*
* I will provide more information on a separate post later
Formula
I did my best to make the variable names as clear as possible and decomposed as much as possible - any question let me know:
// Source_PivotTable =LET( TOPN_value, IF( ISBLANK( SETTINGS!TOPN_Default ), 5, SETTINGS!TOPN_Default ), GROUPED_BrandName, IF( ISBLANK( SETTINGS!TOPN_GROUPED_Names ), "Other Brands", SETTINGS!TOPN_GROUPED_Names ), pivotedAllBrands, PIVOTBY( TableSource[Year], TableSource[Brand], TableSource[Values Month], PERCENTOF, 0, 0, 1, 0, -2, TableSource[NOT_Filtered], 1 ), OtherBrand_Exists?, COLUMNS( pivotedAllBrands) -1 > TOPN_value, groupedOtherBrand_If_Exists, IF( OtherBrand_Exists?, LET( TopnBrand_series, CHOOSECOLS( pivotedAllBrands, SEQUENCE(, TOPN_value, 2 ) ), TopnBrand_values, DROP( TopnBrand_series, 1 ), GroupedBrand_values, BYROW( TopnBrand_values, LAMBDA(rw, 1 -SUM(rw) ) ), AllBrand_series, VSTACK( HSTACK( TAKE( TopnBrand_series, 1 ), GROUPED_BrandName ), HSTACK( TopnBrand_values, GroupedBrand_values ) ), AllBrand_series ), pivotedAllBrands ), prep_UnPivoting_BEG, "------------------------------------------------------", LeftCol_ToDrop, --NOT( OtherBrand_Exists? ), brandNames, DROP( TAKE( groupedOtherBrand_If_Exists, 1 ),, LeftCol_ToDrop ), brandCount, COLUMNS( brandNames ), brandRanks_Desc, SEQUENCE(, brandCount, brandCount, -1 ), paddedBrandNames, REPT( UNICHAR(8203), brandRanks_Desc ) & brandNames, years, DROP( TAKE( pivotedAllBrands,, 1 ), 1 ), values, DROP( groupedOtherBrand_If_Exists, 1, LeftCol_ToDrop ), valuesNotBlank, NOT( ISBLANK( values ) ), prep_UnPivoting_END, "------------------------------------------------------", UnPivotedData, LET( UnPivot, LAMBDA(array, TOCOL( IF( valuesNotBlank, array ) ) ), UnPivoted_years, UnPivot( years ), UnPivoted_brands, UnPivot( paddedBrandNames ), UnPivoted_values, TOCOL( values ), HSTACK( UnPivoted_years, UnPivoted_brands, UnPivoted_values ) ), VSTACK( {"Year", "Brand", "Value"}, UnPivotedData ) )Cheers
Lz.
- Thank you for marking as Solved
- You can safely delete Measure [Total Amount] in the Data Model/Power Pivot. It was used to identify the TOP 5 with the CUBE/MDX approach. It's no longer needed with the CUBE365 one. Sorry for not deleting it earlier
- I will appreciate you later confirm the accuracy of the report
Thanks
Lz.
- Anonymous29007Jan 15, 2026Brass Contributor
Hi Lorenzo,
My previous reply (and this one) might be a bit delayed to you because they undergo moderation before they become visible to you (maybe because I'm new here).
You're most welcome. Thank you so much once again for all the time and effort you put into helping me. I greatly appreciate it and you genuinely did solve my problem.
When you see my previous reply, I'm just having some trouble creating the line chart. I haven't created one before from a dynamic (formula-based) PivotTable before so I'm a bit confused on the table range to select for the line chart. If you can please guide me on this, I'll greatly appreciate it. Then, I will be able to confirm the accuracy of the report. But, so far so good 👍.
Kind regards,
YP
- LorenzoJan 16, 2026Silver Contributor
Built the chart by adding a few formulas to the CALC_SHEET:
To check accuracy, built sheet CHECK with a PivotTable from the Data Model. The 2 slicers from the REPORT sheet are also connected to this PivotTable. Below the PivotTable there's a refence to dynamic array 'Top5Pivoted' from the CALC_SHEET
With the slicers connected to the PivotTable, when you're on the CHECK sheet you can use the PivotTable Region & Year filters instead of:
- Switching to the REPORT sheet
- Selecting items on the slicers
- Switching back to the CHECK sheet
Cheers
Lz.- Anonymous29007Jan 16, 2026Brass Contributor
Hi Lorenzo,
Thank you so much, this works perfectly. And thank you so much for creating the chart and for explaining what you've done. I truly appreciate all your time and efforts and I'm so sorry for all the trouble I may have put you through.
This little exercise actually inspires me to delve more deeper and learn the more advanced stuff in Excel.
Thank you so much once again and have a great weekend.
Cheers,
YP