Creating dynamic lookup-tables with unique values using Power Query instead of a database

 

Guest post by Lars Schreiber

Hello PowerPivot Community,

I’m quite sure most of you know a scenario comparable to the one in the following figure:

 

Bringing two fact-tables with different date-columns together using a 'unique-months'-dimension

Two scenarios (in two tables) – in this case actuals and budget – have to be put together to do some math on them. The only problem you have is the different level of planning regarding the time dimension. While the actual figures are on daily basis, the budget was planned on monthly basis. As you could learn on this website many times before (e.g. here) you need another lookup-table with a unique list of months to bring both tables (actual & budget) together. And this is where Power Query can help you a lot.

 


Two ways to get your list of unique values without using Power Query

  1. Take a hardcopy of this unique-list into an excel-sheet and link this list into your PowerPivot-model
  2. For those of you, who are familiar with SQL and databases, use a database to import the table ‘Dim_Time’ and put a view/ query on it, using ‘SELECT DISTINCT’- or ‘GROUP BY’-statements and connect your PowerPivot-model to that view/ query to get the table ‘Dim_UniqueMonths’.

Version 1 needs no database-professional and no database-license, but the list of unique month-values isn’t dynamic. A change in your input data has no effect on the PowerPivot-model. This is why Version 2 should be preferred. Putting a query on dynamically changing data secures that you always have the correct list of unique values. BUT you need a database and someone (if not you) who knows to deal with it.

Get your dynamic list of unique values with Power Query

Power Query offers a third version to get your unique list of lookup-data, which combines the positive effects of version 1 and 2:

After you installed Power Query on your Computer (download available here) you can import data from a huge amount of different data sources.

 

Importing csv-files with Power Query

Next import the csv-file ‘Dim_Time’, which has all the required months, but not unique as you need it.

Power Query automatically splits up the csv-file into different columns. For your PowerPivot-model you want to have a unique list of months, so you have to delete all the other columns from the imported csv-file, before you can start to remove the duplicates. Therefore just mark the month-column (the only one you need), do a right click into the header and choose “Remove Others”.

 

Removing all unimportant columns from the import file with Power Query

 

Now remove the duplicates and give the query a meaningful name (e.g. ‘PQ_UniqueMonths’). To rename the query just change the name under ‘properties – name’.

Remove duplicates with Power Query

Now you’ve got everything you wanted. Before you save the query, go under properties and deselect ‘load to workbook’, because you don’t need the data within the Excel sheet. You want it directly in your PowerPivot-model.

Back in your PowerPivot-window go to ‘Existing Connections’ and choose your defined Power Query-query ‘PQ_UniqueMonths’. Then press ‘Open’ and PowerPivot will import these data into a new sheet within your PowerPivot-model. Give this sheet a meaningsful name (e.g. ‘Dim_UniqueMonths’) and link it to the other tables as shown in the first figure of this post.

 

Using Power Query as dynamical data source for your PowerPivot-model

From now on you simply have to refresh your PowerPivot-model and the unique list of months is always up-to-date.

Power-BI rocks. Smiley

One Response to Creating dynamic lookup-tables with unique values using Power Query instead of a database

  1. Jester says:

    That’s awesome :) I like that there is no M code in the whole article.

    I can’t but think how great it would be if Power Query was able to query SSAS (MD and Tabular), the current worksheet model and other worksheets models (preferably without Sharepoint needed).

Leave a Comment or Question