Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Roseventura
Responsive Resident
Responsive Resident

Can you use VARs with CALCULATETABLE (or SUMMARIZECOLUMN) ?

...Just a general question I guess...

 

Can you use VARs with CALCULATETABLE?

 

Here's my issue:

 

I'm trying create a virtual or intermediate table which shows these results:
 
All Customers where the first date they ordered is within user-selected Churn Time Period (can be from 30 to 390 days)
AND their totals sales from the first order date thru the current date and the count of those orders.
 
Assume the user selected 60 days from the filter and the current date is 10/13/21, then they want to see
all orders for customers who ordered for the very first time since 8/13/21, the total Sales $$ for
all their orders, and how many orders they placed.
 
The resulting table would show this:
 
Customer      First Order Date     Total $$ Since First Order      No. Orders   
NEAC8/17/20212641
AZAX8/18/20213,3334
UKBQ8/23/20215,6605
WAB48/24/20211,0202
ITKR8/31/20219585
MAA79/1/20214,2107
DECZ9/2/20211782
CAH69/3/20212,0204
CNBH9/3/20213383
MNDK9/8/20217,7259
ITDC9/10/2021451
CAA59/13/2021791
ITLK9/13/20218255
GYKV9/21/20216336
ITK99/21/20215,15212
IHCB9/22/20211,9969
TXD79/22/20212,89214
AKAW9/27/20217593
CAH79/29/20218563
ITGY9/29/20211,9788
ZDHH10/1/20212505
INC710/4/202118,20624
ITGO10/4/20212624
UTCZ10/6/20214594
 
 

 

I tried using CALCULATETABLE, SUMMARIZE, SUMMARIZECOLUMN and neither will let me specify variables (or I'm doing something wrong).  The VAR needs to be the Churn Time Period Value (user selected).  Every video I've watched shows how to use these measures, but they all hard-code in the variable  (i.e., [Date] = 10/13/2020 ).  What if the date isn't known?  The user needs to decide the time period.

 

 

1 ACCEPTED SOLUTION
V-pazhen-msft
Community Support
Community Support

@Roseventura 
You cannot create a table using measure, neither CALCULATETABLE or SUMMARIZE. What you can do is create a var table and refer to the table content in the return. For example.

 

Measure =

VAR SelectedTime = 'Churn Time Period'[Churn Time Period Value]
var virtual table_ =SUMMARIZE(Shipments, Customers[CUSTOMER],Shipments[Date], "Total Sales", MAX([Total Sales]), "Total Customers", MAX([Total Customers]))
 
Return
Sumx(Filter(virtual table_ ,[Date] < today() - SelectedTime), [Total Sales])
 

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly

 
 

View solution in original post

6 REPLIES 6
V-pazhen-msft
Community Support
Community Support

@Roseventura 
You cannot create a table using measure, neither CALCULATETABLE or SUMMARIZE. What you can do is create a var table and refer to the table content in the return. For example.

 

Measure =

VAR SelectedTime = 'Churn Time Period'[Churn Time Period Value]
var virtual table_ =SUMMARIZE(Shipments, Customers[CUSTOMER],Shipments[Date], "Total Sales", MAX([Total Sales]), "Total Customers", MAX([Total Customers]))
 
Return
Sumx(Filter(virtual table_ ,[Date] < today() - SelectedTime), [Total Sales])
 

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly

 
 
Roseventura
Responsive Resident
Responsive Resident

Ibendlin,

 

I use VARs a lot in my measures, but I've never used them in a table measure which is what I'm trying to do here.  And it's not working.

 

The above table that I provided IS the desired result.  I have a data set that goes back to 2019 thru current, so if the user chose 60 from the Churn Time Period filter, it would look ONLY at those customers who had their first order ever with us sometime between 8/13/21 and 10/13/21 (60 days).  Here's the table measure I'm using:

 

Calculatetable =
VAR SelectedTime = 'Churn Time Period'[Churn Time Period Value]
return
CALCULATETABLE(SUMMARIZE(Shipments,
Customers[CUSTOMER],
Shipments[Date]),
filter( Shipments, Shipments[Date] < today() - SelectedTime ),
"Total Sales", [Total Sales],
"Total Customers", [Total Customers] )
 
 
The above table measure results this message, but I'm not sure where in the table measure this is occurring:
 
The True/False expression does not specify a column. Each True/False expressions used as a table filter expression must refer to exactly one column.
 
 

Calculatetable =
VAR TodayMinusSelectedTime = today() - 'Churn Time Period'[Churn Time Period Value]
return
CALCULATETABLE(SUMMARIZE(Shipments,
Customers[CUSTOMER],
Shipments[Date]),
filter( Shipments, Shipments[Date] < TodayMinusSelectedTime ),
"Total Sales", [Total Sales],
"Total Customers", [Total Customers] )

Hi Ibendlin,

 

I'm still getting the "True/False expression...." error message with your suggestion.  I'm not sure where in the measure that True/False error is occurring.  I removed the filter portion of the measure thinking maybe that was causing the error, but I still get the error.

 

 

Please provide sanitized sample data that fully covers your issue. Paste the data into a table in your post or use one of the file services. 

lbendlin
Super User
Super User

Not only can you use variables, you can even use them inside each of the parameters of any of the DAX functions and you can nest them as often as you want.  Remember that you need to RETURN a value in that case.

 

You already provided sample data - can you also indicate the expected outcome?

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors