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
aaronvincentnz
Helper III
Helper III

Current Month, Previous Month formula

Hi, 

 

I have the following formula(s) which both return the same result, but both results are incorrect (I know the answer I need, hence asking this question).

 

The formulas are as follows

CurrentMonth = CALCULATE(sum('Strategic NPS'[NPS]), DATEADD('Strategic NPS'[Date], 0, MONTH))
CurrentMonth = CALCULATE(sum('Strategic NPS'[NPS]), FILTER ('Strategic NPS','Strategic NPS'[Date]))
 
My source data is formatted as mmmm yyyy (or 01/12/20 if I use the dd/mm/yy format). I'm thinking there's something wrong with my source data set up given both the above formulas give me the same answer (I'd like to use the first formula).
 
I've replicated the source data and the dashboards etc from a colleague so have followed their set up with regards to column names, formats etc, but no luck.
 
Hoping someone can assist.
 
Thanks
1 ACCEPTED SOLUTION

Thanks everyone, I figured out why it wasn't working. I needed to add a date filter and filter by 'top 1 latest date'

View solution in original post

4 REPLIES 4
v-eqin-msft
Community Support
Community Support

Hi @aaronvincentnz ,

 

According to my understanding, you want to calculate the sum of each Retailer ,right?

You could use the difference calculated byDATEDIFF([DATE1],[DATE2],MONTH) function to identify current/previous month.

 

But I'm a little confused about the "Current". If the newest date in table (202012) or today(202101) is current?

 

previous when current is 202012 =
CALCULATE (
    SUM ( 'Strategic NPS'[NPS] ),
    FILTER (
        'Strategic NPS',
        'Strategic NPS'[Retailer] = MAX ( 'Strategic NPS'[Retailer] )
            && DATEDIFF ( [Date], MAXX ( ALL ( 'Strategic NPS' ), [Date] ), MONTH ) = 1
    )
)
previous when current is 202101 =
CALCULATE (
    SUM ( 'Strategic NPS'[NPS] ),
    FILTER (
        'Strategic NPS',
        'Strategic NPS'[Retailer] = MAX ( 'Strategic NPS'[Retailer] )
            && DATEDIFF ( [Date], TODAY (), MONTH ) = 1
    )
)

 

1.27.2.1.PNG

Please take a look at the pbix file here.

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks everyone, I figured out why it wasn't working. I needed to add a date filter and filter by 'top 1 latest date'

AllisonKennedy
Super User
Super User

@aaronvincentnz  You need to use a Date table with time intelligence functions such as DATEADD. You should also use the Date table date in your visual or filter. Finally, your Current Month formula typicallly does not need a filter. 

https://excelwithallison.blogspot.com/2020/04/dimdate-what-why-and-how.html 

 

I suggest using the DATEADD version: 

 

CurrentMonth = SUM('Strategic NPS'[NPS])

 

PrevMonth= CALCULATE(sum('Strategic NPS'[NPS]), DATEADD('DimDate'[Date], -1, MONTH))


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

 

Thanks Allison. I'll have to work through your detail above. Just wondering though why my colleague has been able to get the correct answer when I haven't, despite having set things up (source data wise) the same way they have. Granted, I haven't been able to ask them exactly what they've done, so there might be something behind the scenes that I'm unaware of.

 

As an FYI, here is a sample of what my data looks like and im expecting the results to return the NPS results from Dec-20 for Retailers A, B and C

 

DateRetailerNPSRank
Nov-20A2.41
Nov-20B-16.43
Nov-20C-4.82
Dec-20A61
Dec-20B-22.83
Dec-20C-52

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.