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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
smazo
New Member

Unpivot Columns without duplicating data in Power Query

Hello dear colleagues:

 

I'm running into a unique problem with my large payroll dataset that is affecting how other numbers are calculated. 

 

In short, I have a table that looks like this (this is an example of how my table looks):

smazo_1-1687380148898.png

 

When I unpivot the bonus categories in Power Query (so it can show up cleaner in my BI models) it duplicates data that messes up other models by summing up numbers that shouldn't be summed up. This is what the table looks like after unpivoting the bonus columns:

smazo_2-1687380156468.png

 

This inturn miscalcuates the hours work and the gross pay in other parts of my model for a single table. What would be a quick/easy/doable way to make the table in Power Query look something like this instead, so that column total for total hours worked and Gross pay don't get affected?

 

smazo_3-1687380165006.png

 

Thank you in advance for any solutions that might help! 

9 REPLIES 9
Manjit_smv
New Member

I am getting the same problem while unpivoting, have you found any solution. Please share 

Ahmedx
Super User
Super User

pls try this

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdFJDoMwDAXQu2RtVR5wQ5ctvQXK/a9Ru3RwcItiwSdIPOUrrGu52oBHgYJysouRxV6IBO0xebB6bneDHhEGNM8fcyDqiLjZgMe+WEX/SPRQdK3GRGx1yYJxM4sNeOx7nVmfR5V2iaLvpRgFvVbJ1JFd7jbgkX4k458jjqQrRpqJ4A8Um9H0RYRv5qvWHg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, PayDate = _t, #"Gross Pay" = _t, #"Hours Worked" = _t, #"General Bonus" = _t, #"Summer Bonus" = _t, #"Pickup Bonus" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"PayDate", type date}, {"Gross Pay", Int64.Type}, {"Hours Worked", Int64.Type}, {"General Bonus", Int64.Type}, {"Summer Bonus", Int64.Type}, {"Pickup Bonus", Int64.Type}}),
    #"Grouped Rows" = 
    Table.Group(#"Changed Type", {"Name", "PayDate"},
                               {{"Count", (x)=> Table.AddColumn(
                                                  Table.AddColumn( 
                                                     Table.AddIndexColumn(
                                                       Table.UnpivotOtherColumns(x, {"Name", "PayDate", "Gross Pay", "Hours Worked"}, "Attribute", "Value")
                                                    ,"Index", 1, 1, Int64.Type),
                                                  "_Gross Pay", each if [Index] > 1 then 0 else [Gross Pay]),
                                                  "_Hours Worked", each if [Index] > 1 then 0 else [Hours Worked])}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Attribute", "Value", "_Gross Pay", "_Hours Worked"}, {"Attribute", "Value", "_Gross Pay", "_Hours Worked"})
in
    #"Expanded Count"

Screen Capture #1297.png

Hi Ahmedx,

 

I cannot share the data table I am working with, but what I did was example. I am working with something like 43 colmuns, 4 date columns, 6 text columns, 33 number columns (of which 7 need to be unpivoted). Also your code didn't work.

Hi Ahmedx,

 

Could you please explain the steps you took in the code shown above? I'm having a hard time understanding what you did. I understand that you used Power Query M, but how would that look like in the actual Power Query window on Power BI?

Share sample pbix file to help you.

Ashish_Mathur
Super User
Super User

Hi,

Those measures will have to be edited to account for the transformed dataset.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish,

 

That's great idea. Could you please give an example of how I would do that? I'm having a hard time visualizing the implications of your solution.

Hi,

Share some data to work with and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
amitchandak
Super User
Super User

@smazo , Those all are measures you do need to break them, in case you need slicer, you can consider field parameters or calculations groups

 

Power BI Field Parameters — A Quick way for Dynamic Visuals: https://amitchandak.medium.com/power-bi-field-parameters-a-quick-way-for-dynamic-visuals-fc4095ae9af...
Power BI Field Parameters- Measure Slicer and Axis/Dimension slicer: https://youtu.be/lqF3Wa1FllE
Switch TOPN with Field Parameters: https://amitchandak.medium.com/switch-topn-with-field-parameters-299a0ae3725f

 

Calculation Groups- Measure Slicer, Measure Header Grouping, Measure to dimension conversion. Complex Table display : https://youtu.be/qMNv67P8Go0

 

If you want the split text in the name

Power BI- Text Part slicer to filter/search text - https://youtu.be/MKKWeOqFG4c
https://medium.com/@amitchandak/power-bi-search-text-parts-in-text-ad2f9783ffa2

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Top Solution Authors