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
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
Super User
Super User

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
Super User
Super User

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
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.

Top Solution Authors
Top Kudoed Authors