Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Nazdac2024
Helper I
Helper I

Convert DAX prefix-based hierarchy lookup to Power Query custom column

I have the following DAX expression in a report that extracts the level 1 category name based on prefix-matching and a known hierarchy level:

Level_1 = 
VAR ThisCode = 'DimensionTable'[Code]
RETURN
CALCULATE (
    MAX('DimensionTable'[Name]),
    FILTER (
        'DimensionTable',
        'DimensionTable'[Level] = 1 &&
        LEFT(ThisCode, LEN('DimensionTable'[Code])) = 'DimensionTable'[Code]
    )
)

I want to implement the same logic directly in Power Query as a custom column, using M language. The logic should:

  • Use the first characters of a code field

  • Match it against rows where Level = 1

  • Return the corresponding Name from that row

    Nazdac2024_0-1750766408413.png

     

Could someone help me rewrite this logic correctly in Power Query?

 

1 REPLY 1
johnt75
Super User
Super User

You can try

= let 
    CurrentCode = [iec_code],
    Name =
    List.First(
        Table.Column(
            Table.SelectRows( #"Previous Step", each Text.StartsWith([iec_code], CurrentCode) and [level] = 1 ),
            "name"
        )
    )
in
    Name 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.