Transpose? Pivot? Unpivot? Which? How?

Hello

I have a wide fact table, which is likely to get wider in future, and that I would like to transform into a much much less wide fact table.
The original wide one looks like so:

At the moment I am “cheating” by creating duplicate copies of the table (within Query editor view) to edit and append to achieve the outcome that I would like, which looks like so:

This approach of creating temp tables won’t be sustainable since the columns shown in the original raw data are likely to grow and it does not make sense to keep doing things the way that I am currently doing. There must be a quicker way.
I currently can’t figure out which some combination of transpose/pivot/unpivot to use to achieve the goal within one table (if that is possible).

I hope someone can assist with this.
Power BI file attached.
Power query - structure challenge.pbix (31.1 KB)

Thanks for your time.

@Mo.jo.jo,

This is definitely doable. However, when posting a PQ-related question, please be sure to also post the Excel data file, since PQ wili throw this type of error without it. Thanks!

  • Brian

@BrianJ

Hello again Brian :slight_smile:

Ok. Noted.

Please find Excel file attached.

Power query - Test data.xlsx (12.4 KB)

Hi @Mo.jo.jo

Try this

Power query - structure challenge.pbix (38.1 KB)

3 Likes

Hi @Mo.jo.jo

@Rajesh has already replied very fast but still i post below code:

let
    Source = Excel.Workbook(File.Contents("C:\Users\vgamidi\Downloads\Power query - Test data.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Table1_Table, {"Business Unit"}, "Attribute", "Goals"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Added Index" = Table.AddIndexColumn(#"Split Column by Delimiter", "Index", 0, 1, Int64.Type),
    #"Added Entries" = Table.AddColumn(#"Added Index", "No of Entries", each #"Added Index"{[Index]+1}[Goals]),
    #"Added Date" = Table.AddColumn(#"Added Entries", "Date", each #"Added Index"{[Index]+2}[Goals]),
    #"Filtered Rows" = Table.SelectRows(#"Added Date", each ([Attribute.2] = null)),
    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Business Unit", "Goals", "No of Entries", "Date"})
in
    #"Removed Other Columns"
2 Likes

Absolutely love it !!
Just shows how many different solutions are possible in Power Query.

@Rajesh and @MK3010 :+1:

Seems I started late… since I also have another approach I’m just going to share it as well.
I’ve created a fxTransformTable function that takes a single table as argument.

(myTable as table) =>
let
    TableToColumns = Table.ToColumns(myTable),
    ConvertToTable = Table.FromList(TableToColumns, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    RemoveFirstRows = Table.Skip(ConvertToTable,1),
    AddedIndex = Table.AddIndexColumn(RemoveFirstRows, "Index", 0, 1, Int64.Type),
    UpdatedIndex = Table.AddColumn(AddedIndex, "UpdatedIndex", each if Number.Mod([Index], 3)= 0 then [Index] else null),
    #"Filled Down" = Table.FillDown(UpdatedIndex,{"UpdatedIndex"}),
    AddBusinessUnitList = Table.Group(#"Filled Down", {"UpdatedIndex"}, {{"Count", each {{myTable[Business Unit]}, _[Column1]}, type list}}),
    ExpandL1 = Table.ExpandListColumn(AddBusinessUnitList, "Count"),
    ExpandL2 = Table.ExpandListColumn(ExpandL1, "Count"),
    GroupRows = Table.Group(ExpandL2, {"UpdatedIndex"}, {{"Table", each Table.FromColumns(_[Count]), type table}})
in
    GroupRows

.
When Invoked it creates a series of nested tables.
image

And when combined with those sideward arrows in the Table column
It doesn’t matter how many columns sets will be added in future, this solution is dynamic.

Here’s your sample file eDNA - Structure challenge.pbix (35.7 KB) .
I hope this is helpful

3 Likes

@Rajesh

Hello again :slight_smile:

Thank you once again for another solution to a problem.
Still not sure how it works but will work through trying to get it.
Many many thanks.

@MK3010 & @Melissa
Thank you both a lot for your solutions and assistance.
Very helpful to see the different approaches used.

@Melissa I was going to complain about how difficult I thought Rajesh’s solution was but then then you did the function thing and then I deleted that comment I was typing.ha ha.

@Rajesh @MK3010 @Melissa
Can any of you suggest any resources to better understand the thinking behind understanding these Pivot, Unpivot situations and how to learn going about them? I don’t get how to even start thinking about how to go about them other than just trial, error and unsuccessful guess work.

1 Like

@Melissa - well, if that’s not a video request, I don’t know what is… :laughing: This would be a great example to add to your ongoing series.

@Rajesh, @MK3010 and @Melissa - well done, all!

  • Brian
2 Likes

@BrianJ Great shout
Gets my vote :smiley:

Did not know about the ongoing series.
Though there’s quite a bit of good learning trawling through EDNA videos, are there any links to easily find these?

1 Like

Hi @Mo.jo.jo

One way to think of is if you have repeated structured column then you try to use this method— you have dates in columns headers.

2 Likes

You can find those on the eDNA YouTube Channel
But they have also been added to the Portal you can find them in the Power Query series module

It’s an ongoing project so new video’s in the “Unpivot Series” will continue to be added over time.

3 Likes

@MK3010
Ok. So it is kind of a pattern to use where your columns follow a set pattern.
Here’s hoping I don’t face a situation where a random naughty column gets added into the mix.ha ha.

@Melissa
Thanks for the link. I missed that within the portal.
Brilliant and very necessary addition. There goes a bit of Saturday :slight_smile:

Thank you all and wishing you a very good weekend.

1 Like

I have not read all the answers in detail, but any of these two solutions may be different in some respects from those already presented Power query - structure challenge.pbix (36.0 KB)

Edoited and corrected.
Power query - structure challenge1.pbix (35.7 KB)

1 Like

other proposed solution

Power query - structure challenge2.pbix (42.5 KB)

Does this mean that it is not considered useful or is it not even allowed to intervene on old posts to propose possibly different points of view?
if so, ask where / how could I propose other ideas on the same topic?

Hi @sprmnt21.

One of the main goals of this forum is to enable the best learning experience possible for everyone. There are many who only look at unsolved posts, and many who look for all possible supporting materials before addressing a post, so it’s recommended that when you have a follow-on issue on a solved thread, you create a new thread (and reference the old thread, if helpful) and upload all of the available information (PBIX, dataset, desired outcome mockup, etc.) to maximize both the visibility of your issue and the ability of forum members to offer comments/solutions.
Greg

1 Like

Following the discussion of this interesting problem, I would like to make some proposals that are partly different from those previously provide

Split_and_Combine.pbix (46.4 KB)

1 Like