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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
kd_pandey
New Member

Wildcard Lookup in Power Query/BI

kd_pandey_0-1713366124219.png

Hi All,

 

I need to populate category in first table from "Msg Code" table where "Error Msg" contains "Msg Code". (Power BI Data Modelling)

thanks in advance for your help.

1 ACCEPTED SOLUTION
amustafa
Super User
Super User

In your example, your Error Msg can be linked to multiple Cat. In this case it will get multiple Cat values seprated by a comma. In another case you could get the first Cat matched value. See the attached Excel file and Power Query section and results.

 

Multiple Matches.

 

let
    // Load the ErrorMsg table
    SourceErrorMsg = Excel.CurrentWorkbook(){[Name="TableErrorMsg"]}[Content],
    // Load the MsgCode table
    SourceMsgCode = Excel.CurrentWorkbook(){[Name="TableMsgCode"]}[Content],
    // Change column type for Error Msg to text
    #"Changed Type" = Table.TransformColumnTypes(SourceErrorMsg,{{"Error Msg", type text}}),
    
    // Add a custom column to perform the match and concatenate 'Cat' values
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Cat", each 
        let
            CurrentError = [Error Msg],
            // Filter rows in MsgCode table where Msg Code is a part of Error Msg
            FilteredRows = Table.SelectRows(SourceMsgCode, each Text.Contains(CurrentError, [Msg Code])),
            // Concatenate the 'Cat' values if there are matches
            ConcatenatedCats = if Table.IsEmpty(FilteredRows) then null else Text.Combine(FilteredRows[Cat], ", ")
        in
            ConcatenatedCats)
in
    #"Added Custom"

 

amustafa_0-1713381660279.png

 

Single Match...

 

let
    // Load the ErrorMsg table
    SourceErrorMsg = Excel.CurrentWorkbook(){[Name="TableErrorMsg"]}[Content],
    // Load the MsgCode table
    SourceMsgCode = Excel.CurrentWorkbook(){[Name="TableMsgCode"]}[Content],
    // Change column type for Error Msg to text
    #"Changed Type" = Table.TransformColumnTypes(SourceErrorMsg,{{"Error Msg", type text}}),
    
    // Add a custom column to perform the match and get the first 'Cat' value
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Cat", each 
        let
            CurrentError = [Error Msg],
            // Filter rows in MsgCode table where Msg Code is a part of Error Msg
            MatchedRows = Table.SelectRows(SourceMsgCode, each Text.Contains(CurrentError, [Msg Code])),
            // Get the first 'Cat' value if there is a match
            FirstCat = if Table.IsEmpty(MatchedRows) then null else MatchedRows[Cat]{0}
        in
            FirstCat),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Cat", type text}})
in
    #"Changed Type1"

 

amustafa_1-1713381696912.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

1 REPLY 1
amustafa
Super User
Super User

In your example, your Error Msg can be linked to multiple Cat. In this case it will get multiple Cat values seprated by a comma. In another case you could get the first Cat matched value. See the attached Excel file and Power Query section and results.

 

Multiple Matches.

 

let
    // Load the ErrorMsg table
    SourceErrorMsg = Excel.CurrentWorkbook(){[Name="TableErrorMsg"]}[Content],
    // Load the MsgCode table
    SourceMsgCode = Excel.CurrentWorkbook(){[Name="TableMsgCode"]}[Content],
    // Change column type for Error Msg to text
    #"Changed Type" = Table.TransformColumnTypes(SourceErrorMsg,{{"Error Msg", type text}}),
    
    // Add a custom column to perform the match and concatenate 'Cat' values
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Cat", each 
        let
            CurrentError = [Error Msg],
            // Filter rows in MsgCode table where Msg Code is a part of Error Msg
            FilteredRows = Table.SelectRows(SourceMsgCode, each Text.Contains(CurrentError, [Msg Code])),
            // Concatenate the 'Cat' values if there are matches
            ConcatenatedCats = if Table.IsEmpty(FilteredRows) then null else Text.Combine(FilteredRows[Cat], ", ")
        in
            ConcatenatedCats)
in
    #"Added Custom"

 

amustafa_0-1713381660279.png

 

Single Match...

 

let
    // Load the ErrorMsg table
    SourceErrorMsg = Excel.CurrentWorkbook(){[Name="TableErrorMsg"]}[Content],
    // Load the MsgCode table
    SourceMsgCode = Excel.CurrentWorkbook(){[Name="TableMsgCode"]}[Content],
    // Change column type for Error Msg to text
    #"Changed Type" = Table.TransformColumnTypes(SourceErrorMsg,{{"Error Msg", type text}}),
    
    // Add a custom column to perform the match and get the first 'Cat' value
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Cat", each 
        let
            CurrentError = [Error Msg],
            // Filter rows in MsgCode table where Msg Code is a part of Error Msg
            MatchedRows = Table.SelectRows(SourceMsgCode, each Text.Contains(CurrentError, [Msg Code])),
            // Get the first 'Cat' value if there is a match
            FirstCat = if Table.IsEmpty(MatchedRows) then null else MatchedRows[Cat]{0}
        in
            FirstCat),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Cat", type text}})
in
    #"Changed Type1"

 

amustafa_1-1713381696912.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors