2

I'd like to create a calculated column that reverses the string in an existing column. For example, if I have the following table

ID  String
----------
1   Alpha
2   Beta
3   Gamma

I'd like to get this:

ID  String  Reverse
-------------------
1   Alpha   ahplA
2   Beta    ateB
3   Gamma   ammaG

I know how to use SEARCH, FIND, LEFT, RIGHT, MID, SUBSTITUTE, REPLACE, REPT, LEN etc. but can't seem to get them to do what I want.


I'm providing one possible solution below, but I'm interested in other possibilities as well.

Alexis Olson
  • 38,724
  • 7
  • 42
  • 64

2 Answers2

1

May not be pretty, but one approach to this is to generate an index, reverse it, and then concatenate each character by the reversed index:

Reverse =
    CONCATENATEX (
        ADDCOLUMNS (
            GENERATESERIES ( 1, LEN ( Table1[String] ) ),
            "Index", LEN ( Table1[String] ) - [Value] + 1
        ),
        MID ( Table1[String], [Index], 1 )
    )

For the string Alpha, which has 5 characters, this generates the following table:

Value  Index = 5 - Value + 1
----------------------------
 1      5
 2      4
 3      3
 4      2
 5      1

Now for each row in this table, the MID function picks the character in the Index position:

Value  Index  MID("Alpha", Index, 1)
------------------------------------
 1      5      a
 2      4      h
 3      3      p
 4      2      l
 5      1      A

The CONCATENATEX function takes this table and concatenates the third column to get ahplA.

Alexis Olson
  • 38,724
  • 7
  • 42
  • 64
1

You're asking for DAX specifically. But since you're also saying that you're looking for other possibilities than the solution you've provided yourself, I feel compelled to mention a very elegant approach using Python in Power BI:

#In:
'StackOverflow'[::-1]

#Out:
'wolfrevOkcatS'

That's it!

vestland
  • 55,229
  • 37
  • 187
  • 305