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
    • Office 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
      • Online workshops
      • 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
    • 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
    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
    • For Loop

    For Loop

    03-05-2019 11:51 AM

    Super User Greg_Deckler
    Super User
    8223 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    For Loop

    ‎03-05-2019 11:51 AM

    This is a demonstration on how to emulate a traditional For loop in DAX. For further explanation, see this article:

     

    https://community.powerbi.com/t5/Community-Blog/For-and-While-Loops-in-DAX/bc-p/637511

     

     

    For Loop = 
    // Provide some starting values
    VAR __n = 5
    VAR __sum = 10
    // Generate a "loop table", this will emulate a for loop for i=1 to some number
    VAR __loopTable = GENERATESERIES(1,__n)
    // Add in our calculated sum, emulating calculations done as iterations over the loop
    VAR __loopTable1 = ADDCOLUMNS(__loopTable,"__sum",SUMX(FILTER(__loopTable,[Value]<=EARLIER([Value])),[Value]))
    // Determine our MAX interation, the maximum value for "i"
    VAR __max = MAXX(__loopTable1,[Value])
    RETURN
    // Return the value associated with the maximum value of "i" which is the last iteration in our "loop"
    MAXX(FILTER(__loopTable1,[Value]=__max),[__sum])

     

     

    eyJrIjoiMmZkY2UzOWQtZmQxYi00ZTRiLTg1M2QtZjllZmVjNWQ0ZmNjIiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9


    @ me in replies or I'll lose your thread!!!
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Learn Power BI 2nd Edition
    Preview file
    28 KB
    ForLoop.pbix
    Labels:
    • Labels:
    • Mathematical
    • Other
    • Totals
    Message 1 of 1
    8,223 Views
    0
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • 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

    © 2022 Microsoft

    Follow Power BI

    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks