简体   繁体   中英

Power Bi DAX: Relative Date Filtering

I have a quick question, is there a way to filter data from set days between 2 months?

Eg filter from 16-15 of Jan-Feb.

Is this possible?

For example i have used a measure to calculate days between dates

Last Assessment 16-15 
    = CALCULATE(SUM('Table1'[Duration1]),'Table1'[Start], 
             DATESBETWEEN('Calendar'[Date], [Assessment Date], [Assessment one month]))

Assessment Date = if(DAY(TODAY())<16,DATE(YEAR(TODAY()),MONTH(TODAY())-1,15),
                      DATE(YEAR(TODAY()),MONTH(TODAY()),15))

Assessment one month = EDATE([Assessment Date],-1)+1
Assessment 6 = EDATE([Assessment Date],-6)+1
Assessment 12 = EDATE([Assessment Date],-12)+1

The last assessment does show from the 16th of 2 months ago to last months 15th eg Dec 16th - Jan 15th. But i need to show from last 6 months and the last 1 year.

How can i work this out so i can show the Last 6 months and 1 year.

So far i have had to use a date filter to manually select the dates which i want to stop and have it be automatic.

If it is just the last 6 months or the last year you could make a custom column in the query editor (this would be the easiest way then). Like a filter flag:

'Includes the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6) or Date.IsInCurrentMonth([YourDate])

'Without the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6)

The same for last year:

Last Year Flag = Date.IsInPreviousYear([YourDate])

Drag and drop these custom columns as filter on your report and you are done.

consider following measure for Latest assessment (basically what you had before with EDATE for safe Dec/Jan handling and with variables for better performance)

Latest Assessment Date = 
VAR __today = TODAY()
RETURN
IF (
    DAY ( __today ) < 16,
    DATE ( YEAR ( __today ), MONTH(EDATE(__today, - 1)), 15 ),
    DATE ( YEAR ( __today ), MONTH ( __today ), 15 )
)

then you have this measure for handling 1/6/12 scenarios, you just replace 12 with whatever number of months you need

Measure 12 =
VAR __nrOfMonthsBack = 12
VAR __lastAssesment = [Latest Assessment Date]
RETURN
    CALCULATE (
        SUM ( Table1[Duration] ),
        ALL('Calendar'),  --this will reset any date filters coming from slicers
        DATESBETWEEN (
            'Calendar'[Date],
            EDATE ( __lastAssesment, - __nrOfMonthsBack ) + 1,
            __lastAssesment
        )
    )

EDIT: added ALL('Calendar') to disable filters coming from slicers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM