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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Blevels
Frequent Visitor

Power Query CASE to SELECT from another Table

Hello,

How does one recreate the following in Power Query:

SELECT 
COLUMN1, ..., COLUMNX,
(CASE WHEN COLUMNX = '' THEN "DNE"
ELSE (SELECT [VALUE] FROM [CODES_TABLE] WHERE [POSITION] = 1
         AND [LOOKUPVALUE] = SUBSTRING([CODES_TABLE], 1,1)
         ) END) as CUSTOMERTYPE,
.... COLUMNY, COLUMNZ
FROM [CUSTOMERS]

 

Doing the above where the assumption is that the [CODES_TABLE] is another query and the CUSTOMERTYPE 

column needs to select from the query in a CASE statement? 

 

Thank you for your response(s) in advance.

1 ACCEPTED SOLUTION

Hello - this will return the expected result.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vcw5DsAgDETRu7imsCFkKbNvSk9kcf9rZKCI5MLF+xpZlYQcjTgvnvmm7JQ8OJWUAvNTUwDnsnyFOdXUgItNEVxtasGt/Ip/6sDdrnrwsGkAT5uE4QtHOX8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, ColumnX = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", type text}, {"ColumnX", type text}}),
    Result = Table.AddColumn ( 
        #"Changed Type", "CUSTOMERTYPE", each if [ColumnX] = "" then "DNE" else Table.SelectRows(
        Codes, 
        (x)=> 
            x[LookupValue]=Text.Start([ColumnX], 1)
        ){0}[Value], type text 
    )
in
    Result

jennratten_0-1669822262123.png

 

 

 

View solution in original post

3 REPLIES 3
Blevels
Frequent Visitor

CUSTOMERS TABLE:

Column1Column2ColumnXCUSTOMERTYPE

1A21200K 
2B2X300M 
3CAY100X 
4DAY100X 
5EAY100X 
6F25100X 
7GAY100X 
8HAY100X 
9IAY100X 

 

CODES_TABLE:

IDPositionLookupValueAttributeValue

111Customer TypeHEALTH CARE
212Customer TypeAUTOMOTIVE
313Customer TypeMANUFACTURING
414Customer TypeTECHNOLOGY
515Customer TypePRODUCTION
616Customer TypeCONSTRUCTION
717Customer TypeTRADE
818Customer TypeFINANCE
919Customer TypeEDUCATION
101ACustomer TypeMILITARY

 

EXPECTED OUTCOME:

Column1Column2ColumnXCUSTOMERTYPE

1A21200KAUTOMOTIVE
2B2X300MAUTOMOTIVE
3CAY100XMILITARY
4DAY100XMILITARY
5EAY100XMILITARY
6F25100XAUTOMOTIVE
7GAY100XMILITARY
8HAY100XMILITARY
9IAY100XMILITARY

Hello - this will return the expected result.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vcw5DsAgDETRu7imsCFkKbNvSk9kcf9rZKCI5MLF+xpZlYQcjTgvnvmm7JQ8OJWUAvNTUwDnsnyFOdXUgItNEVxtasGt/Ip/6sDdrnrwsGkAT5uE4QtHOX8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, ColumnX = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", type text}, {"ColumnX", type text}}),
    Result = Table.AddColumn ( 
        #"Changed Type", "CUSTOMERTYPE", each if [ColumnX] = "" then "DNE" else Table.SelectRows(
        Codes, 
        (x)=> 
            x[LookupValue]=Text.Start([ColumnX], 1)
        ){0}[Value], type text 
    )
in
    Result

jennratten_0-1669822262123.png

 

 

 

lbendlin
Super User
Super User

Please provide sanitized sample data that fully covers your issue.
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Kudoed Authors