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

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.

Reply
Roseventura
Responsive Resident
Responsive Resident

Using VARS with CALCULATETABLE

Could someone please explain to me why this works:

 

LastPurchaseDates =
VAR Churnvalue = 365
RETURN
CALCULATETABLE(
ADDCOLUMNS(
SUMMARIZE(
Shipments,
Customers[CUST_CODE],
"Total Sales", [Total Sales]
),
"LastPurchaseDate", CALCULATE(MAX( Shipments[Ship Date] ))),
Shipments[Ship Date] < today()-churnvalue
)

 

But this does NOT:

 

LastPurchaseDates =
VAR ChurnvalueSELECTEDVALUE('Churn Time Period'[Churn Time Period])
RETURN
CALCULATETABLE(
ADDCOLUMNS(
SUMMARIZE(
Shipments,
Customers[CUST_CODE],
"Total Sales", [Total Sales]
),
"LastPurchaseDate", CALCULATE(MAX( Shipments[Ship Date] ))),
 
Shipments[Ship Date] < today()-churnvalue
)
 
I need to reference the Chur Time Period Value as that can be changed by a user. I can't get this to work
unless I hard-code '365' into the measure.
 
I basically need a table showing the Customer Code, the last purchase date (Ship date), and the total Sales, but I only want the table to return Customers who have not purchased anything in over "N" time period (Churn Time Period Value), selected by user.
 
How can I get this to work?
 
Thanks.
 
 
5 REPLIES 5
Roseventura
Responsive Resident
Responsive Resident

I need to submit sample data.  Please do not close this issue.

v-eqin-msft
Community Support
Community Support

Hi @Roseventura ,

 

Any updates?

 

You could refer to the blogs below to know How to provide sample data in the Power BI Forum and How to Get Your Question Answered Quickly

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Ashish_Mathur
Super User
Super User

Hi,

Writing measures is the correct approach.  Share some data and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
AlexisOlson
Super User
Super User

A calculated table cannot be dynamic based on user selection since calculated tables (and calculated columns) are only calculated once each time the data model is loaded or refreshed rather than in response to slicers or filters from the report page.

 

On the other hand, this is what measures are designed to do. So your ultimate goal is very likely possible--you just need a slightly different approach.

VahidDM
Super User
Super User

HI @Roseventura 

 

Read this : 

https://radacad.com/caution-when-using-variables-in-dax-and-power-bi

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.