Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
I have a table value in a variable and would like to summarize it, grouping some columns and summing others.
Does anyone know a nice way to achieve it?
Here's what I've tried so far.
I can use SUMMARIZE to group by columns, but I run into trouble trying to use SUM to sum up columns:
I tried giving the columns in my temporary table the name of a real physical table in my model, but it didn't help:
If I use SUMX I can sum up the whole column, but the values that I am grouping by don't seem to make their way into the filter context, so I end up with the grand total on every row:
Now in this toy example I can get the right answers by brute-force filtering every row:
But in my real-world use case, my table variable has hundreds of thousands of rows and I need to group by ~20 columns, and so this filtering will be really cumbersome to author and maintain, let alone slow to run.
Any ideas for achieving this in a simple and fast way?
Thanks for any tips!
Solved! Go to Solution.
In DAX, it can't determine the current context since the row context is not generated yet when resolving the expression. You have to use a calculated column formula to calculate the results.
For your requirement, I think the easiest way is creating a calculated table instead of table variable. Then summarize this calculated table.
Regards,
fyi - if anyone is looking at this facing the same challenge, it can be solved easily using GROUPBY and CURRENTGROUP().
EVALUATE VAR cars = DATATABLE( "Maker", STRING, "Sales", INTEGER, { { "Ford", 300 }, { "Jaguar", 180 }, { "Jaguar", 1 } }) RETURN GROUPBY( cars, [Maker], "total sales", SUMX(CURRENTGROUP(),[Sales]) )
fyi - if anyone is looking at this facing the same challenge, it can be solved easily using GROUPBY and CURRENTGROUP().
EVALUATE VAR cars = DATATABLE( "Maker", STRING, "Sales", INTEGER, { { "Ford", 300 }, { "Jaguar", 180 }, { "Jaguar", 1 } }) RETURN GROUPBY( cars, [Maker], "total sales", SUMX(CURRENTGROUP(),[Sales]) )
Excellent, Matthew - this saved my day (or at least solved the issue I had, which could not be resolved by a calc. column).
Cudos to you for posting your solution for the internet to consume 🙂
In DAX, it can't determine the current context since the row context is not generated yet when resolving the expression. You have to use a calculated column formula to calculate the results.
For your requirement, I think the easiest way is creating a calculated table instead of table variable. Then summarize this calculated table.
Regards,
Can you explain this like im 5? What does it mean to generate row context when resolving the expression?
What is a calculated column formula vs the table variable? What is a calculated table and how is that different from calculated column and a table variable?
Thank you!
Welcome to the PowerBi community. Where all answers are extremely vague and without context and most "Solved" questions are to "please post this question as a new feature so i can mark this as solved even there is no actual answer"
Thanks very much for the reply. Sounds like a sensible suggestion. Awkwardly for me, we are allowing the analyst to enter some parameters on the fly for the query, and so I'm not able to pre-compute the result in calculted columns or a calculated table.
I was really intrigued by your comment that "the row context is not generated yet when resolving the expression". I've read loads about DAX and done lots of experimentation, but hadn't come across this knowledge before. Is there a book or some kind of online resource I can consult for learning DAX at this level of detail...?
Thanks again!
Matt.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
112 | |
97 | |
84 | |
67 | |
59 |
User | Count |
---|---|
150 | |
120 | |
99 | |
87 | |
68 |