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:
- open excel (DUH) and start with a blank worksheet.
- if you haven’t downloaded power query, you will need to do that first.
- switch to the power query tab.
- select from file > from csv.
- 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!
- change the query name to emplIDs.
- click the new id column.
- right-click the new id column and choose change type > text.
- make sure the old id column stays as number. if not, change the type to number.
- under load settings, choose load to data model only.
- click apply & close.
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.)
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.
- on the power query tab, choose from file > from csv.
- open the second file.
- name the query userIDs.
- choose merge queries from the ribbon bar.
- drop down the query selection and choose emplIDs.
- 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 and values that state Table. we need to indicate what we want to pull back from the emplIDs data model.
- click the symbol of confusion.
- select the column new id and click OK.
- rename the new column to new id.
- click apply & close.
the result should be a beautifully merged set of data!
Comments
Post a Comment