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
stfromliPBI
Regular Visitor

error in this code

HI, I'm trying to create a search and when i use this code, there's an error in every field name after the first part of each Nameof Function.  How do i fix or rewrite this?
Here's the slicer - it uses the following code
stfromliPBI_0-1767284581605.png

_Categories =
    {
        ("Title - Artist", NAMEOF(TrackList[Track - Artist]), 0),
        ("Genre", NAMEOF('Genre'[Genre]), 1),
        ("Artist", NAMEOF('Artist'[Artist]), 2),
        ("Album", NAMEOF('Album'[Album]), 3),
        ("Record Label", NAMEOF('RecordLabel'[RecordLabel]), 4),
        ("Region", NAMEOF('Region'[Region]), 5),
        ("Retail Sales - Tracks", NAMEOF('PurchasedFrom'[PurchasedFrom]), 6),
        ("Retail Sales - Albums", NAMEOF('PurchasedFrom'[Purchased From]), 7)
    }


The error is "Cannot find Name [Track - Artist]"  
each of the tables/fields are valid but in each of the NAMEOF code, the error appears.
Is there a way to rewrite this?


ActiveSearchField =
VAR SelectedCategory = SELECTEDVALUE('_Categories'[CategorySelector])
RETURN
SWITCH(
SelectedCategory,
NAMEOF(TrackList[Track - Artist]), TrackList[Track - Artist],
NAMEOF(Genre[Genre]), Genre[Genre],
NAMEOF(Artist[Artist]), Artist[Artist],
NAMEOF(Album[Album]), Album[Album],
NAMEOF(RecordLabel[RecordLabel]), RecordLabel[RecordLabel],
NAMEOF(Region[Region]), Region[Region],
NAMEOF(PurchasedFrom[PurchasedFrom]), PurchasedFrom[PurchasedFrom],
BLANK()
)

7 REPLIES 7
v-hashadapu
Community Support
Community Support

Hi @stfromliPBI , Hope you're doing fine. Can you confirm if the problem is solved or still persists? Sharing your details will help others in the community.

cengizhanarslan
Solution Sage
Solution Sage

A measure cannot return a column reference dynamically. Measures return a single scalar value, not “whichever column the user chose”.

So you should not try to build a slicer that changes which column a measure returns.

  • Use the field parameter _Categories as the thing that switches columns in the visual axis/rows/columns (that’s what field parameters are for).

  • If you need a “search” experience, you have two common options:

    1. Use the slicer’s built-in Search box (works when the slicer is bound to the parameter field you’re showing).

    2. If you mean “type text and filter rows”, use a disconnected search text table + a measure that returns 1/0 and apply it as a visual filter.

Here’s a working “typed search” measure pattern (apply it as a visual-level filter = 1).
Assumes you have a disconnected table SearchTerm with a single column [Term] (or use a parameter / what-if table).

Search Match =
VAR term = LOWER(SELECTEDVALUE(SearchTerm[Term], ""))
VAR cat  = SELECTEDVALUE('_Categories'[CategorySelector])
VAR valueToSearch =
    LOWER(
        SWITCH(
            cat,
            "Title - Artist", SELECTEDVALUE('TrackList'[Track - Artist]),
            "Genre",          SELECTEDVALUE('Genre'[Genre]),
            "Artist",         SELECTEDVALUE('Artist'[Artist]),
            "Album",          SELECTEDVALUE('Album'[Album]),
            "Record Label",   SELECTEDVALUE('RecordLabel'[RecordLabel]),
            "Region",         SELECTEDVALUE('Region'[Region]),
            "Retail Sales - Tracks", SELECTEDVALUE('PurchasedFrom'[PurchasedFrom]),
            "Retail Sales - Albums", SELECTEDVALUE('PurchasedFrom'[PurchasedFrom]),
            ""
        )
    )
RETURN
IF(
    term = "" || CONTAINSSTRING(valueToSearch, term),
    1,
    0
)

SELECTEDVALUE() works here because the visual provides row context (each row has a single value for that field). This is how you “switch what text you search” without trying to return a column.

 

_________________________________________________________
If this helped, ✓ Mark as Solution | Kudos appreciated
Connect on LinkedIn
FBergamaschi
Solution Sage
Solution Sage

The Field Parameters (and therefore the NAMEOF function) have another purpose.

 

I believe you should create a table with the values you want and use that.

 

Here is the approach:

 

Category =
SELECTCOLUMNS(
    {
        "Title - Artist",
        "Genre"
    },
    "Category to Select", [Value]
)
 
FBergamaschi_0-1767523949634.png

 

FBergamaschi_2-1767524038913.png

 


 

 

But it is unclear to me what you are trying to achieve, can you explain the goal? So we can more easily help you up to the solution

 

If this helped, please consider giving kudos and mark as a solution

@me in replies or I'll lose your thread

Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

Consider voting this Power BI idea

Francesco Bergamaschi

MBA, M.Eng, M.Econ, Professor of BI

v-hashadapu
Community Support
Community Support

Hi @stfromliPBI , Thank you for reaching out to the Microsoft Community Forum.

 

This error is happening because field parameters and NAMEOF() are being used in an unsupported way. Field parameters are not real columns that DAX can resolve inside a measure. They are evaluated by the visual layer, not by the DAX engine. Because of that, a SWITCH() measure cannot dynamically return different columns using NAMEOF(), even though those columns exist and even though their names are valid.

 

There is no syntax fix for this. To achieve a searchable or switchable field, you must either place the field parameter directly on the slicer or visual and let Power BI handle the selection or redesign the logic using a disconnected selector table with a SWITCH() measure that explicitly references each column (without NAMEOF()). Field parameters cannot be dereferenced inside measures.

danextian
Super User
Super User

Hi @stfromliPBI 

 

Hi  As what  has mentioned, that isn't how you use field parameters. Field parameters are not physical columns themselves but serve as a field selector.  Even if you remove the Track - Artist row, you will run into a similar error but  for a different field.  

 

This has already been posted already, and it’s still unclear what the actual goal is. If the intent is to use the selected column as the search field, the slicer header already shows the selected column by default. If the requirement is to visually highlight the selected column in the field list, that will require a disconnected table as using the same or related table will filter the visible rows to what's been selected.

danextian_0-1767326941419.png

Also, if the goal is to get a solution that’s as close as possible to what’s needed, please provide a sample PBIX. As mentioned in another post, the solution works in the sample file but not in yours, which suggests there may be model or setup details we’re not aware of.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
nidhigk
Frequent Visitor

Hi, 
Would recommend the following:
1. Rename Track - Artist to TrackArtist . This is because NameOf() function is very strict. And does not allow hyphens. Do this if Renaming is possible.
2. Other Option is try using Field Parameters
Rewrite without using NameOf()

Create Field Parameter table:
_Categories =
{
("Title - Artist", "TrackArtist", 0),
("Genre", "Genre", 1),
("Artist", "Artist", 2),
("Album", "Album", 3),
("Record Label", "RecordLabel", 4),
("Region", "Region", 5),
("Retail Sales - Tracks", "PurchasedFrom", 6),
("Retail Sales - Albums", "Purchased From", 7)
}

Create active search field:

ActiveSearchField =
VAR SelectedCategory =
SELECTEDVALUE('_Categories'[CategorySelector])
RETURN
SWITCH(
SelectedCategory,
"TrackArtist", TrackList[Track - Artist],
"Genre", Genre[Genre],
"Artist", Artist[Artist],
"Album", Album[Album],
"RecordLabel", RecordLabel[RecordLabel],
"Region", Region[Region],
"PurchasedFrom", PurchasedFrom[PurchasedFrom],
"Purchased From", PurchasedFrom[Purchased From],
BLANK()
)

Please try, see if it works. 






lbendlin
Super User
Super User

That's not how you are supposed to use Field Parameters.  What are you trying to achieve?

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!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.