The ultimate Microsoft Fabric, Power BI, Azure AI, and SQL learning event: Join us in Stockholm, September 24-27, 2024.
Save €200 with code MSCUST on top of early bird pricing!
Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
I developed the following measure in order to report on the customer first order date, using publicly available Contoso data model. I want to put this measure on the table visual with the granularity of order.
Here is the code:
DEFINE
MEASURE customer[FirstOrderDate] =
CALCULATE (
MIN ( Sales[Order Date] ),
FILTER (
ALL (Sales),
Sales[CustomerKey] = SELECTEDVALUE ( sales[CustomerKey] )
)
)
EVALUATE
SUMMARIZECOLUMNS (
Customer[CustomerKey],
Sales[Order Number],
FILTER ( customer, Customer[CustomerKey] = 6 ),
"FirstOrderDate", [FirstOrderDate]
)
It produce the correct values for a filtered customer. However, when I run this against full customer base, it throws a timeout. Any idea on how to improve the measure so it could calculate succesfully, without timeouts?
Link to DAX.DO: https://dax.do/wxU6NNRHrencrg/
Solved! Go to Solution.
DEFINE
MEASURE Customer[FirstOrderDate] =
CALCULATE (
MIN ( Sales[Order Date] ),
// This works for any set of customers
// not only one. If there is a set of
// customers visible in the current context
// the date of the first order for the
// whole set of customers is returned.
VALUES ( Sales[CustomerKey] ),
ALL ( )
)
EVALUATE
SUMMARIZECOLUMNS (
Customer[CustomerKey],
Sales[Order Number],
"FirstOrderDate", [FirstOrderDate]
)
Piece of advice: NEVER, ever filter a table if you can filter a column. To know why this is so, please grab yourself the book "The Definitive Guide to DAX" by The Italians and read.
@daxer-almighty It works super-fast. However, it's still not clear to me, as your measure definition looks totally different then mine - may I ask you for additional explanation on how this measure actually evaluates?
@Anonymous
You should start reading the articles on www.sqlbi.com if you want to know how to write correct and fast DAX. There is no better way and, in fact, no OTHER way (apart from their book). I've read this book "The Definitive Guide to DAX" by Marco Russo and Alberto Ferrari at least 4 times, cover to cover. I'd suggest you do the same if you dare 🙂 (But that's what has made me a DAX guru).
How does my measure work? It's rather a simple formula but it takes a tiny bit of knowledge to do it.
DEFINE
MEASURE Customer[FirstOrderDate] =
CALCULATE (
MIN ( Sales[Order Date] ),
// VALUES ( Sales... ) gets the currently
// visible customer keys in Sales and re-applies
// them after ALL ( ) has got rid of all
// the other filters. This is how you make sure
// that your measure only calculates in a different
// filter context, one where only the column Sales[CustomerKey]
// has a filter on it.
VALUES ( Sales[CustomerKey] ),
// The directive ALL ( ) removes ALL
// FILTERS from the model.
ALL ( )
)
....
@daxer-almighty One additional question regarding discussed topic - As a given customer could be given different Customer IDs in my data setup (due to SCD), I wanted to replace:
VALUES(Sales[CustomerKey])
with
Values(Customer[CustomerEmail])
but then measure slowed down drastically. Could you please advice me on how to achieve this. I imagine I need some sort of LOOKUPVALUE, that should be passed to VALUES(), however VALUES() does not accept expression as a parameter. Thanks in advance for your help!
DEFINE
MEASURE Customer[FirstOrderDate] =
CALCULATE (
MIN ( Sales[Order Date] ),
// This works for any set of customers
// not only one. If there is a set of
// customers visible in the current context
// the date of the first order for the
// whole set of customers is returned.
VALUES ( Sales[CustomerKey] ),
ALL ( )
)
EVALUATE
SUMMARIZECOLUMNS (
Customer[CustomerKey],
Sales[Order Number],
"FirstOrderDate", [FirstOrderDate]
)
Piece of advice: NEVER, ever filter a table if you can filter a column. To know why this is so, please grab yourself the book "The Definitive Guide to DAX" by The Italians and read.
Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.
Check out the August 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
21 | |
18 | |
15 | |
14 | |
14 |
User | Count |
---|---|
37 | |
33 | |
22 | |
18 | |
17 |