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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

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
Community Champion
Community Champion

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
Community Champion
Community Champion

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
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.