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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Midway
Helper I
Helper I

Help Modifying this DAX Formula

I have this formula, that someone here posted it in another thread,

The below DAX formula defaults sales for current month and when other visual month is selected, it shows the correct sales for the selected month; this measure is showing in a card visual;

var dt = eomonth( MAXX(summarize(FactSageProject, DimDate[Date] ), dimdate[date]), 0)
return
CALCULATE(
SUM(FacttSALES]),
    DATESBETWEEN( DimDate[Date], EOMONTH( dt, -1) + 1, dt )
)
This works really well to calculate a single field from the fact table.
 
Now, I'm trying to modify it to get the margin from the same fact, the other field in the fact is COST.
Trying to modify like below;
 
var dt = eomonth( MAXX(summarize(Fact, DimDate[Date] ), dimdate[date]), 0)
return
CALCULATE(
SUM(Fact[SALES]) - SUM(Fact[COST])
    DATESBETWEEN( DimDate[Date], EOMONTH( dt, -1) + 1, dt )
)

by default it shows the correct margin for the current month (Card visual), when selecting current month, it shows number correctly as well but when selecting other month,  the number shows a zero or blank.
How can I make this work so it calculates margins for the rest of the selected months?

 

Thanks in advance for your help!

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Midway 

If both cost and sales are coming from the same table, then you need to remove the filter from the month slicer and any other column that is used as a sort by or a group by column. Even safer to remove tye filter from the date table completely. 

var dt = eomonth( MAXX(summarize(Fact, DimDate[Date] ), dimdate[date]), 0)
return
CALCULATE(
SUM(Fact[SALES]) - SUM(Fact[COST])
    DATESBETWEEN( DimDate[Date], EOMONTH( dt, -1) + 1, dt ),
ALL ( DimDate )
)
 
but what I don't understand is the reason of not using the month directly in the formula like for example 
CALCULATE(
SUM(Fact[SALES]) - SUM(Fact[COST]),
  • DimDate[EndOfMonth] = MAX ( DimDate[EndOfMonth] )
)

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @Midway 

If both cost and sales are coming from the same table, then you need to remove the filter from the month slicer and any other column that is used as a sort by or a group by column. Even safer to remove tye filter from the date table completely. 

var dt = eomonth( MAXX(summarize(Fact, DimDate[Date] ), dimdate[date]), 0)
return
CALCULATE(
SUM(Fact[SALES]) - SUM(Fact[COST])
    DATESBETWEEN( DimDate[Date], EOMONTH( dt, -1) + 1, dt ),
ALL ( DimDate )
)
 
but what I don't understand is the reason of not using the month directly in the formula like for example 
CALCULATE(
SUM(Fact[SALES]) - SUM(Fact[COST]),
  • DimDate[EndOfMonth] = MAX ( DimDate[EndOfMonth] )
)

hi tamerj1,

that worked! thanks so much!

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.