Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be 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

Reply
bhuprakashs
Frequent Visitor

Calculate Last Month till Today date

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 !

2 ACCEPTED SOLUTIONS

@bhuprakashs This should do it. .pbix attached.

 

View solution in original post

 @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. 

View solution in original post

15 REPLIES 15
bchager
Super User
Super User

@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.

Previous Month's value to today's day =
VAR CurrentDate = MAX('Table'[Date]) -- Captures the selected date from the slicer
VAR TodaysDay = DAY(TODAY()) -- Extracts today's day of the month
VAR PrevMonthYear = IF(MONTH(CurrentDate) = 1, YEAR(CurrentDate) - 1, YEAR(CurrentDate)) -- Adjusts year for December
VAR PrevMonthNumber = IF(MONTH(CurrentDate) = 1, 12, MONTH(CurrentDate) - 1) -- Adjusts month for December
VAR PrevMonthStartDate =
    DATE(
        PrevMonthYear,
        PrevMonthNumber,
        1
    )
VAR PrevMonthEndDate =
    DATE(
        PrevMonthYear,
        PrevMonthNumber,
        TodaysDay
    ) -- Sets the end date to the current day of the previous month
RETURN
CALCULATE(
    SUM('Table'[Value]),
    'Table'[Date] >= PrevMonthStartDate,
    'Table'[Date] <= PrevMonthEndDate
)

 

 

 

Hi @bchager , I found one solution on the web which is working for me. Here is the syntex. Kindly check.

Previous Month's value to today's day  =
CALCULATE (
    sum ( value ),
    DATEADD (
        FILTER ( DATESMTD ( Dates[Date] ), Dates[Date] <= TODAY () ),
        -1,
        MONTH
    )
)

 @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.

@bhuprakashs This should do it. .pbix attached.

 

Bibiano_Geraldo
Resident Rockstar
Resident Rockstar

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

 

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 

@Bibiano_Geraldo - thank you let me work on the solution . I will confirm you if that works. 

Great, take your time

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 

Hi @bhuprakashs ,

Its this working?

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 

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. 

bchager
Super User
Super User

@bhuprakashs Does this solution work for you? Sample .pbix attached.

Value YTD =
IF(
    MAX('Table'[Date]) <= TODAY(),
    CALCULATE(
        SUM('Table'[Value]),
        FILTER(
            ALL('Table'),
            'Table'[Date] <= MAX('Table'[Date])
        )
    )
)

 

@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.

danextian
Super User
Super User

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.










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.