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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

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
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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