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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
it_kwwk
Frequent Visitor

Dynamically calculate 'last month sales amount' when filtering in a table

Hi there, 

I want to show two columns in the same table: one for the sales amount of the last month and one for the sales amount of the current month.

Using DAX, I've already calculated the 'Current Month Sales Amount'. The data shown in the table can dynamically change based on the year and month I select. And the results are correct.

The issue is that the 'Last Month Sales Amount' doesn't work when I use slicers (it appears empty or shows all results as 0). The 'Last Month Sales Amount' only works correctly when no slicers are applied (in other words, in the default case).

 

The screenshot shows below:

it_kwwk_0-1736143861230.png

 

Here is the DAX of LastMonth sales:

 

 

LastMonth_Sales = 
VAR SelectedYear = COALESCE(SELECTEDVALUE('Append2_for line chart and table'[DocYear]), YEAR(TODAY()))
VAR SelectedMonth = COALESCE(
    SWITCH(
        SELECTEDVALUE('Append2_for line chart and table'[DocMonth]),
        "January", 1, "February", 2, "March", 3, "April", 4,
        "May", 5, "June", 6, "July", 7, "August", 8,
        "September", 9, "October", 10, "November", 11, "December", 12,
        BLANK()
    ),
    MONTH(TODAY())
)
VAR LastMonth = MOD(SelectedMonth - 2, 12) + 1  // Efficient calculation for last month
VAR LastMonthYear = SelectedYear - IF(LastMonth > SelectedMonth, 1, 0)  // Adjust year if crossing January
VAR StartDateLast = DATE(LastMonthYear, LastMonth, 1)
VAR EndDateLast = EOMONTH(StartDateLast, 0)

RETURN
CALCULATE(
    [Filtered_Sales_Amount],
    DATESBETWEEN(
        'Append2_for line chart and table'[DocDate],
        StartDateLast,
        EndDateLast
    )
)

 

 

Here is the DAX of CurrentMonth Sales:

 

CurrentMonth_Sales = 
VAR SelectedYear = COALESCE(SELECTEDVALUE('Append2_for line chart and table'[DocYear]), YEAR(TODAY()))
VAR SelectedMonth = COALESCE(
    SWITCH(
        SELECTEDVALUE('Append2_for line chart and table'[DocMonth]),
        "January", 1, "February", 2, "March", 3, "April", 4,
        "May", 5, "June", 6, "July", 7, "August", 8,
        "September", 9, "October", 10, "November", 11, "December", 12,
        BLANK()
    ),
    MONTH(TODAY())
)
VAR StartDate = DATE(SelectedYear, SelectedMonth, 1)
VAR EndDate = EOMONTH(StartDate, 0)

RETURN
CALCULATE(
    [Filtered_Sales_Amount],
    DATESBETWEEN(
        'Append2_for line chart and table'[DocDate],
        StartDate,
        EndDate
    )
)

 

 Can anyone solve this? Thank you!

1 ACCEPTED SOLUTION
rajendraongole1
Super User
Super User

Hi @it_kwwk  - you need to adjust the context in the CALCULATE function so it ignores or manipulates the slicer filters appropriately.

Update your last month sales measure as below:

LastMonth_Sales =
VAR SelectedYear = COALESCE(SELECTEDVALUE('Append2_for line chart and table'[DocYear]), YEAR(TODAY()))
VAR SelectedMonth = COALESCE(
SWITCH(
SELECTEDVALUE('Append2_for line chart and table'[DocMonth]),
"January", 1, "February", 2, "March", 3, "April", 4,
"May", 5, "June", 6, "July", 7, "August", 8,
"September", 9, "October", 10, "November", 11, "December", 12,
BLANK()
),
MONTH(TODAY())
)
VAR LastMonth = MOD(SelectedMonth - 2, 12) + 1 // Efficient calculation for last month
VAR LastMonthYear = SelectedYear - IF(LastMonth > SelectedMonth, 1, 0) // Adjust year if crossing January
VAR StartDateLast = DATE(LastMonthYear, LastMonth, 1)
VAR EndDateLast = EOMONTH(StartDateLast, 0)

RETURN
CALCULATE(
[Filtered_Sales_Amount],
DATESBETWEEN(
'Append2_for line chart and table'[DocDate],
StartDateLast,
EndDateLast
),
REMOVEFILTERS('Append2_for line chart and table'[DocYear], 'Append2_for line chart and table'[DocMonth]) // Remove slicer context on year and month
)

 

Verify that the LastMonth_Sales column displays the correct values for the previous month, even when slicers are used.I hope this works.





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

Proud to be a Super User!





View solution in original post

3 REPLIES 3
Ray_Minds
Responsive Resident
Responsive Resident

Hi @it_kwwk 

  1. Need to create a Date table  based on the Min and Max column of the Sales table as i am using finance table .
    Dax to create Table :- Date Dimension = GENERATESERIES ( MIN ( financials[Date] ), MAX ( financials[Date]),1)
    create year column :- Year = YEAR('Date Dimension '[Date])
    Month = MONTH('Date Dimension'[Date])
    Month Name = FORMAT('Date Dimension'[Date],"MMMM")
  2. Create relationship between these table based on the Date column and ensure that column formate should be same for both 

Ray_Minds_0-1736397917767.jpeg

 

3. Create current Month sales Measure 

Current Month Sales =

CALCULATE(

    SUM(financials[ Sales]),

    DATEADD('Date Dimension'[Date], 0, MONTH)

)

4. Create Last Month Sales Measure 

Last Month Sales =

CALCULATE(

    SUM(financials[ Sales]),

    DATEADD('Date Dimension'[Date], -1, MONTH)

)

5. Add into the visualization for last and current month and add the slicer 

6. Validate the current Month sales based on the selected slicer = feb

 

7. we showing the last month Data as well for the selected slicer 

 

Ray_Minds_2-1736397917768.jpeg

 

Note :- We are using the validated table to bypass the slicer filter for data validation prospective.



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

rajendraongole1
Super User
Super User

Hi @it_kwwk  - you need to adjust the context in the CALCULATE function so it ignores or manipulates the slicer filters appropriately.

Update your last month sales measure as below:

LastMonth_Sales =
VAR SelectedYear = COALESCE(SELECTEDVALUE('Append2_for line chart and table'[DocYear]), YEAR(TODAY()))
VAR SelectedMonth = COALESCE(
SWITCH(
SELECTEDVALUE('Append2_for line chart and table'[DocMonth]),
"January", 1, "February", 2, "March", 3, "April", 4,
"May", 5, "June", 6, "July", 7, "August", 8,
"September", 9, "October", 10, "November", 11, "December", 12,
BLANK()
),
MONTH(TODAY())
)
VAR LastMonth = MOD(SelectedMonth - 2, 12) + 1 // Efficient calculation for last month
VAR LastMonthYear = SelectedYear - IF(LastMonth > SelectedMonth, 1, 0) // Adjust year if crossing January
VAR StartDateLast = DATE(LastMonthYear, LastMonth, 1)
VAR EndDateLast = EOMONTH(StartDateLast, 0)

RETURN
CALCULATE(
[Filtered_Sales_Amount],
DATESBETWEEN(
'Append2_for line chart and table'[DocDate],
StartDateLast,
EndDateLast
),
REMOVEFILTERS('Append2_for line chart and table'[DocYear], 'Append2_for line chart and table'[DocMonth]) // Remove slicer context on year and month
)

 

Verify that the LastMonth_Sales column displays the correct values for the previous month, even when slicers are used.I hope this works.





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

Proud to be a Super User!





Thank you so much! It works, really appreciate it!

 

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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.