Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • Intuit stock jumps 9%+ after reporting Q3 revenue up 15% YoY to $7.8B with FY 2025 guidance of $18.72B to $18.76B, up from $18.16B to $18.35B (Ashley Capoot/CNBC)
    • Costco Offering Buy Now, Pay Later With Affirm. But Is It Worth It?
    • AI system resorts to blackmail if told it will be removed
    • How to Evaluate LLMs and Algorithms — The Right Way
    • Innovative contact lenses enable vision of infrared light and heat
    • London-based PlaySafe ID raises €1 million euros to make gaming safer by targeting hackers and even predators
    • The Best Travel Tech for Families (2025)
    • Zyrtec and Xyzal Flagged by FDA for New Side Effect. How to Know When to See Your Doctor
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Friday, May 23
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»About Calculating Date Ranges in DAX
    Artificial Intelligence

    About Calculating Date Ranges in DAX

    Editor Times FeaturedBy Editor Times FeaturedMay 23, 2025No Comments8 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    When growing Time Intelligence Measures with Energy BI or in Material in Semantic Fashions, it may be essential to create a date vary to calculate the consequence for a particular time-frame.

    To be exact, nearly all the Time Intelligence capabilities in Dax create an inventory of dates for a date vary.

    However typically we should create a customized date vary on account of particular necessities.

    DAX gives us two capabilities for this job:

    Each capabilities take a Begin Date as a parameter.
    However for the Finish Date, the conduct is totally different.

    Whereas DATESINPERIOD() takes Intervals (Days, Months, Quarters, Years), DATESBETWEEN() takes a specified Date used because the Finish Date.

    In distinction, DATEADD() makes use of the present Filter Context to get the Begin Date and to calculate the Finish Date.

    However we need to go a Begin Date, which might differ from the Date(s) within the present Filter Context.

    That is when one of many capabilities talked about above comes into play.

    On the finish of this text, I’ll present you a sensible instance utilizing the strategies proven right here.

    Instruments and state of affairs

    Like in lots of different articles, I take advantage of DAX Studio to write down DAX Queries and analyze the outcomes.

    In case you are not accustomed to writing DAX queries, learn my piece on the right way to study to write down such queries:

    This time, I take advantage of the Information mannequin just for the Date desk.

    I need to calculate a date vary ranging from Could 5. 2025 and both 25 days or 2 Months into the long run.

    To set the beginning date, I take advantage of this expression:

    DEFINE
        VAR StartDate = "2025-05-05"
    
    EVALUATE
           { StartDate }

    That is the lead to DAX Studio:

    Determine 1 – Question and lead to DAX Studio (Determine by the Writer)

    I outline a Variable and assign the results of the date expression for the following queries.

    One other option to outline the beginning date is to create a date worth utilizing DATE(2025, 05, 05).

    The consequence would be the identical.

    The distinction between these two approaches is that the primary returns a string, however the second returns a correct date.

    The DAX capabilities used right here can work with each.

    Utilizing DATESINPERIOD()

    Let’s begin with DATEINPERIOD().

    I’ll use this operate to get a date vary string from the Begin Date and 25 days into the long run:

    DEFINE
        VAR StartDate = "2025-05-05"
        
    EVALUATE
        DATESINPERIOD('Date'[Date]
                        ,StartDate
                        ,25
                        ,DAY)

    The result’s a desk with 25 rows for the times ranging from Could 05, 2025, to Could 29, 2025:

    Determine 2 – 25 Days calculated with DATESINPERIOD() (Determine by the Writer)

    Now, let’s barely change the question to get an inventory of all dates from the Begin Date to 2 Months into the long run:

    DEFINE
        VAR StartDate = "2025-05-05"
        
    EVALUATE
        DATESINPERIOD('Date'[Date]
                        ,StartDate
                        ,2
                        ,MONTH)

    The question returns 61 rows ranging from Max 05, 2025, till July 04, 2025:

    Determine 3 – 2 Months of dates generated with DATESINPERIOD() (Determine by the Writer)

    I can go the interval with an arbitrary variety of days (e.g., 14, 28, 30, or 31 days), and the operate routinely calculates the date vary.

    After I go damaging numbers, the date vary goes to the previous, beginning with the beginning date.

    Utilizing DATESBETWEEN()

    Now, let’s take a look at DATESBETWEEN().

    DATESBETWEEN() takes a Begin- and an Finish-Date as parameters.

    This implies I need to calculate the top date earlier than utilizing it.

    After I need to get a date vary from Could 05 to Could 29, 2025, I need to use the next question:

    DEFINE
        VAR StartDate = "2025-05-05"
        
        VAR EndDate = "2025-05-25"
        
    EVALUATE        
        DATESBETWEEN('Date'[Date]
                        ,StartDate
                        ,EndDate)

    The consequence is similar as with DATESINPERIOD().

    Nevertheless, there’s one essential level: The tip date is included within the consequence.

    This implies I can write one thing like this to get a date vary over two months from Could 05 to July 05, 2025:

    DEFINE
        VAR StartDate = "2025-05-05"
        
        VAR EndDate = "2025-07-05"
        
    EVALUATE        
        DATESBETWEEN('Date'[Date]
                        ,StartDate
                        ,EndDate)

    The result’s similar to the one utilizing DATESINPERIOD() and month because the interval, however with one row extra:

    Determine 4 – Outcome for a date vary over two months, plus one row (Determine by the Writer)

    This provides me extra flexibility to create the date ranges, as I can pre-calculate the top date in line with my wants.

    Use in Measures – a sensible instance.

    I can use these strategies to calculate a working complete in a Measure.

    However we should take care to make use of the 2 capabilities in the precise approach

    For instance, to calculate the working complete monthly for 25 days.

    Take a look at the next code, the place I outline two Measures utilizing the 2 capabilities:

    DEFINE
        MEASURE 'All Measures'[25DayRollingTotal_A] =
            VAR DateRange =
                DATESINPERIOD('Date'[Date]
                                ,MIN ( 'Date'[Date] )
                                ,25
                                ,DAY)
            
            RETURN
                CALCULATE ( [Sum Online Sales]
                            , DateRange )
    
        MEASURE 'All Measures'[25DayRollingTotal_B] =
            VAR DateRange =
                DATESBETWEEN ( 'Date'[Date]
                                ,MIN ( 'Date'[Date] )
                                ,MIN ( 'Date'[Date] ) + 25 )
            
            RETURN
                CALCULATE ( [Sum Online Sales]
                            , DateRange )
    
    EVALUATE
    CALCULATETABLE (
        SUMMARIZECOLUMNS (
            'Date'[Year]
            ,'Date'[Month]
            ,"Gross sales", [Sum Online Sales]
            ,"25DayRollingTotal_A", [25DayRollingTotal_A]
            ,"25DayRollingTotal_B", [25DayRollingTotal_B]
            )
            ,'Date'[Date] >= DATE(2023, 01, 01) && 'Date'[Date] <= DATE(2023, 12, 31)
    )
    ORDER BY 'Date'[Month]

    That is the consequence:

    Determine 5 – Results of the working complete over 25 days with the 2 capabilities (Determine by the Writer)

    Discover the distinction between the 2 outcomes.

    It’s because DATESBETWEEN() consists of the top date within the consequence, whereas DATESINPERIOD() provides the variety of intervals to the beginning date however consists of the beginning date.

    Strive it out with the next question:

    DEFINE
        VAR StartDate = DATE(2025,05,05)
        
        VAR EndDate = StartDate + 25
        
    EVALUATE
        DATESINPERIOD('Date'[Date]
                        ,StartDate
                        ,25
                        ,DAY)
        
    EVALUATE        
        DATESBETWEEN('Date'[Date]
                        ,StartDate
                        ,EndDate)

    The primary returns 25 rows (Could 05 – Could 29, 2025) and the second returns 26 rows (Could 05 – Could 30, 2025).

    Due to this fact, I need to change one of many two Measures to get the identical consequence.

    On this case, the calculation definition is: Begin from the primary date and go 25 into the long run.

    The corrected logic is that this:

    DEFINE
        MEASURE 'All Measures'[25DayRollingTotal_A] =
            VAR DateRange =
                DATESINPERIOD('Date'[Date]
                                ,MIN ( 'Date'[Date] )
                                ,25
                                ,DAY)
            
            RETURN
                CALCULATE ( [Sum Online Sales]
                            , DateRange )
    
        MEASURE 'All Measures'[25DayRollingTotal_B] =
            VAR DateRange =
                DATESBETWEEN ( 'Date'[Date]
                                ,MIN ( 'Date'[Date] )
                                ,MIN ( 'Date'[Date] ) + 24 )  // 24 as a substitute of 25 days
            
            RETURN
                CALCULATE ( [Sum Online Sales]
                            , DateRange )
    
    EVALUATE
    CALCULATETABLE (
        SUMMARIZECOLUMNS (
            'Date'[Year]
            ,'Date'[Month]
            ,"Gross sales", [Sum Online Sales]
            ,"25DayRollingTotal_A", [25DayRollingTotal_A]
            ,"25DayRollingTotal_B", [25DayRollingTotal_B]
            )
            ,'Date'[Date] >= DATE(2023, 01, 01) && 'Date'[Date] <= DATE(2023, 12, 31)
    )
    ORDER BY 'Date'[Month]

    Now, each measures return the identical consequence:

    Determine 6 – Results of the corrected Measures (Determine by the Writer)

    I examined the efficiency of each capabilities for a similar calculation (Rolling complete over 25 days), and the outcomes have been equal. There was no distinction in efficiency or effectivity between these two.

    Even the execution plan is similar.

    Which means DATEINPERIOD() is a shortcut operate for DATESBETWEEN().

    Conclusion

    From a performance perspective, each of the proven capabilities are nearly equal.

    The identical applies from the efficiency perspective.

    They differ in the way in which the top date is outlined.

    DATESINPERIOD() relies on calendar intervals, like days, months, quarters, and years.
    This operate is used when the date vary have to be calculated based mostly on the calendar.

    However when we’ve a pre-defined finish date or should calculate the date vary between two pre-defined dates, the DATESBETWEEN() operate is the operate to make use of.

    For instance, I take advantage of DATESBETWEEN() when performing Time Intelligence calculations for weeks.

    You possibly can learn this piece to study extra about weekly calculations:

    As you possibly can learn, I retailer the beginning and finish dates of the week for every row within the information desk.

    This manner, I can simply lookup every date’s begin and finish dates.

    So, once we should choose between these two capabilities, it’s not a matter of performance however of necessities outlined by the stakeholders of the brand new reviews or the wanted information evaluation.

    Learn this text to discover ways to acquire and interpret Efficiency information with DAX Studio:

    Like in my earlier articles, I take advantage of the Contoso pattern dataset. You possibly can obtain the ContosoRetailDW Dataset totally free from Microsoft here.

    The Contoso Information will be freely used beneath the MIT License, as described in this document. I modified the dataset to shift the information to modern dates.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    How to Evaluate LLMs and Algorithms — The Right Way

    May 23, 2025

    Inheritance: A Software Engineering Concept Data Scientists Must Know To Succeed

    May 23, 2025

    Multiple Linear Regression Analysis | Towards Data Science

    May 23, 2025

    Google’s AlphaEvolve: Getting Started with Evolutionary Coding Agents

    May 22, 2025

    What Statistics Can Tell Us About NBA Coaches

    May 22, 2025

    The Role of Natural Language Processing in Financial News Analysis

    May 22, 2025
    Leave A Reply Cancel Reply

    Editors Picks

    Intuit stock jumps 9%+ after reporting Q3 revenue up 15% YoY to $7.8B with FY 2025 guidance of $18.72B to $18.76B, up from $18.16B to $18.35B (Ashley Capoot/CNBC)

    May 23, 2025

    Costco Offering Buy Now, Pay Later With Affirm. But Is It Worth It?

    May 23, 2025

    AI system resorts to blackmail if told it will be removed

    May 23, 2025

    How to Evaluate LLMs and Algorithms — The Right Way

    May 23, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    These Are the Best Smart Devices for Amazon Alexa in 2025

    February 5, 2025

    Best Smart Home Devices of 2025

    March 21, 2025

    The Role of Financial Reporting in Strategic Decision-Making

    March 2, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.