0
votes

I'm new with Pivot Tables, Power Query, DAX formulas, etc and I want to know how to get the output desired with Pivot Tables, Power Query, etc or only would be possible with VBA macro.

I'm using Excel 2016.

Before I asked this question in other site and the only answer I received was a reference to this example but I after I tranformed the table to List the formula they give there Table.FromRows( List.Split( Table1[Column1], 3) ) simply doesnt work for me.

This is my input Table:

| DESCRIPTION | VALUE  |
|-------------|--------|
| STAGE       | 1      |
| ID          | 0      |
| NAME        | JFMSC  |
| TYPE        | MLRR   |
| DFRUL       | P1     |
| ADDR        | 1001   |
| RRUL        | P1     |
| SPRR        | TRUE   |
| ISGALW      | FALSE  |
| ISUTWD      | FALSE  |
| STAGE       | 1      |
| ID          | 2      |
| NAME        | PLLSJS |
| TYPE        | MLRR   |
| DFRUL       | P1     |
| STAGE       | 1      |
| ID          | 4      |
| NAME        | AAAARR |
| TYPE        | MLRR   |
| DFRUL       | R2     |
| ADDR        | 3553   |
| RRUL        | P1     |
| SPRR        | FALSE  |
| ISGALW      | FALSE  |
| ISUTWD      | FALSE  |
| ADDR        | 66444  |
| RRUL        | P1     |
| SPRR        | FALSE  |
| ISGALW      | FALSE  |
| ISUTWD      | FALSE  |
| ADDR        | 890087 |
| RRUL        | P1     |
| SPRR        | FALSE  |
| ISGALW      | FALSE  |
| ISUTWD      | FALSE  |
| STAGE       | 1      |
| ID          | 0      |
| NAME        | PPROOA |
| TYPE        | RRHN   |
| DFRUL       | P1     |
| ADDR        | 7034   |
| RRUL        | P1     |
| SPRR        | FALSE  |
| ISGALW      | FALSE  |
| ISUTWD      | FALSE  |

this is the output I'd like to obtain:

| STAGE | ID | NAME   | TYPE | DFRUL | ADDR   | RRUL |
|-------|----|--------|------|-------|--------|------|
| 1     | 0  | JFMSC  | MLRR | P1    | 1001   | P1   |
| 1     | 2  | PLLSJS | MLRR | P1    |        |      |
| 1     | 4  | AAAARR | MLRR | R2    | 3553   | P1   |
|       |    |        |      |       | 66444  | P1   |
|       |    |        |      |       | 890087 | P1   |
| 1     | 0  | PPROOA | RRHN | P1    | 7034   | P1   |

UPDATE

Input with spaces and some other uneeded text, but with same field to transform.

+----------------------------+---------+
| DESCRIPTION                |   VALUE | 
+----------------------------+---------+
|                            |         |
|                            |         |
| ..d  2019-03-07 17:35:52   |         |
| KLAPW                      | #075286 |
| STAGE=1, ID=0, TYPE=MLRR   |         |
|                            |         |
|                            |         |
| STAGE                      | 1       |
| ID                         | 0       |
| NAME                       | NAME1   |
| TYPE                       | MLRR    |
| DFRUL                      | P1      |
| ADDR                       | 8876    |
| RRUL                       | P1      |
| SPRR                       | TRUE    |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
|                            |         |
|                            |         |
| Total count                | 10      |
|                            |         |
| There is together 1 report |         |
|                            |         |
|    END_BLOCK               |         |
|                            |         |
|                            |         |
| ..d  2019-03-07 17:35:52   |         |
| KLAPW                      | #075287 |
| STAGE=1, ID=1, TYPE=MLRR   |         |
|                            |         |
|                            |         |
| STAGE                      | 1       |
| ID                         | 1       |
| NAME                       | NAME2   |
| TYPE                       | MLRR    |
| DFRUL                      | R2      |
| ADDR                       | 526766  |
| RRUL                       | P1      |
| SPRR                       | FALSE   |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
|                            |         |
|                            |         |
| Total count                | 10      |
|                            |         |
| There is together 1 report |         |
|                            |         |
|    END_BLOCK               |         |
|                            |         |
|                            |         |
| ..d   2019-03-07 17:35:52  |         |
| KLAPW                      | #075288 |
| STAGE=1, ID=2, TYPE=MLRR   |         |
|                            |         |
|                            |         |
| STAGE                      | 1       |
| ID                         | 2       |
| NAME                       | NAME3   |
| TYPE                       | MLRR    |
| DFRUL                      | P1      |
| ADDR                       | 232424  |
| RRUL                       | R2      |
| SPRR                       | FALSE   |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
|                            |         |
| ADDR                       | 13112   |
| RRUL                       | R2      |
| SPRR                       | FALSE   |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
|                            |         |
| ADDR                       | 131223  |
| RRUL                       | R2      |
| SPRR                       | FALSE   |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
|                            |         |
| ADDR                       | 111324  |
| RRUL                       | R2      |
| SPRR                       | FALSE   |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
|                            |         |
| ADDR                       | 56543   |
| RRUL                       | R2      |
| SPRR                       | FALSE   |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
|                            |         |
| ADDR                       | 11133   |
| RRUL                       | R2      |
| SPRR                       | FALSE   |
| ISGALW                     | FALSE   |
| ISUTWD                     | FALSE   |
+----------------------------+---------+
1
Unfortunately you really have a problem in that your initial data is missing a record ID... a third column to identify what rows a field belongs to once it's unpivoted. A person could try to rely on the order in the table, but you have these null values that will foul that up. Also, from a purist position, sets are inherently unordered and you should never rely that the order of records is in any way constant unless enforced by a sort on the available data. - Ryan B.
Then Pivot Table, Power Query wouldn't be the tools for this task? - Ger Cas
If you go in and add a third column that can be used to group the fields according to rows, then Power Query can easily do what you want. Otherwise, no. - Ryan B.
I understand from you, have a 3rd column with the fields that I want to use as headers in output. Is like that? May you show me how to that in Power Query if I have that 3rd column please? - Ger Cas
@Olly has shown you a programmatic way to add the '3rd column'. If you step through his solution, you see how the 'Record Number' is required for a pivot to work. - Ryan B.

1 Answers

2
votes

If we can assume that your source data is always sorted in field order, even if not all fields are populated for each record, then we can extract the data you want:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
    #"Added Field Index" = Table.AddColumn(#"Added Index", "Field Index", each List.PositionOf({"STAGE","ID","NAME","TYPE","DFRUL","ADDR","RRUL","SPRR","ISGALW","ISUTWD"},[Decription]), Int64.Type),
    #"Added Previous Field Index" = Table.AddColumn(#"Added Field Index", "Previous Field Index", each try #"Added Field Index"[Field Index]{[Index]-1} otherwise null, Int64.Type),
    #"Added Record Index" = Table.AddColumn(#"Added Previous Field Index", "Record Index", each if [Field Index] < [Previous Field Index] or [Previous Field Index] = null then [Index] else null, Int64.Type),
    #"Filled Down Record Index" = Table.FillDown(#"Added Record Index",{"Record Index"}),
    #"Record Numbers" = Table.AddIndexColumn(Table.Group(#"Filled Down Record Index", {"Record Index"}, {}), "Record", 1, 1),
    #"Merged Record Numbers" = Table.NestedJoin(#"Filled Down Record Index",{"Record Index"},#"Record Numbers",{"Record Index"},"Filled Down",JoinKind.LeftOuter),
    #"Expanded Record Numbers" = Table.ExpandTableColumn(#"Merged Record Numbers", "Filled Down", {"Record"}, {"Record"}),
    #"Selected Columns" = Table.SelectColumns(#"Expanded Record Numbers",{"Decription", "Value", "Record"}),
    #"Pivoted Column" = Table.Pivot(#"Selected Columns", List.Distinct(#"Selected Columns"[Decription]), "Decription", "Value"),
    #"Removed Other Columns" = Table.SelectColumns(#"Pivoted Column",{"STAGE", "ID", "NAME", "TYPE", "DFRUL", "ADDR", "RRUL"})
in
    #"Removed Other Columns"

Example file: https://excel.solutions/so_55152879/