Text.Reverse reverses the order of the characters in a text value. Available in Excel (Power Query), Power BI Desktop, and Power BI Service.
If you want to flip a string so its characters run back to front, or check whether a word reads the same in both directions, this is the function you reach for.
Syntax of Text.Reverse Function
Text.Reverse(text as nullable text) as nullable text
where
text(required, nullable text). The text value whose characters you want to reverse.
Returns: a text value with the characters of text in reverse order. If text is null, it returns null.
In plain terms, you hand it a string and it gives you the same characters read from the last one to the first.
Example 1: Reverse a single word
Reverse the characters in the word Spreadsheet.
let
Result = Text.Reverse("Spreadsheet")
in
Result
Result: teehsdaerpS
Every character is flipped end to end, so the last letter becomes the first.
Example 2: Check whether a word is a palindrome
A palindrome reads the same forwards and backwards. Compare a word to its reversed self to test for one.
let
Word = "racecar",
Result = Text.Reverse(Word)=Word
in
Result
Result: true
racecar reversed is still racecar, so the comparison returns true.
Example 3: Reverse the text in a whole column
Reversing one value is handy, but you will usually want to do it for every row in a column.
Say you have a Codes query with a single SKU column.
Here is the starting data:
| SKU |
|---|
| ABC-123 |
| WIDGET-7 |
| BOLT-M8 |
Add a new column that reverses each SKU with Table.AddColumn:
let
Source = Excel.CurrentWorkbook(){[Name="Codes"]}[Content],
Result = Table.AddColumn(Source,"Reversed",each Text.Reverse([SKU]),type text)
in
Result
This keeps the original SKU and adds a Reversed column with each code flipped.
The result is:
| SKU | Reversed |
|---|---|
| ABC-123 | 321-CBA |
| WIDGET-7 | 7-TEGDIW |
| BOLT-M8 | 8M-TLOB |
The hyphens and digits reverse along with the letters, since the function treats them all as plain characters.
Example 4: Pass a null value through
When a column can hold blanks, it helps to know what happens to a null.
let
Result = Text.Reverse(null)=null
in
Result
Result: true
Text.Reverse(null) returns null, so the comparison to null is true. The function never errors on a blank, the same way Text.StartsWith passes a null straight through.
Things to keep in mind with Text.Reverse
- It reverses characters, not words.
Text.Reverse("big red bus")gives"sub der gib", not"bus red big". For word order, split on a space, reverse the list, then join back. To reorder a list by value instead, reach for List.Sort. - The input must be text or null. A number throws
Expression.Error: We cannot convert the value 42 to type Text.Convert it first withText.From. - It does not trim or pad anything. Leading and trailing spaces are characters too, so they end up on the opposite side. Clean the value first with Text.Trim if stray spaces would land in the wrong place.
- It works on the character level, so it can split combined characters. An accented letter built from a base plus a combining mark can reverse into a mark sitting on the wrong character. Plain ASCII codes and words are unaffected.
Common questions about Text.Reverse
How do I reverse the order of words instead of the characters?
Split the text into a list, reverse the list, then recombine it: Text.Combine(List.Reverse(Text.Split([Column]," "))," "). Text.Reverse alone only flips characters.
Is Text.Reverse case-sensitive?
There is no comparison happening, so case is irrelevant. The function returns the exact same characters, in the same case, just in reverse order.
List of All Power Query Functions
Related Power Query Functions / Articles: