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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
jonclay
Helper IV
Helper IV

Showing values as a string of text

Hi everyone

I have a field in my SQL database that shows a field called Type_ with a set of numbers separated by a comma for several million record e.g.

jonclay_0-1640283113331.png


Is it possible to add a column / measure that says "if the field Type_ contains 15742000 then show this as A and if the field contains / also contains 154720004 then show this as B)? So, in this example we would show A, B in stead of 15472000, 154720004. There are many of these numbers and therefore literally thousands of combinations, otherwise I would create a lookup table and link that.

Any help would be much appreciated.

Many thanks
Jon

1 ACCEPTED SOLUTION
harshnathani
Community Champion
Community Champion

Hi @jonclay ,

 

This may not be the best way. But you can try something like this.

 

Create a Calculated Column

 

Column = SUBSTITUTE(
SUBSTITUTE(Table4[Type],"154720004","B"),"15472000","A")

 

 

12.JPG

 

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

 

View solution in original post

5 REPLIES 5
smpa01
Super User
Super User

@jonclay   what is the RDMBMS? Is it MSSQL? 

 

If there is no filter context involved in this, I would strongly suggest you to do this transformation server side for a million row table. An example in TSQL with a lookup table

 

 

DECLARE @fact AS TABLE
  (
     rownum INT,
     descr  VARCHAR (40)
  )

INSERT INTO @fact
SELECT *
FROM   (VALUES(1,
       'email,phone'),
              (2,
       'email,phone,pager'),
              (3,
       'fax,computer')) t(a, b)

DECLARE @lookup AS TABLE
  (
     _name    VARCHAR(10),
     _altname VARCHAR(10)
  )

INSERT INTO @lookup
SELECT *
FROM   (VALUES('email',
       'e'),
              ('phone',
       'p1'),
              ('pager',
       'p2'),
              ('fax',
       'f'),
              ('computer',
       'c')) t(a, b);

WITH cte1
     AS (SELECT rownum,
                x.value AS _splitValue,
                z._altname
         FROM   @fact
                CROSS apply String_split(descr, ',')x
                CROSS apply (SELECT y.*
                             FROM   @lookup y
                             WHERE  x.value = y._name)z)
SELECT a1.*,
       x.plc
FROM   @fact a1
       CROSS apply(SELECT *
                   FROM  (SELECT rownum,
                                 Stuff((SELECT ',' + _altname
                                        FROM   cte1 a
                                        WHERE  a.rownum = b.rownum
                                        FOR xml path ('')), 1, 1, '') AS plc
                          FROM   cte1 b
                          GROUP  BY rownum) a2
                   WHERE  a1.rownum = a2.rownum) x 

 

 

 

From here

smpa01_0-1640286672572.png

 

to

smpa01_1-1640286696042.png

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
harshnathani
Community Champion
Community Champion

Hi @jonclay ,

 

This may not be the best way. But you can try something like this.

 

Create a Calculated Column

 

Column = SUBSTITUTE(
SUBSTITUTE(Table4[Type],"154720004","B"),"15472000","A")

 

 

12.JPG

 

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

 

@harshnathani Thanks so much, this works perfectly!

parry2k
Super User
Super User

@jonclay in this example your new value will be B since all the rows contain both the values, correct?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k Many thanks for your reply. No, the field can contain any number of different values e.g. 15470000, 15470001, 15470002 and so on.

Helpful resources

Announcements