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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Dynamic column names needed in a dynamic sum equation

Hi guys,

 

I need to pivot a column full of payslip IDs with values that is their pay amount for the fortnight. The payslip IDs are incremental so I am able to extract only the latest 6 payslips and then perform the pivot. But I need to sum the payslips which now are columns with dynamically changing names the next time it is run. How do i make it so that the payslip numbers can update to the latest 5 payslips? 

 

Power Query > Add Custom field:

Sum of =

[Payslip 1] + [Payslip 2] + [Payslip 3] + [Payslip 4] + [Payslip 5] + [Payslip 6] = XYZ

 

For example these columns below 453, 452, 451, 450, 449 and 448:

 

dynamic columns.jpg

 

I need to create a new Custom Column to add the values in Columns 453, 452, 451, 450, 449 and 448 but how do I make the column names dynamic instead of 453, 452, 451, 450, 449, 448 as these payslip IDs will change incrementally as I extract only the latest 6 payslips....

dynamic columns in sum equation.jpg

 

 

 

 

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @Anonymous 

 

Not sure if you really need to unpivot those columns, keep them then sum up, but yes, there are ways to do in M. I created dummy data to show 2 ways in the file. You need to amend the logic accordingly for finding the latest 6 columns, and remove values not in these columns - last6 list and the list.sum

Vera_33_0-1637801853799.png

 

View solution in original post

3 REPLIES 3
Vera_33
Resident Rockstar
Resident Rockstar

Hi @Anonymous 

 

Not sure if you really need to unpivot those columns, keep them then sum up, but yes, there are ways to do in M. I created dummy data to show 2 ways in the file. You need to amend the logic accordingly for finding the latest 6 columns, and remove values not in these columns - last6 list and the list.sum

Vera_33_0-1637801853799.png

 

Anonymous
Not applicable

Thank you, that was exactly what I wanted. 

AlexisOlson
Super User
Super User

I'd strongly recommend against trying to do it this way. Life will be much easier keeping things unpivoted and doing any summing with DAX measures rather than Power Query custom columns.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.