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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
diogoricciardi
Frequent Visitor

Add multiple columns using List.A

Hi all,

 

I have a table with daily sales information, where product categories are in rows and days of the month are in columns.

My goal is to get each category's percentege of sales by day of the month. The last column is a list.sum of each day of sales of the respective category.

 

The best solution I got to achieve that is to add multiple columns dividing each category's day of sales by its total sales of the month and I'm trying to do that using list.accumulate.

The code I'm using is the following:

 

columns = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
added = List.Accumulate(columns, #"Added Custom", (tab,col) =>
                       Table.AddColumn(tab, Number.ToText(col) & "p", each if [col] <= 0 then 0 else [col]/[Total], Percentage.Type))

 

But it isn't working.

I also tried input the column names as text but it didn't work either.

My understanding is that I'm doing something wrong after the each. Can someone, please, give a hand with that?

 

Thankss

 

 

 

 

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @diogoricciardi ,

this sounds like a calculation that should better be performed dynamically with DAX.

But if you really need to do it in the query editor in M, you should consider unpivoting the days columns (check the columns that should be kept and select: Unpivot other columns). Then you have just 2 columns to divide against each other.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
ImkeF
Community Champion
Community Champion

Hi @diogoricciardi ,

this sounds like a calculation that should better be performed dynamically with DAX.

But if you really need to do it in the query editor in M, you should consider unpivoting the days columns (check the columns that should be kept and select: Unpivot other columns). Then you have just 2 columns to divide against each other.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi Imke,

 

That actually helped a lot. I can't believe I didn't see it hehe.

One of the last steps in my query was pivoting the table so the days would be in columns. So what I did was to calculate the percentages before pivoting it and it worked beautifully.

 

Thanks a lot, I appreciate it.

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.