# Convert “Column1” to “A”

I admit that the technique I will share with you in this post may not have many use-cases, but it was a crucial component for the Excel Compare Power BI app that we released on AppSource earlier this week, and you can consider reading this post as a technical challenge to evaluate your Power Query and M skills (or the skills of your colleagues and team).

## Background

As part of our focus in delivering low-code data quality automation using Power Query here, and here, we have released a new auditing app that allows you to compare between two Excel files and track changes and errors.

One of the core features of the app is that it compares the values and errors in cells across two matching worksheets and returns “A –> B” if the value had changed from A to B. Or “A –> #error” if a new error was detected in the cell in the second worksheet. To make the solution user-friendly, we wanted to use the column and row “coordinates” to direct the users to the changes and errors. And here is the problem — When you import an Excel worksheet into Power BI, the column names are represented as “Column1”, “Column2”, …, “Column34” etc, and not as “A”, “B” or “AH”.

We didn’t want to tell users – “look at Column34, Row 1 to review the change we have found in the app”. This information will leave the client with the need to start counting to 34 from Column A to find the right column with the change. Instead, we preferred a user experience that will guide the user to “look at Column AH, Row 1 to review the change”.

Before we share our solution, could you challenge yourself and try it on your own?

Write a Power Query function that receives a number and returns its corresponding Excel alphabetical column name. Here are few examples?

For column “Column1”, the function will receive 1 and return A.

For column “Column2”, the function will receive 2 and return B.

For column “Column34”, the function will receive 34 and return AH.

## The Solution

If you are really stuck, here is a hint…

HINT 1: Solving only A to Z is easier, but for very wide tables with 1000 columns how would you solve it?

HINT 2: Will a recursion work for you? Take a look at this book excerpt on M recursions.

HINT 3: There are 26 letters in English and you can use Number.IntegerDivide and Number.Mod to generate a single English character in each iteration of the recursion.

The Recursion

``````let
GetLetterFromNumber =
(ColumnNum as number) as text =>
let
Mod = Number.Mod(ColumnNum - 1, 26),

PrefixLetter =
if Mod + 1 = ColumnNum then
""
else
@GetLetterFromNumber(Number.IntegerDivide(ColumnNum - 1, 26)),

Result = PrefixLetter & Character.FromNumber(Mod + 65)
in
Result

in
GetLetterFromNumber``````

The List.Generate Solution

Now, when you have the function working, you can move to a more advanced option to solve this challenge using a loop. Here is a great solution that was shared in the comments below by Stéphane. Using loops instead of recursions is typically better when you work with large data since recursions will soon consume the call stack of your computer. But practically speaking – For our specific use-case, you will not notice any gain as the largest column is XFD, and recursion of 3 levels is not a big deal…

``````(ColumnNum as number) as text =>
Text.Combine(
List.Reverse(
List.Transform(
List.Generate(
() => ColumnNum,
each _ >= 1,
each (_ - 1) / 26
),
each Character.FromNumber(
Number.Mod(
Number.RoundDown(_) - 1,
26
) + 65
)
)
)
)``````

Please try the app, and consider writing a review here.

CategoriesUncategorized
Previous article

Next article

## Handling Excel Formula Errors in Power Query

1. My solution with List.Generate

(ColumnNum as number) as text =>
Text.Combine(
List.Reverse(
List.Transform(
List.Generate(
()=>ColumnNum,
each _>=1,
each (_-1)/26),
each
Character.FromNumber(
Number.Mod(
Number.RoundDown(_)-1,
26
)
+65
)
)
)
)

2. I dont think you need recursion

(colnum as number)=>
let
ModNum = Number.Mod(colnum-1,26)+1,
QuotientNum = Number.IntegerDivide(colnum-1,26),
AscNumMod = ModNum+64,
AscNumQuot = QuotientNum,
AscMod = Character.FromNumber(AscNumMod),
AscQuot = if AscNumQuot>=1 then Character.FromNumber(AscNumQuot+64) else “”,
ColName = AscQuot & AscMod
in
ColName

•
{"ft_lang_label":"__label__en","ft_lang_prob":0.8661762,"math_prob":0.9054963,"size":4343,"snap":"2022-40-2023-06","text_gpt3_token_len":1066,"char_repetition_ratio":0.11569486,"word_repetition_ratio":0.021857923,"special_character_ratio":0.2472945,"punctuation_ratio":0.119668245,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.964938,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-06T21:56:04Z\",\"WARC-Record-ID\":\"<urn:uuid:942fa0ae-48f7-45c0-b252-b99ae7467284>\",\"Content-Length\":\"100570\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d4af98ea-8ccb-4694-a8f9-c7c074becebb>\",\"WARC-Concurrent-To\":\"<urn:uuid:229c80a4-dc8d-4237-922f-68194721ebc6>\",\"WARC-IP-Address\":\"35.208.115.184\",\"WARC-Target-URI\":\"https://datachant.com/2021/08/04/convert-column1-to-a/\",\"WARC-Payload-Digest\":\"sha1:V5BIOBNML6NEH6TSLI5ITJSIKBALSS43\",\"WARC-Block-Digest\":\"sha1:P7ZGFS63YC73CHQBOYM3HFXMXPNDJKT2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500365.52_warc_CC-MAIN-20230206212647-20230207002647-00418.warc.gz\"}"}