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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
suprdata
Regular Visitor

HOW to show last 6 months sales in bar chart when user select Month

HOW to show last 6 months sales in bar chart when user select Month

2 ACCEPTED SOLUTIONS
DataNinja777
Super User
Super User

Hi @suprdata ,

 

To show the last six months of sales in a bar chart when a user selects a month, you need to create a DAX measure that dynamically filters sales data based on the selected month. The following DAX measure calculates sales for the last six months relative to the selected date:

Last6MonthsSales = 
VAR SelectedMonth = MAX('DateTable'[Date])
RETURN
CALCULATE(
    SUM(SalesTable[SalesAmount]),
    DATESINPERIOD(
        'DateTable'[Date], 
        SelectedMonth, 
        -6, 
        MONTH
    )
)

Once the measure is created, add a bar chart in Power BI and use the 'DateTable'[MonthYear] column as the X-axis and the Last6MonthsSales measure as the Y-axis. Ensure that the DateTable is properly related to SalesTable via the date column. When a user selects a specific month, the measure will dynamically adjust to show data for the last six months, including the selected month.
An alternative method is to apply a relative date filter directly in the bar chart's filters pane. Select the chart, add a filter on 'DateTable'[Date], choose relative date filtering, and set it to show the last six months. This ensures that the visualization always reflects the last six months dynamically without requiring a DAX measure.

 

Best regards,

View solution in original post

danextian
Super User
Super User

Hi @suprdata 

You will need to use a disconnected dates/period table as using one that has a relationship to your fact table will show only the rows that have been selected, and then create a measure that references a column from that table.

 

Here's a sample measure

Sales Disconnected - L6M = 
CALCULATE (
    [Sales],
    KEEPFILTERS (
        DATESINPERIOD ( Dates[Date], MAX ( DisconnectedDates[Date] ), -6, MONTH )
    )
)

danextian_0-1739515276739.png

 

 Please see the attached pbix for the details.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


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

View solution in original post

3 REPLIES 3
Bibiano_Geraldo
Super User
Super User

Hi @suprdata ,

Considering that you have sales table, to achieve your goal, first you need to create a disconnected calendar table, something like this:

Calendar = 
CALENDAR(
    MIN(financials[Date]),
    MAX(financials[Date])
)

 after created the table, now you can create these two calculated columns in the calendar tble:

Month Name = FORMAT('Calendar'[Date], "mmm-yy")
Month No = VALUE(FORMAT('Calendar'[Date], "yyyymm"))

At this point, your table should look like this:

Bibiano_Geraldo_3-1739520286017.png

Now select the month name column and sort by month no. column:

Bibiano_Geraldo_4-1739520362434.png

Make sure that these tables are NOT related:

Bibiano_Geraldo_2-1739519434255.png

Now create a measure to calculate last 6 months:

Last6MonthSales = 
VAR vLastDate = MAX('Calendar'[Date])
RETURN
CALCULATE(
    SUM(Sales[ Sales]),
    DATESINPERIOD(Sales[Date],vLastDate,-6,MONTH)
)

 

In your report add a new slicer with month name column from disconnected calendar table, and add the column chart, in your x-axis add the date or month column from your sales table and in y-axis add the last6monthsales measure.

 

Your output should look like this:

Bibiano_Geraldo_5-1739520605873.png

 

 

danextian
Super User
Super User

Hi @suprdata 

You will need to use a disconnected dates/period table as using one that has a relationship to your fact table will show only the rows that have been selected, and then create a measure that references a column from that table.

 

Here's a sample measure

Sales Disconnected - L6M = 
CALCULATE (
    [Sales],
    KEEPFILTERS (
        DATESINPERIOD ( Dates[Date], MAX ( DisconnectedDates[Date] ), -6, MONTH )
    )
)

danextian_0-1739515276739.png

 

 Please see the attached pbix for the details.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"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.
DataNinja777
Super User
Super User

Hi @suprdata ,

 

To show the last six months of sales in a bar chart when a user selects a month, you need to create a DAX measure that dynamically filters sales data based on the selected month. The following DAX measure calculates sales for the last six months relative to the selected date:

Last6MonthsSales = 
VAR SelectedMonth = MAX('DateTable'[Date])
RETURN
CALCULATE(
    SUM(SalesTable[SalesAmount]),
    DATESINPERIOD(
        'DateTable'[Date], 
        SelectedMonth, 
        -6, 
        MONTH
    )
)

Once the measure is created, add a bar chart in Power BI and use the 'DateTable'[MonthYear] column as the X-axis and the Last6MonthsSales measure as the Y-axis. Ensure that the DateTable is properly related to SalesTable via the date column. When a user selects a specific month, the measure will dynamically adjust to show data for the last six months, including the selected month.
An alternative method is to apply a relative date filter directly in the bar chart's filters pane. Select the chart, add a filter on 'DateTable'[Date], choose relative date filtering, and set it to show the last six months. This ensures that the visualization always reflects the last six months dynamically without requiring a DAX measure.

 

Best regards,

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

Check out the March 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

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

Top Solution Authors