cancel
Showing results for
Did you mean:
Helper I

## PREVIOUSMONTH with wrong total and subtotal

Hello,

I am trying to calculate the previous month value of transactions with the following code :

``````Transactions PM =
VAR LastDayAvailable =
CALCULATE (
MAX ( TKT[TKT_DATE] ),
ALL ( TKT )
)
VAR CurrentDates =
FILTER (
VALUES ( Date[Date] ),
Date[Date] <= LastDayAvailable
)
VAR Result =
CALCULATE (
[Number of transactions],
PREVIOUSMONTH ( CurrentDates )
)
RETURN Result``````

The results are good concerning each indivudual months. However the totals are wrong as seen on screenshot.

1 ACCEPTED SOLUTION
Super User

@mpatel - This looks like a measure totals problem. Very common. See my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

Check out my External Tool for Power BI Desktop! Microsoft Hates Greg's Quick Measures
Check out my latest book!

5 REPLIES 5
Super User

@mpatel , if you have date you can use time intelligence with date table

``````MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
previous month value =  CALCULATE(sum('table'[total hours value]),previousmonth('Date'[Date]))
``````

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 :
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-Y...

Dashboard of My Blogs !! Connect on Linkedin

Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!
Helper I

Dear @amitchandak

Thanks for the reply but your solution does not work for me.

As I said, my problem is with the totals and subtotals.

Regards,

Super User

@mpatel , because the previous month  is in context so grand total will on last month

try like

sumx(summarize(Table, Table[year], Table[Month],"_1",[Transactions PM]),[_1])

Dashboard of My Blogs !! Connect on Linkedin

Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!
Super User

@mpatel - This looks like a measure totals problem. Very common. See my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

Check out my External Tool for Power BI Desktop! Microsoft Hates Greg's Quick Measures
Check out my latest book!

Helper I

Dear @Greg_Deckler,

Thank you very much, you put me on the right track.

Just add the following code in the return :

``RETURN IF(HASONEVALUE(Date[Date_id_mois]);Result)``

It works great !

Announcements

#### Launching new user group features

Learn how to create your own user groups today!