Forum Discussion

Aasozab's avatar
Aasozab
Regular Visitor
2 years ago
Solved

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?

 

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

     

     

    [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]

     

     

10 Replies

  • dufoq3's avatar
    dufoq3
    Community Champion

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

     

     

    [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]

     

     

    • Aasozab's avatar
      Aasozab
      Regular Visitor

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

      Please tell me:

      1. whether or not I followed your steps correctly?

      2. Why getting this error?

      TIA

       

      • dufoq3's avatar
        dufoq3
        Community Champion

         

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

         

         

  • dufoq3's avatar
    dufoq3
    Community Champion

    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".

  •     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"}
        )
  • Aasozab's avatar
    Aasozab
    Regular Visitor

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