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
abhiram342
Employee
Employee

MOM Calculation based on Last Day of Month

Hi Team,

 

We are storing last day of month as key in our fact tables. We need to calculate mom but I see the previous month value is coming as blank because it doesn't fall on last day of month. 

Exmaple:

For April 2022, Lasy day is 2022-04-30 when we use dateadd(month,-1) then it falls on march 30 and It's expected to be 20220331.

Please suggest how to resolve it

As per our design, we have below logic

Is Latest (( we keep track to latest date from Fact)

Latest Sales Date Key
20220430

Date (( Standard Date Tables)

FactSales

DateIdSalesAmount
20220430200
20220331300

 

DAX Code:

PrevLatest is not working as expected
 
'Fact Sales'[Sales] = CALCULATE(SUM('Fact Sales'[Sales Amount]))
Sales MoM:=
VAR Curr = 'Fact Sales'[Sales]
VAR Prev =
    CALCULATE ( 'Fact Sales'[Sales], DATEADD ( 'Date'[Date], -1, MONTH ) )
VAR LatestDateKey =
    MAX ( 'Is Latest'[Latest Sales Date Key] )
VAR LatestDate =
    CALCULATE ( MAX ( 'Date'[Date] ), 'Date'[Date Key] = LatestDateKey )
VAR PrevLatest =
    CALCULATE (
        'Fact Sales'[Sales]
        DATEADD ( TREATAS ( { LatestDate }, 'Date'[Date] ), -1, MONTH )
    )
VAR CurrMOM =
    IF ( ISBLANK ( Curr ), BLANK (), Curr - Prev )
VAR LatestMOM =
    IF ( ISBLANK ( Curr ), BLANK (), Curr - PrevLatest )
RETURN
    IF ( ISCROSSFILTERED ( 'Date'[Date] ), CurrMOM, LatestMOM)

 

Thanks,

Abhiram

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@abhiram342 , You are storing data only on one date , then all last month formula should work

 

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))

 

 

if you only want the last month-end date

 

Last Month = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[date]=eomonth(max('Date'[Date]),-1) ))

 

 

Why Time Intelligence Fails - Powerbi 5 Savior Steps for TI :https://youtu.be/OBf0rjpp5Hw
https://amitchandak.medium.com/power-bi-5-key-points-to-make-time-intelligence-successful-bd52912a5b...
To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

View solution in original post

2 REPLIES 2
amitchandak
Super User
Super User

@abhiram342 , You are storing data only on one date , then all last month formula should work

 

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))

 

 

if you only want the last month-end date

 

Last Month = CALCULATE(sum('Table'[Qty]), FILTER(ALL('Date'),'Date'[date]=eomonth(max('Date'[Date]),-1) ))

 

 

Why Time Intelligence Fails - Powerbi 5 Savior Steps for TI :https://youtu.be/OBf0rjpp5Hw
https://amitchandak.medium.com/power-bi-5-key-points-to-make-time-intelligence-successful-bd52912a5b...
To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

Thanks @amitchandak ! thank you for you solution. I have update below code as workaround. Please let me know if there are alternative ways

 

VAR PrevLatest =
CALCULATE (
'Fact Sales'[Sales],
'Date'[Date] = EOMONTH(EDATE(LatestDate, -1),0))
)

 

Thanks,

Abhiram

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.

Top Solution Authors