O R G A N I C / F E R T I L I Z E R: excel: my first use of power query (and i love it)

Jun 4, 2014

excel: my first use of power query (and i love it)

let’s face it. if you’re a techie and you don’t use excel, you are not peeking out your geeking out. :o) i use excel for a number of different things. it’s a really powerful program which can handle doing much more than figuring out how much i’ve spent on lunch over the last three months.

at teched, i got my first taste of power query during some of the hands-on-labs (available online for free now.) something came up recently that gave me a chance to explore it a bit more to see its value. let’s explore a scenario where your organization is absorbed or is absorbing another organization. after a domain migration, human resources decides they want to start over with new employee IDs.

 

SCENARIO

HR provides you with a file claiming it has all the information you need. (and clearly, you’ve no reason to doubt their claim.) upon examining you file, you notice that the only thing in the file is a column with the old employee ID and a column with the new employee ID.

drawing from your history, you know that employeeID is not an indexed attribute in active directory and would take quite a length of time to query each user by their old employeeID and write their new one. you’re also short on time to get any changes to the AD schema pushed through.

knowing you’ll need it, you create your own export of user ID and employee ID from active directory. game on.

 

THE VLOOKUP WAY

the way i would have approached this in the past (and did to be honest until my colleague encouraged me to use power query) is to create two sheets in excel. one sheet would contain the information from HR, and the other would contain the information from AD. from the HR sheet, you could run a vlookup formula in a new column, pulling the user IDs that match from AD.

 

THE POWER QUERY WAY

i’m about to demonstrate is magic, my friends. magic known as PFM… the purest kind available. i’m going to take one file that looks with just the old id and the new id and merge it with another that contains the old id and the user id ending up with a new dataset that contains the old id, the new id, and the user id. there’s also a little wrinkle here. the new id value they want starts with a leading zero. excel loves converting them which basically switches it to a number, removing the leading zero. no problem.

here’s a sample of how the files look:

SNAGHTML24c0fb47

  1. open excel (DUH) and start with a blank worksheet.
  2. if you haven’t downloaded power query, you will need to do that first.
  3. switch to the power query tab.
  4. select from file > from csv.
  5. open the first .csv file.

everything looks good except that leading 0 problem. hell, even the first row header was automatically applied. the leading 0 is a non-issue since it can be changed as an applied step!

  1. change the query name to emplIDs.
  2. click the new id column.
  3. right-click the new id column and choose change type > text.
  4. make sure the old id column stays as number. if not, change the type to number.
  5. under load settings, choose load to data model only.
  6. click apply & close.

image

you can’t see it in the worksheet, but have faith in magic that it’s all there (or use the peek by hovering your mouse over the query.)

image

let’s work on getting the merged results from both files into a worksheet now that we have a dataset with which we can combine.

  1. on the power query tab, choose from file > from csv.
  2. open the second file.
  3. name the query userIDs.
  4. choose merge queries from the ribbon bar.
  5. drop down the query selection and choose emplIDs.
  6. select the old id column in both tables and click OK.

a new column now shows up in the query with a peculiar little symbol image and values that state Table. we need to indicate what we want to pull back from the emplIDs data model.

  1. click the symbol of confusion.
  2. select the column new id and click OK.
  3. rename the new column to new id.
  4. click apply & close.
image

the result should be a beautifully merged set of data!

image