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
    • Re: DAX's PATH function equivalent Custom Column i...

    Re: DAX's PATH function equivalent Custom Column in Power Query

    12-21-2021 19:27 PM

    justlogmein
    Helper II
    1946 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    Zubair_Muhammad
    Super User Zubair_Muhammad
    Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    DAX's PATH function equivalent Custom Column in Power Query

    ‎09-24-2019 12:38 AM

    This custom column formula in Power Query provides an equivalent of DAX's PATH function.

    If you have many levels (PATHLENGTH is greater than say 10), it can save you time having to create calculated columns to get each PATHITEM

    With Power Query, you can simply split the PATH into PATHITEMS with one click.

     

    Just change the text in Red Color font in below formula according to your column names and previous step name. See the picture for guidance

     

     

    =let //Define your columns below
     c=[Child],p=[Parent],mytable=#"Changed Type",pc="Parent",cc="Child" 
      in
    let mylist={c} & List.Generate(()=>[x=0,y=p,w=1],each [w] > 0,each [z=[y], 
    x=Table.Column(Table.SelectRows(mytable,each Record.Field(_,cc)=z),pc),y=x{0},w=List.Count(x)
    ],
    each [y])
            in
    Text.Combine(List.Reverse(List.RemoveItems(
    List.Transform(mylist,each Text.From(_)),{null,""})),"|")

     

     

    PATH.jpg

     


    Regards
    Zubair

    Please try my custom visuals
    • Hierarchical Bar Chart
    • Multiple Sparklines
    • Cross the River Game
    Preview file
    301 KB
    Replicating DAX's Path function in M LG.pbix
    Labels:
    • Labels:
    • Other
    Message 1 of 7
    9,709 Views
    4
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    Oscar_Mtz_V
    Oscar_Mtz_V Kudo Collector
    Kudo Collector
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎04-18-2022 12:19 AM

    @Zubair_Muhammad , thanks a lot!
    Just a small note for those that might be working with records with orphan records (Parent = null), you might need to fill this with the same "child id". This code can come in handy:

    = Table.ReplaceValue(#"Replaced Errors",null, each _[Child],Replacer.ReplaceValue,{"Parent"})

    Cheers!

    Oscar

     

    Don't forget to follow my BI blog in www.bibb.pro

     

    Bibb Logo-03.png

     

     

    Message 7 of 7
    681 Views
    1
    Reply
    a1b1c1
    a1b1c1 Advocate I
    Advocate I
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-27-2019 02:15 AM

     Hi @Zubair_Muhammad, @Daniil 
    Either of the steps are too time consuming, it's been running for about 45 minutes and still hasn't been able to complete this step. Any help?

    Message 5 of 7
    7,838 Views
    2
    Reply
    justlogmein
    justlogmein Helper II
    Helper II
    In response to a1b1c1
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-21-2021 07:27 PM

    I have tried this method and you are correct, this is much too inefficient. I have only 6,000 rows and we are talking hours to run. Did you find another solution?

    Message 6 of 7
    1,946 Views
    0
    Reply
    Daniil
    Daniil Kudo Kingpin
    Kudo Kingpin
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎09-24-2019 04:23 AM

    Good work! Just to simplify the formula a bit:

    let //Define your columns here
    mytable=ChangedType,p="Parent",c="Child" 
      in
    let mylist={Record.Field(_,c)} & List.Generate(()=>[x=0,y=Record.Field(_,p),w=1],each [w] > 0,each [z=[y], x=Table.Column(Table.SelectRows(mytable,each Record.Field(_,c)=z),p),y=x{0},w=List.Count(x)
    ],
    each [y])
            in
    Text.Combine(List.Reverse(List.RemoveItems(
    List.Transform(mylist,each Text.From(_)),{null,""})),"|")
    Message 2 of 7
    9,621 Views
    3
    Reply
    asoysal
    asoysal
    New Member
    In response to Daniil
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎12-12-2019 04:53 AM

    Thanks to @Daniil and @Zubair_Muhammad . I created the custom column,with no syntax error in the Custom Column creation step. However I receive an Error in the table view. 

    See below:

     

    = let //Define your columns here
    mytable=#"Changed Type",p=[#"DirectReportTo Asso. No."],c=[#"Asso. No."]
    in
    let mylist={Record.Field(_,c)} & List.Generate(()=>[x=0,y=Record.Field(_,p),w=1],each [w] > 0,each [z=[y], x=Table.Column(Table.SelectRows(mytable,each Record.Field(_,c)=z),p),y=x{0},w=List.Count(x)
    ],
    each [y])
    in
    Text.Combine(List.Reverse(List.RemoveItems(
    List.Transform(mylist,each Text.From(_)),{null,""})),"|")

    Message 4 of 7
    8,206 Views
    0
    Reply
    Zubair_Muhammad
    Super User Zubair_Muhammad
    Super User
    In response to Daniil
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Email to a Friend
    • Report Inappropriate Content

    ‎09-24-2019 04:42 AM

    Thanks @Daniil 


    Regards
    Zubair

    Please try my custom visuals
    • Hierarchical Bar Chart
    • Multiple Sparklines
    • Cross the River Game
    Message 3 of 7
    9,613 Views
    0
    Reply

    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