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

DAX query to create new boolean columns

Hello, 

 

I need help with the following DAX query. I am using Power Bi Desktop. I have a table_1 with the following fields

 

record_ID

text

record_1

'blablabla'

record_2

'blablabla2'

 

where record_ID is the primary key (unique values) and a table_2 with different tags related to the records in table_1

 

record_IDtag_ID
record_1tag1
record_1tag2

record_2

tag1

record_2

tag3

 

and a third table with the name related to the tag

 

tag_IDtag_name
tag1name_tag1
tag2name_tag2
tag3name_tag3

 

Given that I have different tags in total (3 in the example), I would like to

  1. add to table_1 n different boolean columns with true/false values if the related record_id has that tag or not;
  2. add a final column with all the tags concatenated and comma-separated.

In this example, the result would be:

 

record_ID

textname_tag1name_tag2name_tag3all_tags

record_1

'blablabla'

True

True

False

name_tag1, name_tag2

record_2

'blablabla2'

True

False

True

name_tag1, name_tag3

 

how can I achieve this using DAX?

1 ACCEPTED SOLUTION
JustJan
Responsive Resident
Responsive Resident

Hi@Anonymous 

 

You are looking for a sort of unpivot function to create colunms using DAX. You can do this in m-query, but as far as I know you can't dynamically create columns in dax without some sort of manual intervention.

Using a measure it is no problem to create your table.

2020-07-23 20_18_42-Untitled - Power BI Desktop.png

 

hope this still a bit helpfull.

 

Jan

 

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

@Anonymous .

 

Follow  these steps.

 

1. Choose matrix visual.

2. Drag recordId column from table1 on Rows.

3. Drag tag_name column on Columns.

4 . Use the measure suggested by @JustJan on Values.

 

 

Thats all. 

Hope this helps.

 

Appreciate with kudos.

Mark as solution if this resolves your problem.

 

Thanks

 

Anonymous
Not applicable

@Anonymous 

 

Easier than I expected 🙂 thanks a lot, very helpful. Not sure about which reply I should mark as solution, but I guess it's the one from @JustJan since he provided the measure.

JustJan
Responsive Resident
Responsive Resident

Hi@Anonymous 

 

You are looking for a sort of unpivot function to create colunms using DAX. You can do this in m-query, but as far as I know you can't dynamically create columns in dax without some sort of manual intervention.

Using a measure it is no problem to create your table.

2020-07-23 20_18_42-Untitled - Power BI Desktop.png

 

hope this still a bit helpfull.

 

Jan

 

 

Anonymous
Not applicable

Hi @JustJan ,

 

thanks for your help and sorry for the late response.

When I try your DAX formula, I get the error "Expressions that yield variant data-type cannot be used to define calculated columns." if I create a new column with your expression. If, as you suggest, I create a measure, then I am able to use it in a visual, but only the "Total" column is created, not the boolean columns Tag_T1, Tag_T2 etc.

 

I have a further question: is creating a measure, rather than defining a new table in the data model, the most efficient way to do it? Wouldn't a new table allow me to more flexibility when creating new visuals based on this data? I am asking since I am completely new to power bi. Thank you.

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.