Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more
Hello everyone!
I came up with a solution to a problem that works, but I am not sure whether it is the best solution. However, regardless of that, I don't understand why one of my many other trials doesn't work given my final solution.
I want to clarify why my final solution works, but not my other one.
Here is a gross summary of what I was trying to achieve:
[OPTIONAL READING: For the curious reader, we always start with company averages rather than taking the granular salary entries to ensure that large companies do not skew the calcs.
Furthermore, the third step (taking the average of the medians) becomes "necessary" when someone's job encompasses the responsibilities of two (or more) jobs, but the underlying data is scarse for one or more of the jobs; again, trying not to skew and take the arithmetic average instead.
Maybe not the best, I don't know, but that's what the specs were.]
Basically, we want the average median of the averages.
The following code does exactly what we want:
Measure_Works = AVERAGEX( SUMMARIZE( SUMMARIZE( 'FactCompensation'; DimCompany[CompanyID]; DimFunction[FunctionId]; "Average"; AVERAGE(FactCompensation[CompensationAmount]) ); DimFunction[FunctionId]; "Percentile"; PERCENTILEX.INC( SUMMARIZE( 'FactCompensation'; DimCompany[CompanyID]; DimFunction[FunctionId]; "Average"; AVERAGE([CompensationAmount]) ); [Average]; 0,50 ) ); [Percentile] )
However, this is hard to understand, and the code contains duplicate sections. At first, I had tried to use "var" in order to split the calculations into logical groupings, and also avoid code repetitions altogether. My logic was this:
This is what the code looked like (doesn't work):
Measure_DoesNotWork = var tblAverageByOrg = SUMMARIZE( FactCompensation; DimCompany[CompanyID]; DimFunction[FunctionId]; "Average"; AVERAGE(FactCompensation[CompensationAmount]) ) var tblPercentiles = summarize( tblAverageByOrg; DimFunction[FunctionId]; "Percentile"; PERCENTILEX.INC(tblAverageByOrg; [Average]; 0,50) ) return AVERAGEX(tblPercentiles; [Percentile])
If you replace the variables used by their definition, you'll quickly see that the second code snippet becomes exactly the same as the first code snippet (which works).
Both measures do not return the same results.
Am I mistaken in thinking that they should have returned the same results? Am I missing something?
I have prepared a folder in which you'll be able to see what I mean. The folder includes a Power BI with the data imported in it, as well as an Excel spreadsheet that replicates both calculations (one that is OK, one that is wrong), and have provided the DAX calculations for both measures as well. In the Excel spreadsheet, you'll see that the second step of the calculations (calculating the median of the averages by function) does not work; it calculates the median over all the rows, irrespective of function.
I have shared the folder from my personal Google Driver. Please find it here: PBI_Embedded_Summarize_Example.
Why does the first code snippet work, but not the second one?
Thank you!
Philippe
Solved! Go to Solution.
Hey,
you should just use SUMMARIZE to group rows and add columns using ADDCOLUMNS(...), see here:
https://www.sqlbi.com/articles/best-practices-using-summarize-and-addcolumns/
Under certain circumstances it's still favorable to use ADDCOLUMNS(SUMMARIZRE(...),...) instead of SUMMARIZECOLUMNS, but the aritcle above is also mentioning this.
Not sure if this will help, but SUMMARIZE is an iterator, meaning maybe you should consider to wrap you expression into a CALCULATE(...) to enforce context transitiont.
Just wondering, without using your sample files
Regards,
Tom
Hey,
you should just use SUMMARIZE to group rows and add columns using ADDCOLUMNS(...), see here:
https://www.sqlbi.com/articles/best-practices-using-summarize-and-addcolumns/
Under certain circumstances it's still favorable to use ADDCOLUMNS(SUMMARIZRE(...),...) instead of SUMMARIZECOLUMNS, but the aritcle above is also mentioning this.
Not sure if this will help, but SUMMARIZE is an iterator, meaning maybe you should consider to wrap you expression into a CALCULATE(...) to enforce context transitiont.
Just wondering, without using your sample files
Regards,
Tom
Check out the April 2025 Power BI update to learn about new features.
Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.
User | Count |
---|---|
93 | |
58 | |
44 | |
35 | |
34 |