Forum Discussion

juan jimenez's avatar
juan jimenez
Iron Contributor
Apr 25, 2020
Solved

POWER QUERY - 365 COLUMNS INTO ONLY 1

Hi,   Would it be possible to consolidate different columns in only one so that the next column data appears below the previous day data?   Enclosed please find an example of a few days and I fee...
  • Zack Barresse's avatar
    Apr 25, 2020

    juan jimenez hello there,

    Yes, you can do this. From Excel, select any cell in your Table. Go to the 'Data' tab, click 'From Table/Range' in the 'Get & Transform Data' group. This will pop up the Power Query Editor (PQE). Select all of your columns (with the left-most column selected by default, hold the SHIFT key and click the right-most column). Select the 'Transform' tab and click 'Unpivot Columns'. That's it, you're done. Header values are in the first column, values are in the next. I would rename the headers to suit your needs.

     

    This is good for your example, and is a good way to understand how it works. However, you specified you had 365 column headers. If you look at your query, in the PQE you'll see how each columns name was hard-coded. This doesn't make it dynamic. It's like the macro recorder where it spits out everything exactly as you did it with zero interpolation. To make this dynamic, you need to edit a bit of the M code in the 'Advanced Editor' ('View' tab in PQE). The code below just needs your Table name (I used the one from your sample file) and it will unpivot all columns to the desired result.

     

     

    let
    Source = Excel.CurrentWorkbook(){[Name="Tabla1_2"]}[Content],
    Columns = Table.ColumnNames(Source),
    Unpivot = Table.Unpivot(Source, Columns, "Date", "Value")
    in
    Unpivot

     

     

    HTH

Resources