Generate a Random Alphanumeric String in Power Query
Update 2016/06/01: Bill Szysz has shared a solution in the comments below that is shorter and easier to understand than mine. He’s also shared a couple of other alternatives that seek to improve the randomness of the results. This post describes how to generate a random alphanumeric string in Power Query. This is likely not a common requirement for most Power Query users, but I saw this requirement in the Twitter API and thought it would be a fun challenge in M. Here’s the complete query. Later I’ll explain how it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* Generates a single, random alphanumeric string. The string length can be modified by changing the StringLength variable. Author - Chris Koester https://chris.koester.io/ */ let StringLength = 32, ValidCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456879", fnRandomCharacter = (text) => Text.Range(ValidCharacters,Int32.From(Number.RandomBetween(0, Text.Length(ValidCharacters)-1)),1), GenerateList = List.Generate(()=> [Counter=0, Character=fnRandomCharacter(ValidCharacters)], each [Counter] < StringLength, each [Counter=[Counter]+1, Character=fnRandomCharacter(ValidCharacters)], each [Character]), RandomString = List.Accumulate(GenerateList, "", (a,b) => a & b) in RandomString |
The result of this query is a value like “kZSRd55cC67QyFWbjTXKlhnwiCttaZxU”. Every time the query is run,Read More →