Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
deanbland
Helper III
Helper III

Unpivot a column based on its location, not name

Hi, 

 

I am trying to unpivot columns based on their position as the column headers are dynamic dates that change each month. 

 

How would I go about this?

 

Thank you!

8 REPLIES 8
AnalyticsWizard
Solution Supplier
Solution Supplier

@deanbland 

 

Unpivoting columns when headers are dynamic (like dates) can be a bit tricky, but Power Query in Power BI has the flexibility to handle such scenarios. Here’s a general approach you can take to unpivot columns based on their position:

 

1. Load the data into Power Query Editor.

 

2. Identify how many columns are static and should not be unpivoted (like IDs, names, etc.).

 

3. After identifying the static columns, go to the 'Advanced Editor' and find the step where the columns are first all selected (usually named `Changed Type` or similar).

 

4. You’ll need to modify the M code to dynamically select columns based on their position. Here’s a pattern you can follow:

 

let
Source = ...,
// Other steps...
UnpivotColumns = Table.UnpivotOtherColumns(Source, {"StaticColumn1", "StaticColumn2"}, "Attribute", "Value")
in
UnpivotColumns

 

Replace `"StaticColumn1", "StaticColumn2"` with the actual names of the static columns you want to keep.

 

5.  Use the `Table.UnpivotOtherColumns` function to unpivot all columns except the ones you specified. This will automatically include all the dynamically named date columns as the ones to unpivot.

 

6. Once you've edited the M code, click 'Done' in the Advanced Editor, then 'Close & Apply' in the Power Query Editor to apply these changes to your model.

 

This method allows you to unpivot columns without having to specify each column by name, which is perfect when dealing with dynamic headers like dates. The key is in step 4, where you only list the static column names, and Power Query takes care of the rest.

 

Remember, every time the column headers change, the query will still work as it’s not looking for specific column names but rather their relative position after the static columns.

 

If you find this approach helpful, please consider marking it as the solution and any kudos are always appreciated!

mathur786
New Member

mathur786_1-1713639014962.png

 

mathur786_0-1713638944797.png

 

 

mathur786_0-1713638748099.png

Unpivot other columns except first 2 columns without knowing the headings of first 2 columns

 

deanbland
Helper III
Helper III

Hi @v-shex-msft , 

 

The basis of what you have done is correct, I just meant to unpivot them, but within the M query, not to use the column headers. 

 

The column headers change each month and so this would caue problems and so I would need to unpivot the columns without using the column header. 

 

Hope this makes a bit more sense? 

 

Regards, 

 

Dean

HI @deanbland,

Yes, it is possible, you may need to check the steps of getting and recognize data on your query table to remove the 'change data type' and 'promote header' operations.
Then you can refer to my sample formulas steps as I share to convert your records to date category with position attribute and correspond values.
Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
v-shex-msft
Community Support
Community Support

Hi @deanbland,

Can you please share some dummy data with a similar data structure and expected results? It should help us clarify your scenario and test to coding formula.

How to Get Your Question Answered Quickly  

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
amitchandak
Super User
Super User

@deanbland ,Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

Hi @v-shex-msft & @amitchandak 

 

The dataset looks like this: 

 

01/01/202201/02/202201/03/202201/04/202201/05/202201/06/202201/07/202101/08/202101/09/202101/10/2021
000000111.20.36
1000.9900.590.6311.580.25
0000.3200.660.4810.320.99
000010.941.2310.081.35

As ststed above, I want to unpivot these columns by referencing their position within the table. 

 

Thank you! 

 

Dean

HI @deanbland,

Did you mean to transform your records to group them based on date and unpivot based on different positions?

1.png

If that is the case, you can take a look at the following formulas: (I transpose these records to vertical and unpivot on position fields)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY7ZDQAhCER78duwAspqLcb+21jFIyRrIpE3DEetLuDTHwUi5xXIAluIFpIFsfAOwAXZQjGAYULz/YYhXQJ3gM4GFrWjtUEpJ02agvDpS1kVSv89wHRSEf1iXo27OIbfDpyeEudtfJSgA4D7uvYB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t]),
    #"Transposed Table" = Table.Transpose(Source),
    #"Renamed Columns" = Table.RenameColumns(#"Transposed Table",{{"Column1", "Date"}, {"Column2", "P1"}, {"Column3", "P2"}, {"Column4", "P3"}, {"Column5", "P4"}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Renamed Columns", {"Date"}, "Attribute", "Value")
in
    #"Unpivoted Columns"

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.