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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
MojoGene
Post Patron
Post Patron

Replace value and move to different column?

M code newbie question: I am trying to move the values in the "Country/Region" column into into the "Province/State" column and replace the moved value in Country/Region with a region name, e.g., "Europe". My efforts to find a solution are floundering. Any guidance appreciated.

Covid-19 Query Editor Screenshot.png

 

1 ACCEPTED SOLUTION

@MojoGene 

 

Right, there is no magic bullet for that. What I did was take the values in your Country/Region table and copied them. I pasted these into a new Enter Data query called Table. I then added a column for Region and put values in there for the region. Now, I got lazy and because I am from the US my geography skills suck so there are a lot of Europe's in there. Just click the gear icon next to the Source step and correct all that to what you want. It's manual, but you only have to do it once.

 

Now, I then added a Merge Queries step to the COVID-19 table that merges this new Table into the COVID-19 table so that you now have the desired Region column. Updated PBIX is attached.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

7 REPLIES 7
Greg_Deckler
Super User
Super User

Sample data is always helpful (as text). Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

 

That being said, right-click your Country/Region column and choose Duplicate column. Remove your existing Province/State column. Rename your duplicate Country/Region column to Province/State. You could then use Replace Values to replace "Italy" with Europe for example.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Greg:

Thanks for the response. The data shown in the screenshot is filtered to show only "Italy" and thus illustrate the problem. The Country/Region column actually has countries such as "United States" where the individual states appear in the "Province/State" column. So, the solution you suggested will remove this important data.

 

Here is a OneDrive link to the PBIX file: https://1drv.ms/u/s!AtPbkr1asYiKgeQ2gjY3fbohKiKrRg?e=Fy3AdU 

OK, I took a look at the PBIX.

 

The process is basically this in your case. Create a new Conditional Column. In this column use rules like the image:

image.png

 

Remove the original Province/State column. Rename the new column to Province/State. I have updated the PBIX for you, it is attached below my signature of this message.

 

 


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Greg: Thanks. That gets me to the last step, which is replacing the data in the Country/Region column with larger Regions, e.g., "Europe", "North America", etc. 

 

I am thinking that if I filtered the Country/Region column to, e.g., just European countries I could replace the values in that column with "European Region" or something like that.

 

It is easy enough to replace the values individually, but is there a way to replace every value in the (filtered) Country/Region column with one value?

@MojoGene 

 

Right, there is no magic bullet for that. What I did was take the values in your Country/Region table and copied them. I pasted these into a new Enter Data query called Table. I then added a column for Region and put values in there for the region. Now, I got lazy and because I am from the US my geography skills suck so there are a lot of Europe's in there. Just click the gear icon next to the Source step and correct all that to what you want. It's manual, but you only have to do it once.

 

Now, I then added a Merge Queries step to the COVID-19 table that merges this new Table into the COVID-19 table so that you now have the desired Region column. Updated PBIX is attached.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Greg:

Thanks once again. I was working on my own solution, but it was very inelegant compared to yours.

I'm on the right track now. 

Thanks again.

@MojoGene - Great! Let me know if you need anything else. It's a snowy Saturday here so I'm just chilling out and answering forum questions. There is zero to do here because of all this #@!$#!# COVID-19 @#$#@#@ shutting absolutely everything down here in the US.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

July Newsletter

Fabric Community Update - July 2024

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

Top Solution Authors
Top Kudoed Authors