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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Aasozab
Regular Visitor

How to swap some values between two columns in power query using either GUI elements or M code?

How to swap some values between two columns in power query using either GUI elements or M code?

Screenshot_4.png

 

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @Aasozab, I recommend you to create new pair of columns with extracted values:

 

dufoq3_1-1714419554519.png

 

[Extracted Date]

 

if (try Date.From([Column1]) otherwise false) is date then [Column1] else [Column2]

 

 

[Extracted Text]

 

if [Column1] = [Extracted Date] then [Column2] else [Column1]

 

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

10 REPLIES 10
AlienSx
Super User
Super User

    Table.ReplaceValue(
        your_table,
        each [DOJ],
        each [Designation],
        (v, o, n) => if Value.Is(n, Text.Type) then v else if Value.Is(v, Text.Type) then n else o, 
        {"DOJ", "Designation"}
    )
dufoq3
Super User
Super User

Hi, I dind´t provide the code with sample data, but you can create it either in power query vie Enger Data button or in excel and then import to power query.

 

The purpose was to show sou the way how to achieve expected result. You can use codes above by adding them separately as custom column. Just replace [Column1] with [DOJ] and [Column2] with [Designation]. Don't forget to set column name for first code: "Extracted Date".


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Aasozab
Regular Visitor

Thank you for your reply. Would you plz tell me how can i download sample data you provided in the solution?

dufoq3
Super User
Super User

Hi @Aasozab, I recommend you to create new pair of columns with extracted values:

 

dufoq3_1-1714419554519.png

 

[Extracted Date]

 

if (try Date.From([Column1]) otherwise false) is date then [Column1] else [Column2]

 

 

[Extracted Text]

 

if [Column1] = [Extracted Date] then [Column2] else [Column1]

 

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi @dufoq3 . I got these errors when followed your steps:

Screenshot_5.pngScreenshot_6.png

Please tell me:

1. whether or not I followed your steps correctly?

2. Why getting this error?

TIA

 

dufoq3_0-1714459500225.png

 

You have to add them into-your query as a custom column.

 

dufoq3_1-1714459541546.png

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

I have done so.

Check thse steps and plz tell where I have made a mistake. and why i am getting error in last step and plz also tell what would be next steps?

112233445566

You addec just "" into Custom Column. You have to add my code there 🙂 Read carefuly my guidelines again please.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

yes it works now. Thank you so much. I was confused by Note at bottom i.e. Note: Check this link to learn how to use my query. and was following these steps:

Aasozab_1-1714558365257.png

 

 

You're welcome. I have notes as signature, because usualy I use sample data.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

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.

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 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors