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
    • Partners Overview
    • Solutions Partners
    • BI Specialized Partners
    • Power BI CSOs
    • Fabric Partner Community
    • 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
    • Power BI forums
    • Updates
    • 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 forums
    • 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: 

    Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

    • Power BI forums
    • Galleries
    • Quick Measures Gallery
    • The Mythical DAX Index

    The Mythical DAX Index

    05-17-2020 12:37 PM - last edited 05-18-2020 06:26 AM

    Super User Greg_Deckler
    Super User
    11589 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
    • Report Inappropriate Content

    The Mythical DAX Index

    ‎05-17-2020 12:37 PM

    It's weird where you find inspiration sometimes. I mean truly mind boggling. Recently, my current inspiration came from this seemingly innocuous thread. The author had implemented a rather nifty little DAX bubble sort that was obviously very tedious (because it's DAX trying to do a bubble sort) and was asking for help and advice on DAX table functions that could make the code cleaner and tidier. Turns out, the entire complex thing could be replaced with a single line of DAX code because CONCATENATEX includes an "Order by" feature that will sort your values for you. 

     

    So, the answer was to just use CONCATENATEX, a function obviously purpose built for this exact scenario. But then I was reprimanded for using a purpose built function to accomplish the exact task for which it was designed. 😞  But, because my brain works weird, instead of letting this make me sad, I decided to use it as an opportunity to invent something that I TRULY should be reprimanded for! 🙂

     

    And hence, the mythical DAX index. Let me just start by saying that you most certainly should NOT be using this and instead use Power Query to create an Index column. There is almost no use for this technique except that I CAN think of one, a scenario where you have a calculated table that you need an index on. Why would you have one of those? I don't know but it has come up before in these forums. Hmmm, and now I am thinking that this technique could have come in extremely handy for my TRIMMEAN function, I may have to revisit that one.

     

    In any case, this approach is superior to any that involves RANKX because it deals with duplicates flawlessly. It actually combines the CONCATENATEX approach with the approaches I was using converting numbers and strings to tables while I was working on Excel to DAX Translation. So basically, when you feed CONCATENATEX a table of values and do not specify a sort order, CONCATENATEX simply processes them in row order within the table. So, when you combine this with GENERATESERIES and PATHITEM, you can actually create an index that mimics what you would get in Power Query, an index that starts at 0 or 1 for the first row of data and increments up until the last row of data. Nifty.

     

    So @gtamir , sorry this took over a year to get you a simple DAX index per your request.

     

    DAX Indexed Table = 
        VAR __SourceTable = 'Table'
        VAR __Count = COUNTROWS(__SourceTable)
        VAR __SortText = CONCATENATEX('Table',[Product],"|")
        VAR __Table = 
            ADDCOLUMNS(
                GENERATESERIES(1,__Count,1),
                "Product",PATHITEM(__SortText,[Value],TEXT)
            )
    RETURN
        __Table

     

    NOTE: There are some interesting things that happen when you have duplicates. If you do not do any sorting, DAX groups duplicates together instead of keeping them in their places. So, definite variation from a Power Query index. But when you sort, you get true unique numbers for duplicates instead of how RANKX handles duplicates which, honestly, can be really infuriating sometimes...

    eyJrIjoiN2ZkNTA0YjktMWQxZi00MjJlLTg4M2MtYjE5YmM2ZDdkM2I1IiwidCI6IjRhMDQyNzQzLTM3M2EtNDNkMi04MjdiLTAwM2Y0YzdiYTFlNSIsImMiOjN9


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Preview file
    89 KB
    DAXIndex.pbix
    Labels:
    • Labels:
    • Other
    Message 1 of 5
    11,589 Views
    14
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    JOSELUISMTZRMZ1
    JOSELUISMTZRMZ1 Helper I
    Helper I
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎03-29-2023 02:34 PM

    Amazing! thank you very much it's what I'm looking for, As a meause I applied on mine but I'm getting the message

    "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value"

    I tried with a column with no duplicated values but message persists

     

    Any idea why?

    Message 3 of 5
    1,770 Views
    0
    Reply
    JOSELUISMTZRMZ1
    JOSELUISMTZRMZ1 Helper I
    Helper I
    In response to JOSELUISMTZRMZ1
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎03-29-2023 02:37 PM

    Oooops because of meausure expects one single value , lol. It has to be a table only, doesn't it?

    Message 4 of 5
    1,770 Views
    2
    Reply
    Greg_Deckler
    Super User Greg_Deckler
    Super User
    In response to JOSELUISMTZRMZ1
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎03-29-2023 03:45 PM

    @JOSELUISMTZRMZ1 Correct, this is for creating a table. It's possible to create this as a table var in a measure and then theoretically if you have a unique identifier you could essentially look up the value of the index.


    @ me in replies or I'll lose your thread!!!
    Instead of a Kudo, please vote for this idea
    Become an expert!: Enterprise DNA
    External Tools: MSHGQM
    YouTube Channel!: Microsoft Hates Greg
    Latest book!:
    Mastering Power BI 2nd Edition

    DAX is easy, CALCULATE makes DAX hard...
    Message 5 of 5
    1,757 Views
    1
    Reply
    gtamir
    gtamir Post Patron
    Post Patron
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎05-20-2020 07:17 AM

    @Greg_Deckler Well.... I am honored to have a special quick measure written for me. Thanks, I'll try it soon.

    Message 2 of 5
    11,151 Views
    0
    Reply

    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