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
Anonymous
Not applicable

Previous 6 months Data

Hi all,
Need your assistance to find a formula to write the DAX for previous 6 months.
Between Aug 2020 to Dec 2020, there is no value.

Below screenshot of desired ouput
Capture.JPG
Regards
Hidayat

1 ACCEPTED SOLUTION
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

Create a Yearmonth column from the date column.

year_month = YEAR('table'[date])*100+MONTH('table'[date])

Create index columns based on month and year.

month_index = 
VAR MonthRow = 'table'[year_month]
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'table'[year_month] ),
        FILTER ('table','table'[year_month] <= MonthRow )
    )

Create an unrelated calendar table as slicer.

Table 2 = SELECTCOLUMNS('table',"DateKey",'table'[date],"YearMonth",'table'[year_month])

Create a measure and apply it to a visual level filter.

Last6Months = 
//Get the year_month from the slicer
var max_date = SELECTEDVALUE('Table 2'[YearMonth])
//Get the index of year_month
var _mindex = CALCULATE(MAX('table'[month_index]),FILTER(ALL('table'),'table'[year_month]=max_date))
//Get the min year_month of the last six months
var min_date = CALCULATE(MIN('table'[year_month]),FILTER(ALL('table'),'table'[month_index]=_mindex-5))
return IF(MAX('table'[year_month])>=min_date&&MAX('table'[year_month])<=max_date,1,0)

Sample .pbix

 

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

View solution in original post

3 REPLIES 3
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

Create a Yearmonth column from the date column.

year_month = YEAR('table'[date])*100+MONTH('table'[date])

Create index columns based on month and year.

month_index = 
VAR MonthRow = 'table'[year_month]
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'table'[year_month] ),
        FILTER ('table','table'[year_month] <= MonthRow )
    )

Create an unrelated calendar table as slicer.

Table 2 = SELECTCOLUMNS('table',"DateKey",'table'[date],"YearMonth",'table'[year_month])

Create a measure and apply it to a visual level filter.

Last6Months = 
//Get the year_month from the slicer
var max_date = SELECTEDVALUE('Table 2'[YearMonth])
//Get the index of year_month
var _mindex = CALCULATE(MAX('table'[month_index]),FILTER(ALL('table'),'table'[year_month]=max_date))
//Get the min year_month of the last six months
var min_date = CALCULATE(MIN('table'[year_month]),FILTER(ALL('table'),'table'[month_index]=_mindex-5))
return IF(MAX('table'[year_month])>=min_date&&MAX('table'[year_month])<=max_date,1,0)

Sample .pbix

 

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

amitchandak
Super User
Super User

Try like with date table

Rolling 6 = CALCULATE(Average(Table[Trade Price]),DATESINPERIOD('Date'[Date],ENDOFMONTH(Sales[Sales Date]),-6,MONTH))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

Hi @amitchandak ,

Not looking for average in the statement.
I would need the statement to lookup the last 6 month record.

Regards
Hidayat

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!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.