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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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