Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Hello everyone,
I'm trying to create a formula that groups all customers whose purchases represent less than 1% of total sales (later on, I would like to make that 1% vary with a parameter, but I'll solve that later). I have tried to solve this problem in various ways, and one of them is summarizing the table with my customers, filtering them by their sales level, counting the rows, saving that information in a variable, and using it as the value of N in the TOPN formula. But it doesn't work.
Specifically, it doesn't generate any error message, the syntax, in that sense, is correct. It's as if TOPN simply doesn't fulfill its function, it doesn't select the number of rows that the variable represents. I have tried transferring all that information to a measure and using it in TOPN, but it doesn't work either.
I have individually evaluated each part of the formula and confirmed that it works correctly. In fact, if I replace the variable in the TOPN formula with an integer, then the formula as a whole works perfectly. Does anyone know why?
I'm from Spain, so the variables are in Spanish. I hope it's not a problem to understand the code.
By the way, I imagine there are more efficient ways to solve this problem, such as using the summarized and filtered table directly instead of counting its rows and evaluating that value in TOPN, but the truth is that system didn't work for me either.
Thanks
Solved! Go to Solution.
Hello
Ok, I did a mix between your code and what I had and I think the problem is finally resolved.
I must confess that I don't know well why one code works and the other doesn't, but ok.
Thank you so much for your help. This couldn't have been possible without your suggestions.
I'll put the code so that anyone with the same problem can see it.
I don't think you need TOPN at all. You already have the filtered table containing the sales amount, you can simply do a SUMX over that.
I also changed the SUMMARIZE to use ADDCOLUMNS, its best practice to do that rather than adding calculated columns using SUMMARIZE itself
Ventas con Otros =
VAR PorcVentas = [TotalVentas] * 0.01
VAR TablaResumen =
ADDCOLUMNS (
SUMMARIZE ( VENTAS, 'Clientes con Otros'[Nombre] ),
"VentaCliente", [TotalVentas]
)
VAR TablaResumenFiltrada =
FILTER ( TablaResumen, [VentaCliente] > PorcVentas )
VAR VentasTopN =
SUMX ( TableResumenFiltrada, [VentaCliente] )
VAR VentasTodos =
CALCULATE ( [TotalVentas], ALLSELECTED ( 'Clientes con Otros' ) )
VAR VentasOtro = VentasTodos - VentasTopN
VAR EsOtroSeleccionado =
SELECTEDVALUE ( 'Clientes con Otros'[Nombre] ) = "Otros clientes"
RETURN
IF (
ISINSCOPE ( 'Clientes con Otros'[Nombre] ),
IF ( EsOtroSeleccionado, VentasOtro, VentasTopN ),
VentasTodos
)
First of all, thank you for your response.
Second, in fact, what you propose is a path that I tried, and it does seem more logical. The problem here is that I don't know at which exact point the issue is generated in such a way that "Other customers" always receives the same value: the total sum of sales. If I use this information in a pie chart, "Other customers" always represents 50%, no matter if I change the formula of PorcVentas and multiply it by 0.0000001 or by 0.5.
If it helps, let me share the formula for the measure [TotalVentas] = SUM(SALES[Amount]).
EDIT:
I will provide further information. I conducted a test for a 20% percentage, then assigned this variable to a card and checked how well the VentasTodos, VentasTopN, and VentasOtros variables were functioning. I performed the calculations separately in an Excel sheet, and I can assure you that those variables provide the correct values. Therefore, I assume that the issue lies in how the value of VentasOtros is assigned to the "Otros clientes" row (the IF statement at the end of the formula). It seems that instead of assigning that variable, the VentasTodos variable is being assigned.
I also tried something else; I replaced the VentasTodos variable with an integer (formula below). And the value was assigned correctly.
What is the 'Clientes con Otros'[Nombre] column and how is that calculated? How is it related to Sales ?
It's a calculated table:
By the way, I've edited the previous replay with futher information.
It seems to me that when 'Clientes con Otros'[Nombre] is filtered to "Otros clientes" that would result in an empty Sales table. Can you check by returning COUNTROWS(TablaResumen) in a visual with 'Clientes con Otros'[Nombre]?
I dont know if I got you correctly
If I put the meassure in a cart, it shows 2, which is correct, and it changes if I cange the formula for PorcVentas.
Then I put it in a table, this is the result:
Now, If I look for "Otros clientes" in the table, it's not there
EDIT:
Sorry, I used TablaResumenFiltrada in COUNTROWS instead of TablaResumen.
I think that's the problem. You need to change it to
VAR TablaResumen =
SUMMARIZE (
ALL(VENTAS),
'Clientes con Otros'[Nombre],
"VentaCliente", [TotalVentas]
)
Ok, this is the current formula:
Is the FILTER the right way round ? It is filtering for people with more than 20%, should it be filtering for less than 20% ?
Correct, I want to single out those with sales higher than 20%, and group the rest.
I think I've got it. PorcVentas isn't being calculated correctly, its using the sales for the currently selected customer, not all the customers. It should be
VAR PorcVentas = CALCULATE([TotalVentas], ALLSELECTED ( 'Clientes con Otros' ) ) * 0.2
Hello Johny75
I dont know if the problem was there. I did the change you suggested, and now every customer has exactly the same sales value.
If it helps, that value is equivalent to the sum of the customers who meet the condition of being above 20% of sales.
On the other hand, I just figured out that external filters don't change VentasTopN values and I want that. If I filter by a month, for example, I want the 20% of sales to be calculated based on the sales of that specific month, the same goes for VentasTopN, etc.
In TablaResumn you're using ALL(VENTAS), that will remove any filters. You'll need to tweak that to get the right combination of which filters you want to keep and which, if any, you want to remove.
Ok, I replaced it with KEEPFILTERS:
I have a working version of the code,
VAR BoundaryAmount =
CALCULATE( [Amount], ALLSELECTED( 'IDs and others' ) ) * 0.2
VAR CurrentAmount = [Amount]
VAR SummaryTable =
ADDCOLUMNS(
SUMMARIZE( ALL( 'Table' ), 'IDs and others'[Index] ),
"@amount", [Amount]
)
VAR TopSellers =
FILTER( SummaryTable, [@amount] >= BoundaryAmount )
VAR AmountTopSellers = SUMX( TopSellers, [@amount] )
VAR TotalAmount = SUMX( SummaryTable, [@amount] )
VAR AmountOthers = TotalAmount - AmountTopSellers
VAR OthersSelected =
SELECTEDVALUE( 'IDs and others'[Index] ) = "Others"
VAR Result =
IF(
ISINSCOPE( 'IDs and others'[Index] ),
IF(
OthersSelected,
AmountOthers,
IF( CurrentAmount >= BoundaryAmount, CurrentAmount )
),
TotalAmount
)
RETURN
Result
The only thing which would need tweaking is the ALL( 'Table' ). That currently gets rid of any filters, but you will need to rewrite it to selectively keep the filters you want to apply, while removing the filters coming from the visualisation itself.
One key change is that you do not want to return a value if it is less than the boundary - that should instead be grouped into "others".
Hello
Ok, I did a mix between your code and what I had and I think the problem is finally resolved.
I must confess that I don't know well why one code works and the other doesn't, but ok.
Thank you so much for your help. This couldn't have been possible without your suggestions.
I'll put the code so that anyone with the same problem can see it.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
10 | |
9 | |
9 | |
8 | |
8 |
User | Count |
---|---|
14 | |
12 | |
11 | |
11 | |
8 |