skip to main content
Power BI
    • What is Power BI
    • Why Power BI
    • Customer stories
    • Data visuals
    • Security
    • Power BI Desktop
    • Power BI Pro
    • Power BI Premium
    • Power BI Mobile
    • Power BI Embedded
    • Power BI Report Server
  • Pricing
    • Azure + Power BI
    • Microsoft 365 + Power BI
    • Dynamics 365 + Power BI
      • Energy
      • Healthcare
      • Manufacturing
      • Media
      • Retail
    • For analysts
    • For IT
      • Overview
      • Embedded analytics
      • Power BI visuals
      • Automation
      • Documentation
      • Community
    • Overview
    • Find consulting services
    • Partner showcase
    • Find a partner
    • Become a partner
    • Instructor-led training
    • Getting started
      • Overview
      • Self-guided learning
      • Webinars
      • Documentation
      • Roadmap
      • Overview
      • Issues
      • Give feedback
    • Blog
    • Business intelligence topics
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • Events
    • User groups
    • Community blog
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Microsoft Power BI Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power BI
    • Desktop
    • Service
    • Report Server
    • Power Query
    • Mobile Apps
    • Developer
    • DAX Commands and Tips
    • Custom Visuals Development Discussion
    • Health and Life Sciences
    • Power BI Spanish Community
    • Translated Spanish Desktop
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Training and Consulting
    • Instructor Led Training
    • Galleries
    • Community Connections & How-To Videos
    • COVID-19 Data Stories Gallery
    • Themes Gallery
    • Data Stories Gallery
    • R Script Showcase
    • Webinars and Video Gallery
    • Quick Measures Gallery
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Events
    • Ideas
    • Custom Visuals Ideas
    • Issues
    • Issues
    • Events
    • Upcoming Events
    • Community Engagement
    • T-Shirt Design Challenge 2023
    • Community Blog
    • Power BI Community Blog
    • Custom Visuals Community Blog
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power BI Community
    • Galleries
    • Quick Measures Gallery
    • Power BI - Window Function

    Power BI - Window Function

    04-09-2023 02:46 AM - last edited 04-09-2023 02:51 AM

    Super User amitchandak
    Super User
    1999 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    amitchandak
    Super User amitchandak
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    Power BI - Window Function

    ‎04-09-2023 02:46 AM

    Power BI — Window Function

    amitchandak_0-1681032741780.jpeg

     

    The Windows function is one of three new DAX functions that were released in December 2022. Using WINDOW, a slice of results is retrieved using either absolute or relative positioning.

    The WINDOW function can be defined using a combination of functions such as FILTER, ALL, and VALUES based on the requirement.

    The Window function reads all of the data from the table provided by the <table> parameter and divides the records into distinct divisions using partition-by-columns’ unique values. Using the order-by-columns and sorting instructions, arrange the rows within each partition. Returns all the rows between a lower bound and an upper bound, depending on the current partition and the current row.

    Instead of being pushed to the data source, DAX functions like Window’s are executed within the DAX engine. These DAX functions have shown significantly improved performance compared to existing DAX expressions, particularly when sorting non-continuous columns is necessary.

     

    amitchandak_0-1681033536752.png

     

    The WINDOW function in DAX has opened up a whole new world of possibilities for Power BI users, giving them the power to perform complex calculations and analyses on sets of data in a way that is efficient, uniform, and elegant. In addition to running totals, rolling totals, month-to-date, quarter-to-date, year-to-date, and averages, window functions can be used for many other functions as well.

    Format

    WINDOW ( <from>,<from type>, <to>, <to type>, <relation>, <order By>, <blanks>, <partition By> )

    Model: I am using the standard sales model, which I am using for all my videos and blogs. Sales fact with a key measure [net], joined with dimensions: Item, Geography, Date, and Customer.

    amitchandak_0-1681033656778.png

     

    Date Table Script

    Date = ADDCOLUMNS(CALENDAR(date(2018,01,01), date(2020,10,31))
    ,"Month Year", FORMAT([Date], "MMM-YYYY")
    ,"Month Year sort", FORMAT([Date], "YYYYMM")
    , "Year", YEAR([Date])
    ,"Qtr Year" ,FORMAT([Date],"YYYY\QQ"),
    "WeekDay", FORMAT([Date], "ddd")
    ,"Month", FORMAT([Date], "MMM")
    ,"Month sort", FORMAT([Date], "MM")
    )

    Let us create a visual using the Date and net. 

    We will also create our first Windows measure for Rolling 2 days and add to it.

    amitchandak_1-1681033707463.png

     

    Rolling 2 = CALCULATE([Net], WINDOW(-1,REL,0,REL, 
    ALLSELECTED('Date'[Date]),
    ORDERBY('Date'[Date]))
    )

    Observe that it is the sum of the current and previous days.

    amitchandak_2-1681033707596.png

     

    In the same way,

    you can create a rolling 30 measure

    Rolling 30 = CALCULATE([Net], WINDOW(-1,REL,0,REL, 
    ALLSELECTED('Date'[Date]),
    ORDERBY('Date'[Date]))
    )
    amitchandak_3-1681033730007.png

     

    Month-year text cannot be sorted, so we need to use a sort column in the formula to create a rolling months measure

    Rolling 2 Month = CALCULATE([Net], WINDOW(-1,REL,0,REL, 
    ALLSELECTED('Date'[Month Year sort], 'Date'[Month Year]),
    ORDERBY('Date'[Month Year sort]))
    )
    amitchandak_4-1681033730009.png

     

    Rolling 12 Month

    Rolling 12 Month = CALCULATE([Net], WINDOW(-11,REL,0,REL, 
    ALLSELECTED('Date'[Month Year sort], 'Date'[Month Year]),
    ORDERBY('Date'[Month Year sort]))
    )
    amitchandak_5-1681033756063.png

     

    For running total/cumulative we have to start from the 0/1 abs(absolute/fixed) position. In this, I am building the formula using date. By using date I Can solve cumulative on all the above levels like week, month, quarter, and year.

    Cumulative Total = CALCULATE([Net], WINDOW(1,ABS,0,REL, 
    ALLSELECTED('Date'),
    ORDERBY('Date'[Date]))
    )
    amitchandak_6-1681033756063.png

     

    Date Wise running total 

    amitchandak_0-1681033810473.png

     

    Now, if we control this cumulative formula using partition by, we can get MTD, QTD, and YTD 

    MTD = CALCULATE([Net], WINDOW(1,ABS,0,REL, 
    ALLSELECTED('Date'),
    ORDERBY('Date'[Date]),,PARTITIONBY('Date'[Month Year])
    ))
    amitchandak_1-1681033810475.png

     

    Same way, you can have QTD, YTD

     

    My Medium blogs can be found here if you are interested

    Click here to access all my blogs and videos in a jiffy via an exclusive glossary using Power BI. Please like, share, and comment on these blogs. I would appreciate your suggestions for improvement, challenges, and suggestions for topics so that I can explore these in more depth.

    In addition, I have over 500 videos on my YouTube channel that are related to Power BI, Tableau, and Incorta. With the help of these videos, you will learn hundreds of DAX and Power Query functions, in addition to hundreds of Power BI, use cases that will assist you in becoming an expert in Power BI. Make sure you subscribe, like, and share it with your friends.

    Master Power BI
    Expertise in Power BI
    Power BI For Tableau User
    DAX for SQL Users
    Learn SQL



    !! Integrated Course on Microsoft Fabric, Power BI, and SQL for Free!! !! Master Microsoft Fabric- 31 Videos !!
    Microsoft Power BI Learning Resources, 2023 !!
    Learn Power BI - Full Course with Dec-2022, with Window, Index, Offset, 100+ Topics !!
    Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
    AT Small Window post.pbix
    Labels:
    • Labels:
    • Financial
    • Mathematical
    • Time Intelligence
    Message 1 of 1
    1,999 Views
    1
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign in
    • Sign up

    Browse

    • Solutions
    • Partners
    • Consulting Services

    Downloads

    • Power BI Desktop
    • Power BI Mobile
    • Power BI Report Server
    • See all downloads

    Learn

    • Guided learning
    • Documentation
    • Support
    • Community
    • Give feedback
    • Webinars
    • Developers
    • Blog
    • Newsletter

    © 2023 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks
    Consumer Privacy Act (CCPA) Opt-Out Icon Your Privacy Choices