March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
I cannot find the DAX formula that will give me the % of Total results. I added a calculated field (%) to the below power BI table. In that column I am trying to get it to display the results as shown in red. In this case, 541 divided by the total 1558 and 1017 divided by the total 1558. How would I write the % formula to accomplish this? Thx
Solved! Go to Solution.
@Greg_Deckler, I think you meant to use CALCULATE() or switched the order there.
TotalQuantity = SUM( 'Table'[Quantity] ) % Total = [TotalQuantity] / CALCULATE( [TotalQuantity] ,ALL( 'Table' ) ) ***OR*** TotalQuantity = SUM( 'Table'[Quantity] ) % Total = [TotalQuantity] / SUMX( ALL( 'Table' ) ,'Table'[Quantity] )
Either construction would work, but I'd lean toward the former idiom.
In theory, it would be something like the following (as a measure):
= SUM([Quantity]) / SUMX([Quantity],ALL(table))
The idea is that the first SUM should get filtered by the row context but you are specifically including ALL rows in the second SUM(X). SUMX adds the ability to modify the filter context when summing.
@Greg_Deckler, I think you meant to use CALCULATE() or switched the order there.
TotalQuantity = SUM( 'Table'[Quantity] ) % Total = [TotalQuantity] / CALCULATE( [TotalQuantity] ,ALL( 'Table' ) ) ***OR*** TotalQuantity = SUM( 'Table'[Quantity] ) % Total = [TotalQuantity] / SUMX( ALL( 'Table' ) ,'Table'[Quantity] )
Either construction would work, but I'd lean toward the former idiom.
Thanks so much for this, it was surprisingly hard to find a concise write-up on how to do this. I think this about the fifth time I've used one of your DAX approaches, so your contributions are very much appreciated.
@greggyb Thanks for putting this together so succinctly. In applying your solution I found that your first expression should actually be:
TotalQuantity = SUM( 'Table'[Quantity] ) % Total = [TotalQuantity] / CALCULATE( [TotalQuantity] ,ALL( 'Table' ) )
Hello community, greetings from Lima!.
I am using this formula proposed by you:
Building on this - I am trying a similar approach (using your same formula) and not getting the answer I expect.
Effectively, I am building a stacked bar chart where I hope to determine % of total business in each calendar year and month (so year + month is a bar in the chart). I am only showing for our top 15 customers, so by construction, the total % should be < 100 for a given period. The total currently exceeds 100 in each period, so something is definitively wrong with the calculation (namely, the numerator is probably for data for more than just the period for the shown bar).
Do you have advice as to how to fix this? Specifically, I am connected to a company database, so I can only add measures, not calculated columns. Many thanks in advance for your help!
Yeah, I totally fubarred that DAX formula, what I was going for was:
= SUM([Quantity])/SUMX('table',ALL('table'[Quantity]))
I like you splitting it out into separate measures though, makes it cleaner and easier to understand.
That's what I get thinking I can do this stuff from memory sometimes instead of actually firing up Desktop and doing it.
@Greg_Deckler@greggyb I would suggest always use DIVIDE function as it doesn't return errors on zero values and at most data models is faster..Unless you have any other opinion I would love to hear it..
And you guys are super fast - and with well explained answers!!!
@konstantinos, that's a great point, I always forget about DIVIDE.
@Greg_Deckler too much modelling on the first versions of powerpivot/ssas
@konstantinos, have you profiled DIVIDE() to be faster than the division operator? This goes against msot of what I've seen. I typically use DIVIDE() when I'm unsure about someone's data. The denominator in this example is basically guaranteed to return data unless the model is empty, so I see no reason to use DIVIDE().
I try to use the minimally powerful abstractions wherever possible, and let my functions act as indicators about my knowledge of the data.
E.g. TOPN() and SAMPLE() will return the same number of rows if there are no ties. I would use TOPN() if I knew a tie shouldn't be possible, and let the measure blow up if that assumption is wrong and it depends on a strict number of rows returned (e.g. for TOPN(1, ...) expecting implicit conversion to a scalar value). Using SAMPLE() would be "safer", but implies I expect ties.
Similarly, DIVIDE() indicates that I either do not understand the data / measure well, or that I expect denominators = 0 in normal use.
@greggyb No I haven't profiled DIVIDE function but I thought so based on SQLBI article which I trust..
https://www.sqlbi.com/articles/divide-performance/
I think the statement that use of Divide ( or similar ) instead of operator suggests that a measure or data model in not well known or understadable to the author. Sometimes is a matter of style ( like AND() instead of && or the opposite ) or other times the sources are not SQL/DB with data validation but simple excel files that users add manually data prone to errors.
And of course a suggestion on a formula don't implement anything about my (either way limited) knowledge of DAX.
@konstantinos, that article is specifically about the performance of DIVIDE() vs the construction:
IF( [Denominator] <> 0 ,[Numerator] / [Denominator] ,<what to do for [Denominator] = 0> )
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
8 | |
2 | |
1 | |
1 | |
1 |
User | Count |
---|---|
10 | |
3 | |
2 | |
2 | |
2 |