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
Anonymous
Not applicable

DAX Calculation from related table

Hi all,

 

I have this DAX expression and it works fine.

 

PTD Sales =
CALCULATE (
SUM ( Transactions[Sales] ),
FILTER (
Transactions,
Transactions[period_year] = MAX ( Transactions[period_year] )
&& Transactions[date] <= MAX ( Transactions[date] )
)
)
 
Now i have created a seperate Date table and seems like I cannot have the same expression but using the fields from the new data table:
 
PTD Sales =
CALCULATE (
SUM ( Transactions[Sales] ),
FILTER (
Transactions,
Date[period_year] = MAX ( Date[period_year] )
&& Date[date] <= MAX ( Date[date] )
)
 
Can anyone help rewrite the DAX expression to work with the date dimension?
 
Thanks
 
 
 
1 ACCEPTED SOLUTION
Anonymous
Not applicable

I can't tell you the best way to write your particular DAX expression. However, since you are interested in optimizing your DAX, sqlbi.com always publish articles about the best way to optimize DAX.

 

Here is a link to their most recent articles on DAX optimization: 

 

https://www.sqlbi.com/articles/?author=&tag_id=411

 

Also, they have DAX studio which allows you to test how long your DAX takes to run. You can change things up to see if your DAX changes affect performance.

 

Hope that helps.

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

@Anonymous

 

I'm looking at your 2nd expression. It looks like you are filtering the transaction table, but then you don't really specify how to filter the transaction table; your parameters are for a different table.

 

Typically, if I'm trying to filter a specific table based on parameters in another table (related, inactive/active, fact side/dimension side), I use RELATED, RELATEDTABLE, or USERELATIONSHIP in my filter statement.

 

 

Here is an examle from my sqlbi slides...

 

SUMX (
FILTER (
ProductCategory,
COUNTROWS ( RELATEDTABLE ( Product ) ) > 10
),
SUMX ( RELATEDTABLE ( Sales ), Sales[SalesAmount] )
)

Anonymous
Not applicable

 I have written this expression and seems to be working fine... Not sure if this can be optimised in any way or written using best practises?

 

PTD Sales =
CALCULATE (
SUM ( Transactions[Sales] ),
FILTER (
Transactions,
RELATED('Date'[Period Year]) = Max( 'Date'[Period Year] )
&& RELATED('Date'[date]) <= Max( 'Date'[date] )
)
)
Anonymous
Not applicable

Great question. 

 

If you are formatting your DAX properly, which you are obviously doing based on your latest post, you are following best practices with respect to writting/formatting DAX.

 

Keeping up with best practices and your workload can be difficult. The staff at SQLBI do a lot of work to educate people regarding best practices; there is also great content on this board.

 

If you haven't already, check out sqlbi.com. Marco is in some vids with the doods that create Guy In A Cube YouTube content.

 

Well done!

Anonymous
Not applicable

Thanks @Anonymous 🙂

 

But i was not referring to the formatting itself, rather more the logic to ensure it is working efficiently

 

Anonymous
Not applicable

I can't tell you the best way to write your particular DAX expression. However, since you are interested in optimizing your DAX, sqlbi.com always publish articles about the best way to optimize DAX.

 

Here is a link to their most recent articles on DAX optimization: 

 

https://www.sqlbi.com/articles/?author=&tag_id=411

 

Also, they have DAX studio which allows you to test how long your DAX takes to run. You can change things up to see if your DAX changes affect performance.

 

Hope that helps.

danno
Resolver V
Resolver V

can you just confirm the structure of your model - is the transaction date connected to the date dimension via a date key? 

Anonymous
Not applicable

Yes the Transactions table is connected to the DateKey, so relationships are established already.

 

Thanks @danno

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.