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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Syndicate_Admin
Administrator
Administrator

YTD sum of columns in Power Query based on input from Excel

I hope I have the correct forum.

Within Power Query I am trying dynamically add columns based on an input from Excel.

 

The input would be something like this from a cell within Excel (I point a query to this cell)

JeremyHillier_1-1642017438455.png

The above could be selected as Jan, Feb, Mar etc to Dec.

 

The data has this structure:

 JanFebetc.NovDecYTD 

Sales

100100 

100

100200 
Expenses5050 5050100 
        

 

I would like to sum the columns Jan to Feb (in this example) into the YTD column dynamically based on the earlier input.

 

Is this possible?

The formula is easy in Excel but I'm struggling within Power Query.

What is the M formula?

 

Thanks in advance for any help

 

 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Syndicate_Admin @JeremyHillier ,

 

The basic structure of the YTD column calculation would be something like this:

if excelQuery[selectedMonth] = "JAN" then List.Sum({[Jan]})
else if excelQuery[selectedMonth] = "FEB" then List.Sum({[Jan], [Feb]})
else if...
...
else //your escape output here e.g. null, 0 etc.

 

This may be shortened if your excelQuery resolves in Power Query to just a value, rather than a table, to this:

if excelQuery = "JAN" then List.Sum({[Jan]})
else if excelQuery = "FEB" then List.Sum({[Jan], [Feb]})
else if...
...
else //your escape output here e.g. null, 0 etc.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

5 REPLIES 5
Syndicate_Admin
Administrator
Administrator

I faced the same struggling here..thks for the question and answer. 


question - wonder the excelQuery coding here is referring to the name manager i have to create in excel?(option list created in Excel)? 

many thanksss

excelQuery

 

sorry to ask silly question here (i'm first time P.Query user)

 

i failed with the code....Can anyone advise? many thanksss alot...

 

 

Your first List.Sum doesn't have anything to work on. It should be List.Sum( { [Jan] } ).

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Syndicate_Admin
Administrator
Administrator

Brilliant! Thank you for your help - works perfectly

BA_Pete
Super User
Super User

Hi @Syndicate_Admin @JeremyHillier ,

 

The basic structure of the YTD column calculation would be something like this:

if excelQuery[selectedMonth] = "JAN" then List.Sum({[Jan]})
else if excelQuery[selectedMonth] = "FEB" then List.Sum({[Jan], [Feb]})
else if...
...
else //your escape output here e.g. null, 0 etc.

 

This may be shortened if your excelQuery resolves in Power Query to just a value, rather than a table, to this:

if excelQuery = "JAN" then List.Sum({[Jan]})
else if excelQuery = "FEB" then List.Sum({[Jan], [Feb]})
else if...
...
else //your escape output here e.g. null, 0 etc.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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