Forum Discussion

Midway's avatar
Midway
Helper I
1 year ago
Solved

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!

 

  • 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] )
    )

2 Replies

  • tamerj1's avatar
    tamerj1
    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] )
    )