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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Using variable name as column name in DAX

I'm trying to build dynamic RCL in PowerBI. Below is my table structures.

 

AccessControlTable 

MailIDEntityEntityValue
jan_doeUnitABC
joe_doeSubUnitDEF

 

ProjectDetails

ProjectCodeUnitSubUnit
PID100ABCDEF
PID200XYZABC

 

RLS DAX Query:

 

=
VAR NEWTABLE = CALCULATETABLE (AccessControlTable,PATHCONTAINS(LOWER(AccessControlTable[MailID]),
SUBSTITUTE(LOWER(USERNAME()),"itlinfosys\","")))

VAR ENTITY = SELECTCOLUMNS(NEWTABLE,"Entity", [Entity])
VAR ENTITYVALUE = SELECTCOLUMNS(NEWTABLE,"EntityValue", [EntityValue])

RETURN CONTAINS(ProjectDetails,ProjectDetails[ENTITY],ENTITYVALUE) // ENTITY is the variable name

 

Above code doesn't work, throwing error as below.

ahmmadsuroor_0-1665762659881.png

I tried using `SWITCH` and it works, but since I have 100s of ENTITY types it's not efficient method.

 

RETURN SWITCH(
    TRUE(),
    ENTITY = "Unit", CONTAINS(ProjectDetails,ProjectDetails[Unit],ENTITYVALUE),
    ENTITY = "SubUnit", CONTAINS(ProjectDetails,ProjectDetails[SubUnit],ENTITYVALUE),
    ENTITY = "Category1", CONTAINS(ProjectDetails,ProjectDetails[Category1],ENTITYVALUE),
    ENTITY = "Category2", CONTAINS(ProjectDetails,ProjectDetails[Category2],ENTITYVALUE),
    ...
)

 

 

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

I don't think it's possible to dynamically reference a column using a string variable in DAX, unfortunately. (Anyone please correct me if this is not true.)

 

I'd recommend unpivoting your ProjectDetails table to match the shape of the AccessControlTable like this:

AlexisOlson_0-1665786152819.png

 

Then you should be able to write something like

CONTAINSROW ( { ENTITY, ENTITYVALUE }, ProjectDetails[Entity], ProjectDetails[EntityValue] )

View solution in original post

1 REPLY 1
AlexisOlson
Super User
Super User

I don't think it's possible to dynamically reference a column using a string variable in DAX, unfortunately. (Anyone please correct me if this is not true.)

 

I'd recommend unpivoting your ProjectDetails table to match the shape of the AccessControlTable like this:

AlexisOlson_0-1665786152819.png

 

Then you should be able to write something like

CONTAINSROW ( { ENTITY, ENTITYVALUE }, ProjectDetails[Entity], ProjectDetails[EntityValue] )

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.