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

CALCULATE, FILTER, LASTDATE/MAX DATE is Not working

This is really weird phenomenal for me.

 

Here is the problem. 

 

Here is my code.

 

Debt-to-Capital = 
VAR x = SUM('Balance Sheet'[Total Debt])
VAR y = SUM('Balance Sheet'[Total Debt]) + SUM('Balance Sheet'[Total Equity])
VAR latest_date = MAX('Balance Sheet'[Date])

RETURN
    CALCULATE(DIVIDE(x,y), FILTER(('Balance Sheet'), 'Balance Sheet'[Date]=latest_date))

 

You would expect Power BI to give you the latest debt-to-capital of the LATEST DATE

 

But my result end up to be the sum of all the years rather than filtering it specifically to the LATEST DATE.

 

Anyone knows why this is happening?

 

I have tried ALL, LASTDATE, MAX whatever. The filter INSIDE my calculate function is NOT WORKING.

 

Here is the table that i am working with. 

 

dragonmaidenz_0-1598153121091.png

 

My code keeps giving me 67.3%. 

 

But I want it to be 54.6%. BASED on the latest date. 

 

Can anyone help me fix this code or bug?

 

Thanks

 

 

 

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@Anonymous - No, I would not expect that at all. Variables are not actually variable. Variables are calculated a single time within a DAX formula when you instantiate them. So, what you are attempting to do will not work. You need to do this:

 

Debt-to-Capital = 
VAR latest_date = MAX('Balance Sheet'[Date])

RETURN
    CALCULATE(DIVIDE(SUM('Balance Sheet'[Total Debt]),SUM('Balance Sheet'[Total Debt]) + SUM('Balance Sheet'[Total Equity])), FILTER(('Balance Sheet'), 'Balance Sheet'[Date]=latest_date))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@Anonymous , refer if these can help

Debt-to-Capital =
VAR latest_date = MAXX(allselected('Balance Sheet'),'Balance Sheet'[Date])

RETURN
CALCULATE(DIVIDE(SUM('Balance Sheet'[Total Debt]),SUM('Balance Sheet'[Total Debt]) + SUM('Balance Sheet'[Total Equity])), FILTER(('Balance Sheet'), 'Balance Sheet'[Date]=latest_date))

or


Debt-to-Capital =
CALCULATE(lastnonblankvalue('Balance Sheet'[Date],DIVIDE(SUM('Balance Sheet'[Total Debt]),SUM('Balance Sheet'[Total Debt]) + SUM('Balance Sheet'[Total Equity]))))

Greg_Deckler
Super User
Super User

@Anonymous - No, I would not expect that at all. Variables are not actually variable. Variables are calculated a single time within a DAX formula when you instantiate them. So, what you are attempting to do will not work. You need to do this:

 

Debt-to-Capital = 
VAR latest_date = MAX('Balance Sheet'[Date])

RETURN
    CALCULATE(DIVIDE(SUM('Balance Sheet'[Total Debt]),SUM('Balance Sheet'[Total Debt]) + SUM('Balance Sheet'[Total Equity])), FILTER(('Balance Sheet'), 'Balance Sheet'[Date]=latest_date))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thanks that works like a charm. What was the issue here? What's wrong w my variables?

@dragonmaidenz - Nothing is wrong with their variables, they only calculate once, when you declare them. Therefore, not exactly "variable"...


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.