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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
gluizqueiroz
Resolver I
Resolver I

How to verify if exists a contigous selection? Function 'DATEADD' expects a contiguous selection....

I have the following measure: 

Previous Sales = 
    IF(HASONEVALUE(DateDimension[Year]);
        CALCULATE(SUM(BI_Sales[Value]); DATEADD(DateDimension[DateKey]; -1; YEAR));
        BLANK()
    )

And in my page, I have slicers for month and year, If user selects 2019 and 2018 the measure will work fine, my problem is when the user selects 2019 and 2017 or select just 2019 but 2 months, like January and May, note the months are not sequential.

Is there any way to verify if exists a contiguous selection to treat this case or correct this problem?

I have tried to use SAMEPERIODLASTYEAR.

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @gluizqueiroz,

I think you need to create a separate date table and then link your current table to the new date table.
Date table: 

Date = CALENDAR(MIN('DateDimension'[DateKey]),MAX('DateDimension'[DateKey]))

And then you need to redefine this measure as shown below.
Measure: 

Previous Sales =
IF (
    HASONEVALUE ( DateDimension[Year] ),
    CALCULATE (
        SUM ( DateDimension[Value] ),
        FILTER (
            ALL ( 'Date' ),
            YEAR ( 'Date'[Date] )
                = YEAR ( MAX ( 'Date'[Date] ) ) - 1
                && MONTH ( 'Date'[Date] ) = MONTH ( MAX ( 'Date'[Date] ) )
        )
    ),
    BLANK ()
)

Select [Date].Month from the data table as a slicer, the measure still works even if you select two months that are not sequential.

1.png

Best Regards,
Jack Chen

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

Hi @gluizqueiroz,

I think you need to create a separate date table and then link your current table to the new date table.
Date table: 

Date = CALENDAR(MIN('DateDimension'[DateKey]),MAX('DateDimension'[DateKey]))

And then you need to redefine this measure as shown below.
Measure: 

Previous Sales =
IF (
    HASONEVALUE ( DateDimension[Year] ),
    CALCULATE (
        SUM ( DateDimension[Value] ),
        FILTER (
            ALL ( 'Date' ),
            YEAR ( 'Date'[Date] )
                = YEAR ( MAX ( 'Date'[Date] ) ) - 1
                && MONTH ( 'Date'[Date] ) = MONTH ( MAX ( 'Date'[Date] ) )
        )
    ),
    BLANK ()
)

Select [Date].Month from the data table as a slicer, the measure still works even if you select two months that are not sequential.

1.png

Best Regards,
Jack Chen

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hey @v-shex-msft.

Your answer looks good, but I'm not have tried yet, when I try it, I came here to Accept or not your answer as correctly.
I not forget this post.

Thanks.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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