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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
JayashreeG1502
Frequent Visitor

DAX query for comparison based on date range

Hi,

I have a requirement,

If the date period is selected for example May 2 to May 5 2022 then the interval for these is 3 days, the data displayed in the table,

  • Weekwise should be the last 3 weeks where the interval (3 days) is subtracted from the range selected i.e., April 28 to 25, April 21 to 18, April 14 to 11.
  • Monthwise should be the last 3 months same range selected i.e., April 2 to 5, March 2 to 5, Feb 2 to 5.
  • Yearwise should be the last 2 years same range selected i.e., May 2 to 5 2021, May 2 to 5 2020.comparrsion.png

     

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @JayashreeG1502 ,

 

Try this:

Icey_0-1652340593248.png

 

Steps:

1. Create a LabelTable via "Enter Data" and sort columns.

Icey_1-1652340643063.png

 

2. Create a Date table and DO NOT create relationships between it and your fact table.

 

Date = CALENDAR ( DATE ( 2019, 1, 1 ), DATE ( 2022, 12, 31 ) )

 

Icey_2-1652340735294.png

 

3. Create measures.

 

Date Measure = 
SWITCH (
    SELECTEDVALUE ( LabelTable[Label] ),
    "Selected Date",
        MIN ( 'Date'[Date] ) & " - "
            & MAX ( 'Date'[Date] ),
    "Previous Week",
        MIN ( 'Date'[Date] ) - 7 & " - "
            & MAX ( 'Date'[Date] ) - 7,
    "Week 2",
        MIN ( 'Date'[Date] ) - 7 * 2 & " - "
            & MAX ( 'Date'[Date] ) - 7 * 2,
    "Week 3",
        MIN ( 'Date'[Date] ) - 7 * 3 & " - "
            & MAX ( 'Date'[Date] ) - 7 * 3,
    "Previous Mon",
        MINX ( DATEADD ( 'Date'[Date], -1, MONTH ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -1, MONTH ), [Date] ),
    "Mon 2",
        MINX ( DATEADD ( 'Date'[Date], -2, MONTH ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -2, MONTH ), [Date] ),
    "Mon 3",
        MINX ( DATEADD ( 'Date'[Date], -3, MONTH ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -3, MONTH ), [Date] ),
    "Previous Year",
        MINX ( DATEADD ( 'Date'[Date], -1, YEAR ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -1, YEAR ), [Date] ),
    "Year 2",
        MINX ( DATEADD ( 'Date'[Date], -2, YEAR ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -2, YEAR ), [Date] )
)
Value Measure = 
SWITCH (
    SELECTEDVALUE ( LabelTable[Label] ),
    "Selected Date",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FactTable[Date] IN VALUES ( 'Date'[Date] )
        ),
    "Previous Week",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -7 * 1, DAY )
            )
        ),
    "Week 2",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -7 * 2, DAY )
            )
        ),
    "Week 3",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -7 * 3, DAY )
            )
        ),
    "Previous Mon",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -1, MONTH )
            )
        ),
    "Mon 2",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -2, MONTH )
            )
        ),
    "Mon 3",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -3, MONTH )
            )
        ),
    "Previous Year",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -1, YEAR )
            )
        ),
    "Year 2",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -2, YEAR )
            )
        )
)

 

 

4. Create a matrix visual and switch values to rows.

Icey_0-1652340876600.png

For more details, check the attachment.

 

 

Best Regards,

Icey

 

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

2 REPLIES 2
JayashreeG1502
Frequent Visitor

Hi @Icey,

Thanks for your help. It's working.

Icey
Community Support
Community Support

Hi @JayashreeG1502 ,

 

Try this:

Icey_0-1652340593248.png

 

Steps:

1. Create a LabelTable via "Enter Data" and sort columns.

Icey_1-1652340643063.png

 

2. Create a Date table and DO NOT create relationships between it and your fact table.

 

Date = CALENDAR ( DATE ( 2019, 1, 1 ), DATE ( 2022, 12, 31 ) )

 

Icey_2-1652340735294.png

 

3. Create measures.

 

Date Measure = 
SWITCH (
    SELECTEDVALUE ( LabelTable[Label] ),
    "Selected Date",
        MIN ( 'Date'[Date] ) & " - "
            & MAX ( 'Date'[Date] ),
    "Previous Week",
        MIN ( 'Date'[Date] ) - 7 & " - "
            & MAX ( 'Date'[Date] ) - 7,
    "Week 2",
        MIN ( 'Date'[Date] ) - 7 * 2 & " - "
            & MAX ( 'Date'[Date] ) - 7 * 2,
    "Week 3",
        MIN ( 'Date'[Date] ) - 7 * 3 & " - "
            & MAX ( 'Date'[Date] ) - 7 * 3,
    "Previous Mon",
        MINX ( DATEADD ( 'Date'[Date], -1, MONTH ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -1, MONTH ), [Date] ),
    "Mon 2",
        MINX ( DATEADD ( 'Date'[Date], -2, MONTH ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -2, MONTH ), [Date] ),
    "Mon 3",
        MINX ( DATEADD ( 'Date'[Date], -3, MONTH ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -3, MONTH ), [Date] ),
    "Previous Year",
        MINX ( DATEADD ( 'Date'[Date], -1, YEAR ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -1, YEAR ), [Date] ),
    "Year 2",
        MINX ( DATEADD ( 'Date'[Date], -2, YEAR ), [Date] ) & " - "
            & MAXX ( DATEADD ( 'Date'[Date], -2, YEAR ), [Date] )
)
Value Measure = 
SWITCH (
    SELECTEDVALUE ( LabelTable[Label] ),
    "Selected Date",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FactTable[Date] IN VALUES ( 'Date'[Date] )
        ),
    "Previous Week",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -7 * 1, DAY )
            )
        ),
    "Week 2",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -7 * 2, DAY )
            )
        ),
    "Week 3",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -7 * 3, DAY )
            )
        ),
    "Previous Mon",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -1, MONTH )
            )
        ),
    "Mon 2",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -2, MONTH )
            )
        ),
    "Mon 3",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -3, MONTH )
            )
        ),
    "Previous Year",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -1, YEAR )
            )
        ),
    "Year 2",
        CALCULATE (
            SUM ( FactTable[RandValue] ),
            FILTER (
                FactTable,
                FactTable[Date]
                    IN DATEADD ( 'Date'[Date], -2, YEAR )
            )
        )
)

 

 

4. Create a matrix visual and switch values to rows.

Icey_0-1652340876600.png

For more details, check the attachment.

 

 

Best Regards,

Icey

 

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

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.