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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Nazdac2024
Advocate I
Advocate 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 ACCEPTED SOLUTION
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 

View solution in original post

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.