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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
DJKarma
Frequent Visitor

Create a conditional column based on contents of data

This is a simple request.
I have to create a column in query editor, or in table view. Whichever is easy.

Column looks like this -->

A,B,C,D,D,E,D

B,C,D,B,D,A

C,C,D,F,E,G

D,D,E,E,E,F,B

 

 

Result should be based on count of characters present, with 'A' character always taking the priority.
For instance result of the above column next to it will be

A ( A will take priority even if D has most count)

A (Even though B has most count,  A will take Priority)

C ( as C has most count)

E ( as E has most count)

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

hI @DJKarma 
Please refer to attached file with the solution 

1.png

Result = 
VAR Items = SUBSTITUTE ( 'Table'[Column], ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "@Item", PATHITEM ( Items, [Value] ) )
VAR T3 = GROUPBY ( T2, [@Item], "@Count", COUNTX ( CURRENTGROUP(), 1 ) )
VAR T4 = TOPN ( 1, T3, [@Count] )
VAR Result = MAXX ( T4, [@Item] )
RETURN
    IF ( PATHCONTAINS ( Items, "A" ), "A", Result )

View solution in original post

7 REPLIES 7
tamerj1
Super User
Super User

hI @DJKarma 
Please refer to attached file with the solution 

1.png

Result = 
VAR Items = SUBSTITUTE ( 'Table'[Column], ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "@Item", PATHITEM ( Items, [Value] ) )
VAR T3 = GROUPBY ( T2, [@Item], "@Count", COUNTX ( CURRENTGROUP(), 1 ) )
VAR T4 = TOPN ( 1, T3, [@Count] )
VAR Result = MAXX ( T4, [@Item] )
RETURN
    IF ( PATHCONTAINS ( Items, "A" ), "A", Result )

Thanks, I am not able to check this, as there are also blanks in my column which I cant remove.
Can you edit your code to handle blanks as well?
If blank then output "XX"

Hi @DJKarma 
See attached

1.png

Result = 
VAR String = COALESCE ( 'Table'[Column], "XX" )
VAR Items = SUBSTITUTE ( String, ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "@Item", PATHITEM ( Items, [Value] ) )
VAR T3 = GROUPBY ( T2, [@Item], "@Count", COUNTX ( CURRENTGROUP(), 1 ) )
VAR T4 = TOPN ( 1, T3, [@Count] )
VAR Result = MAXX ( T4, [@Item] )
RETURN
    IF ( PATHCONTAINS ( Items, "A" ), "A", Result )

It was a polite request , to know as in how this is working.
Can you please tell me how every line works in short?

Hi @DJKarma 
I'm returning the variables one by one for better understanding

1.png2.png3.png4.png5.png6.png

amitchandak
Super User
Super User

@DJKarma , Can you please share expected output

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

A ( A will take priority even if D has most count)

A (Even though B has most count,  A will take Priority)

C ( as C has most count)

E ( as E has most count)

 

 

Just the characters from the column, i have written reasons in the bracket which you can exclude

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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