cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Advocate I
Advocate I

Summarzing a table variable?

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:

 

eg1.png

 

I tried giving the columns in my temporary table the name of a real physical table in my model, but it didn't help:

eg2.png

 

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:

eg3.png

 

Now in this toy example I can get the right answers by brute-force filtering every row:

eg5.png

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!

2 ACCEPTED SOLUTIONS
Microsoft
Microsoft

@matthewchilton

 

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,

View solution in original post

Advocate I
Advocate I

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])
)

 

View solution in original post

3 REPLIES 3
Advocate I
Advocate I

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])
)

 

View solution in original post

Microsoft
Microsoft

@matthewchilton

 

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,

View solution in original post

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.

Helpful resources

Announcements
secondImage

Happy New Year from Power BI

This is a must watch for a message from Power BI!

December Update

Check it Out!

Click here to read more about the December 2020 Updates!

Community Blog

Check it Out!

Click here to read the latest blog and learn more about contributing to the Power BI blog!

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Mark your calendars and join us for our next Power BI Dev Camp!.

Top Solution Authors
Top Kudoed Authors