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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Jackofall
Resolver I
Resolver I

Calculated numeric field based on text field

I have a hard time understanding the prereqs of DAX.

I am playing around with a toy database to learn more about PBI. I have a table that cointains a text column named Rank. The values there are deliberately contradicting sortorder. S is highest then comes A etc. So in order to get the sorting I want I would usually use IF(Rank = 'S', 1, IF(Rank = 'B', 2, ...) in T-SQL or Report Builder to get the sort order I want.

 

When I try this in DAX I get an error message that there is no aggregate of the textcolumn.  "A single value.... without specifying an aggregation such as min, max,count or sum  to get a single result".
Dax_If.PNG

 

 

I found a much earlier Message here in the From about this problem and saw that it was better to use M in the query Editor.

if Text.Contains([Rank], "A") then "a" else "B"

I could get that to work as far as syntax goes but when I try to apply the query changes it takes forever to update 150 rows of data. I thought it bombed but it came through after 5ish minutes. When I looked at the code after apply went through I had.

= Table.AddColumn(dbo_Draft_ranking, "SortOrder", each if Text.Contains([Rank], "S") then "1" else if Text.Contains([Rank], "A") then "2" else if Text.Contains([Rank], "B") then "3" else if Text.Contains([Rank], "C") then "3" else "4")

I checked my SQL-DB there was no new column created as I feared for a while.

How should I aggregate a text column to get the measure to do the same thing as I managed in the modelling of the query? If Rank = "S", 1, 0 is simple enough but DAX gives me headache.

2 ACCEPTED SOLUTIONS
AlB
Community Champion
Community Champion

Hi @Jackofall

I think you want to do that in a calculated column rather than in a measure. There you will not have the problems of aggregation. In a measure you do not have a row context, that is why DAX does not know what you are referring to when you write Draft_Ranking[Rank]  

View solution in original post

AlB
Community Champion
Community Champion

@Jackofall

In fact what you are doing in M is add a column

View solution in original post

3 REPLIES 3
AlB
Community Champion
Community Champion

Hi @Jackofall

I think you want to do that in a calculated column rather than in a measure. There you will not have the problems of aggregation. In a measure you do not have a row context, that is why DAX does not know what you are referring to when you write Draft_Ranking[Rank]  

Thanks for the feedback.

When I changed the Default Summarization I could sord Rank by SortOrder.

What I would do in T-SQL is to sum(case when d.Rank = 'S' then 1 else 0 end) as 'S' so I understand where my reasoning was off. There is the aggregation Another way I force sort order is to use union and set different values  '3' as [Sortorder] then sort on that column in Report Builder. I managed to fool my self totaly. Thanks for straightening me out.

AlB
Community Champion
Community Champion

@Jackofall

In fact what you are doing in M is add a column

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors