Home > Mobile >  How can I reproduce index/match lookup with wildcards in Excel data model?
How can I reproduce index/match lookup with wildcards in Excel data model?

Time:07-08

TL;DR: The title says it all: is there any way to reproduce an excel INDEX-MATCH look up in the data model with wildcards

I am trying to reorganize an Excel file that uses PowerQuery to import and transform data from external sourced and load it to the data model. This data is then used to produce several pivot tables and extract specific data. I have the need to add a calculated column in the data model that would be relatively easy to calculate with a plain Excel formula but I cannot find any way to make it in the data model. The problem is that the data model is great at replacing vlookups but it does not support wildcards, which are essential in this use scenario. The following is of course a toy example to illustrate the issue:

I have a fact table tblFact that looks like this:

Fact table

The idea is to add column that display a message depending on columns Country, Model and SalesMan, with the following logic:

  • If Model is A1, regardless of the other columns, display "Sold an A1"
  • If SalesMan is Mario, regardless of the other columns, display "ITSAME"
  • If Country is US, regardless of the other columns, display "OH YEAH"
  • In any other case display the string "Default message"

In the original file this was done by adding a nested IF, like this:

=IF([@Model]="A1";"Sold an A1";IF([@SalesMan]="Mario";"ITSAME";IF([@Country]="US";"OH YEAH";"Default message")))

But, since in the real case there are many more cases, with more complex conditions and they need to be updated fairly often, it was very hard to mantain. I thought of adding a look up table and take advantage of the fact that INDEX and MATCH allow for wildcards.

look up table

The following formula would replicate the behaviour of the nested IFs: =INDEX(tblLookUp[Message];MATCH(1;MATCH(tblLookUp[LUKey];[@LUValue];0);0)) And the lookup is much easier to mantain and add specific cases.

look up result

Now this would work if not for the fact that I am forced to use the data model:

  • The real file contains more than a million rows and about 50 columns, I cannot dump everything in a table and add a column to use the formula with INDEX MATCH
  • Some of the columns that need to be looked up are only available in the data model as come from related tables in the data model. As if Salesman were calculated as =RELATED(yetAnotherTable[SalesMan])

I have tried the LOOKUPVALUE function but it does not support wildcards, I have also tried various combinations of FILTER and SEARCH but I couldn't even get a formula that doesn't return an error.

Other relevant notes:

  • I am aware that the index match lookup is sensitive to the order of the lookup table but I can enter an external check
  • Some of the columns that form LUValue in the fact table can contain litterally anything, it is not possible to enter in the look up table all possible cases.
  • I'd rather find a solution in DAX to be used in the data model, but if it is not possible I could, in theory, have power query calculate all columns of tblFact, and try to make a look up in power query. Problem is at that point that Power Query's merge does not support wildcards.

CodePudding user response:

Try this Calculated Column:

=
PATHITEM(
    CONCATENATEX(
        FILTER( tblLookUp, SEARCH( tblLookUp[LUKey], MainTable[LUValue],, 0 ) ),
        tblLookUp[Message],
        "|"
    ),
    1
)

SEARCH supports wildcards.

  • Related