Forum Discussion

diogoricciardi's avatar
diogoricciardi
Frequent Visitor
6 years ago
Solved

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

 

 

 

 

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

     

2 Replies

  • ImkeF's avatar
    ImkeF
    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.

     

    • diogoricciardi's avatar
      diogoricciardi
      Frequent Visitor

      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.