Skip to content

Instantly share code, notes, and snippets.

@grenzi
Last active December 8, 2023 16:06
Show Gist options
  • Save grenzi/78eee1863130c29c6689ed27cb0de4bb to your computer and use it in GitHub Desktop.
Save grenzi/78eee1863130c29c6689ed27cb0de4bb to your computer and use it in GitHub Desktop.
Powerquery M function for Salesforce Id conversion to case insensitive (15 to 18 character)
(sf15Id as any) => let
Source = sf15Id,
#"tab" = #table(1, {{Source}}),
#"cb1" = Table.AddColumn(#"tab", "cb1", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],0,1)) then 1 else 0),
#"cb2" = Table.AddColumn(#"cb1", "cb2", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],1,1)) then 2 else 0),
#"cb3" = Table.AddColumn(#"cb2", "cb3", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],2,1)) then 4 else 0),
#"cb4" = Table.AddColumn(#"cb3", "cb4", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],3,1)) then 8 else 0),
#"cb5" = Table.AddColumn(#"cb4", "cb5", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],4,1)) then 16 else 0),
#"char16" = Table.AddColumn(cb5, "char16", each Text.Middle("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", [cb1]+[cb2]+[cb3]+[cb4]+[cb5], 1)),
#"cb6" = Table.AddColumn(#"char16", "cb6", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],5,1)) then 1 else 0),
#"cb7" = Table.AddColumn(#"cb6", "cb7", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],6,1)) then 2 else 0),
#"cb8" = Table.AddColumn(#"cb7", "cb8", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],7,1)) then 4 else 0),
#"cb9" = Table.AddColumn(#"cb8", "cb9", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],8,1)) then 8 else 0),
#"cb10" = Table.AddColumn(#"cb9", "cb10", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],9,1)) then 16 else 0),
#"char17" = Table.AddColumn(cb10, "char17", each Text.Middle("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", [cb6]+[cb7]+[cb8]+[cb9]+[cb10], 1)),
#"cb11" = Table.AddColumn(#"char17", "cb11", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],10,1)) then 1 else 0),
#"cb12" = Table.AddColumn(#"cb11", "cb12", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],11,1)) then 2 else 0),
#"cb13" = Table.AddColumn(#"cb12", "cb13", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],12,1)) then 4 else 0),
#"cb14" = Table.AddColumn(#"cb13", "cb14", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],13,1)) then 8 else 0),
#"cb15" = Table.AddColumn(#"cb14", "cb15", each if Text.Contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Text.Middle([Column1],14,1)) then 16 else 0),
#"char18" = Table.AddColumn(cb15, "char18", each Text.Middle("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", [cb11]+[cb12]+[cb13]+[cb14]+[cb15], 1)),
#"Added Custom" = Table.AddColumn(char18, "sf18id", each [Column1]&[char16]&[char17]&[char18]),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"sf18id"})
in
#"Removed Other Columns"
@cbartetzko
Copy link

This seems to be, what I've been looking for.
Since I'm a newcomer to Power BI and especially to Powerquery M language I do need some assistance to incorporate that into my existing Power BI query.
What do I have:
Imported Table with 15-digit-salesforce-IDs in first column.
What do I need:
Replace all 15-digit-salesforce-IDs in first column with calculated 18-digit-salesforce-IDs.

Here is my current Code generated by Power BI:
let Quelle = Excel.Workbook(File.Contents("C:\UserData\adbacl3\OneDrive - Siemens AG\Documents\Scorecard-Test\Opptest.xlsx"), null, true), Tabelle1_Sheet = Quelle{[Item="Tabelle1",Kind="Sheet"]}[Data], #"Geänderter Typ" = Table.TransformColumnTypes(Tabelle1_Sheet,{{"Column1", type text}, {"Column2", type text}}), #"Höher gestufte Header" = Table.PromoteHeaders(#"Geänderter Typ", [PromoteAllScalars=true]), #"Geänderter Typ1" = Table.TransformColumnTypes(#"Höher gestufte Header",{{"OppID", type text}, {"OppName", type text}}) in #"Geänderter Typ1"

@grenzi
Copy link
Author

grenzi commented Apr 6, 2022

@cbartetzko - here's what you do -

  1. Go to Power Query Editor
  2. Click Home Tab, New Source -> Blank Query
  3. Click Home Tab, Advanced Editor
  4. Paste the script above then click Done
  5. In the Queries list on the left of the screen Rename the "Query1" you just created to whatever you want to call the function
  6. On your original query, at the appropriate step, select Invoke Custom Cunction from the "Add Column" tab
  7. Select your function from the drop down, select the 15 character column as the parameter, click ok.
  8. Expand result table on new column (click arrows in column header) to see 18 character ID

@cbartetzko
Copy link

Thank you so much.
Works like a charme !

@Grembus
Copy link

Grembus commented Aug 2, 2022

I never usually comment on things like this but the 15 & 18 character Id's have been driving me nuts for ages. So glad I found this solution.
THANK YOU!

@7mmpioneer
Copy link

This was a huuuge help. Thank you for posting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment