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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Ashish_Mathur
Super User
Super User

Replace comma with semi colon at fixed intervals

Hi,

In 3 cells i have the following entries

 

2145,item a,-27,2.1

2187,item b,-32,2.17,2187,item c,-47,2.25

2111,item c,-47,2.23,2122,item a,-27,2.0,2187,item b,-32,2.15,2187,item d,-52,2.8

 

In each cell, i would like to replace the comma at every instance which is a multiple of 4 with a semi colon.  So the commmas at the 4,8,12,16,20 positions should be replaced with a semi colon.

 

Please help me with the M code for this.

 

Thank you. 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

= Table.AddColumn(Source, "Rpl", each let pos = try List.RemoveNulls(List.Zip(List.Split(Text.PositionOf([Column1], ",", 2),4)){3}?) otherwise {} in List.Accumulate(pos, [Column1], (s,c) => Text.ReplaceRange(s,c,1,";")))

CNENFRNL_0-1680698214780.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

8 REPLIES 8
Ahmedx
Super User
Super User

show the result as it should be

Thank you Ahmedx.  CNENFRNL has answered my question.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
CNENFRNL
Community Champion
Community Champion

= Table.AddColumn(Source, "Rpl", each let pos = try List.RemoveNulls(List.Zip(List.Split(Text.PositionOf([Column1], ",", 2),4)){3}?) otherwise {} in List.Accumulate(pos, [Column1], (s,c) => Text.ReplaceRange(s,c,1,";")))

CNENFRNL_0-1680698214780.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Hi,

Thank you for your help.  Could you kinldy clarify the following:

  1. What is the result of List.Zip in the second row - 2187,item b,-32,2.17,2187,item c,-47,2.25
  2. What is the {3}? doing there.  I read up on the ducumentation of List.Zip but did not find these inputs explained there.

Thank you for devoting your time and shating your knoeledge.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi, my friend, now you see how awful the readability of M is ... for this snippet of code

try List.RemoveNulls(List.Zip(List.Split(Text.PositionOf([Column1], ",", 2),4)){3}?) otherwise {}

you can strip it down this way,

let
    Source = "2187,item b,-32,2.17,2187,item c,-47,2.25",
    #"Pos of All Occurrences" = Text.PositionOf(Source, ",", Occurrence.All),
    #"Split by 4" = List.Split(#"Pos of All Occurrences", 4),
    Zipped = List.Zip(#"Split by 4"),
    #"4th Element of List" = Zipped{3}?, //"?" is "Selection and Projection Operators": https://learn.microsoft.com/en-us/powerquery-m/m-spec-operators
    #"Removed Nulls" = List.RemoveNulls(#"4th Element of List")

in
    #"Removed Nulls"

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Hi,

Thank you for that explanation.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Ahmedx
Super User
Super User

look i got you right

Screen Capture #815.png

Thank you for replying.  The result does not look correct.  I want every 4th comma to become a semi colon.  Also, please paste the code - you have only posted the image.

Thank you for your help.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.