Concept Updated February 6, 2024

Restructuring Data in Excel using Power Query

Info:

Learn to transform your Excel document to fit the guidelines for data used in Valsight with Power Query!

Overview

Data sources uploaded to Valsight need to be correctly formatted as shown in the picture below.

image-20230905-091921.png

If you have an Excel document that is not following those guidelines (e.g. the document in the picture below), there is an easy way of changing it into the correct format using Excel Power Query.

PowerQuerySoldUnits.PNG

Create an empty Excel document and select ‘Data’ → ‘Get Data’  → ‘From File’ → ‘Excel Worksheet’. Now select the file that contains the desired data.

image-20220331-132131.png

Excel automatically imports the data. Now select ‘Clean Data’ to change the data structure in the Power Query-Editor.

image-20220331-132205.png

Changing the Data Structure

In this case, select ‘Use First Row As Headers’ to properly title the columns. In general, use the suitable tool for your need offered by the Power Query-Editor (e.g. “Remove Rows/Columns”, “Split Column”, …) to transform your table.

image-20220331-132248.png

The next step is to select all the columns containing data. In this case this means selecting the columns ‘2017’, ‘2018’, ‘2019’ and ‘2020’.  Afterward, click ‘Transform’ → ‘Unpivot Columns’.

image-20220331-132309.png

Now the only thing left to do is rename the columns by double clicking on their title and to ‘close & load’.

image-20220331-132334.png

Congratulations, you have successfully restructured your data. The resulting worksheet is now linked to the original document.

image-20220331-132405.png

Import Data via Direct Filepath

Power Query offers the opportunity to connect the desired file path directly, to receive the matching data.

The first task is to create a table with one column and one row. You have to name the column (e.g. “Product”) and the table (e.g. “Tabl”) to reference them later.

Now you write down the file path, which leads to the desired, data, to the only cell.

image-20220331-132509.png

Afterward, you open the Power Query-Editor containing the advanced Editor, as seen below.

Select your table and go ‘Data’ → ‘From Table/Range’ → ‘Home’ → ‘Advanced Editor’.

There you type down the following command. Be careful to reference your table and column name correctly, furthermore the sheet reference ({[Item=“Tabelle1”,..) of the desired data.

let

    my_source= Excel.Workbook(File.Contents(Text.From(List.First(Table.Column(Excel.CurrentWorkbook(){[Name="Tabl"]}[Content], "Product")) )), null, true),
    Sheet1_Sheet = my_source{[Item="Tabelle1",Kind="Sheet"]}[Data]
in
    Sheet1_Sheet

image-20220331-132542.png

If you referenced everything correctly, the preview of the data becomes visible.

Just do “Close & Load” to finish the process.

image-20220331-132607.png

It’s done, you created a new sheet that contains the data.

A change or expansion of the referenced data will be adapted automatically.

image-20220331-132632.png

Mapping

Let’s continue with the given data table. To expand or substitute the given data you can use the “Join” Functions of Power Query.

You can add the additional data in different ways, usually by copying/pasting or manually.

image-20220331-132725.png

Afterward, you convert the data into a second table by selecting the desired data and selecting ‘From Table/Range’.

image-20220331-132749.png

Now you close it and import it as a connection only.

image-20220331-132813.pngimage-20220331-132832.png

By selecting ‘Data’ → ‘Get Data’  → ‘Combine Query’ → ‘Merge’ you reach the Merge Window.

image-20220331-132853.png

There you select for each field one of the given data tables and mark the matching columns.

image-20220331-132915.png

You can process with the resulting Power Query depending on your needs. To remove the “Productname” information you only need to delete the column.

For more options push the shown icon and choose the information you want to keep.

image-20220331-132939.png

Now, select ‘Close & Load’ and your new data table is ready.

image-20220331-132955.png

Transforming a Date to a Valsight Month

= Table.AddColumn(#"Geänderter Typ1", "TEst", each Text.Combine(
  {
    Number.ToText(Date.Year([Month])),
    "-",
    Number.ToText(Date.Month([Month]),"00")
  }))
Was this page helpful?