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
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

 

 

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

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

 

 

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

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
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.