March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi team, I need help in writing a measure for last month till today date.
Condition: If I select December month from slicer then November month value should be shown till 12th only not for entire month because today's date is 12 December . But when I select November month from slicer then it should compare complete October month. Kindly help.
Thanks !
Solved! Go to Solution.
@bchager yes your code is working perfectly which is handling previous year also. May be my DAX is looks working because my slicer has YearMonth column which is single select only.
But I am going to use your code only to avoid any issue in future.
Thank you again for helping me.
@bhuprakashs Here is a solution. It also handles a change in year. I included a, "Running total by month" column within the table visualization for testing. Sample .pbix is attached.
Hi @bchager , I found one solution on the web which is working for me. Here is the syntex. Kindly check.
@bchager yes your code is working perfectly which is handling previous year also. May be my DAX is looks working because my slicer has YearMonth column which is single select only.
But I am going to use your code only to avoid any issue in future.
Thank you again for helping me.
@bhuprakashs Glad I could help. That was a fun one to figure out and I hope for the benefit of others that you'd mark my solution as the solution.
@bhuprakashs That does not fully work. Not with my example .pbix at least. It's not handling December or a change in year the way the updated solution I posted earlier today does.
Hi @bchager - This solution is working fine If I select December month from the slicer which shows counts till 13 Nov in Last month measure. But when I am selecting November month from slicer then last month measure is showing till 13 Oct months counts which should show for complete Oct month as Nov also has complete month dates.
Could you please handle this situation also in the above DAX? Thank you so much for the help.
Hi @bhuprakashs ,
Let's assume you have a Calendar table and a Customers fact table, and these tables are related by a date column (e.g., Order_Date in the Customers table). We want to calculate the sales for the exact same period in the previous month based on the latest sale date in the Customers table.
1- Create a new calculated column in the Calendar table to flag dates that are part of the same period as the previous month based on the most recent sales date.
isPast =
VAR lastSaleDate = MAX('Customers'[Order_Date]) // Get the latest sale date from the fact table
VAR lastSaleDatePY = EDATE(lastSaleDate, -1) // Get the same date from the previous month
RETURN
'Calendar'[Date] <= lastSaleDatePY // Check if the current date in the Calendar table is on or before the previous month's date
2- Next, we define a measure to calculate the sales for the same period, one month ago, ensuring that we're working with the exact same period (e.g., last month's sales).
Grand Total Sales Amount Last Month =
VAR total =
CALCULATE(
[Grand Total Sales Amount], // Replace with the measure that calculates your total sales
DATEADD('Calendar'[Date], -1, MONTH), // Shift the date by one month backward
'Calendar'[isPast] // Apply the filter to ensure we're only considering the same period last month
)
RETURN
total
@Bibiano_Geraldo - thank you let me work on the solution . I will confirm you if that works.
Great, take your time
Hi @bhuprakashs ,
Its this working?
Hi @Bibiano_Geraldo - I have sent your DAX to my co-worker to make changes in the symetic data model , most probably it will be available by today. I will confirm you over the weekend or by Monday.
@bhuprakashs Does this solution work for you? Sample .pbix attached.
@bchager - I need to calculate last month (Nov) counts till 12 if I select December month from slicer even if November month has full month data. I do not want to calculate YTD value.
Hi @bhuprakashs
Question, so if I select December, both November and December MTD should appear?
Also and as always, please post a workable sample data.
Proud to be a Super User!
User | Count |
---|---|
96 | |
87 | |
84 | |
71 | |
46 |
User | Count |
---|---|
180 | |
147 | |
89 | |
74 | |
60 |