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
royces99
Regular Visitor

Add columns based off today's date and previous 3 months

I'm trying to add columns to a table with the column names being today's date and the previous 3 months (last day of month).  The code below is free of syntax errors but the last row of code shown generates the error message "Expression.Error: 2 arguments were passed to a function which expects between 3 and 4.  Details: Pattern= Arguments=[List]" and I don't know how to resolve it - what are the other 1 or 2 arguments that I need?  Can anybody help?

 

// STEP 1: Read the initial data
Source = database,

// Get today's date
today = DateTime.Date(DateTime.LocalNow()),

// Calculate the list of date column names
numColumns = 3,
column_names = List.Generate(
() => [i = 0, prevMonthEnd = today],
each [i] < numColumns,
each [i = [i] + 1, prevMonthEnd = Date.From(Date.EndOfMonth(Date.From(Date.AddMonths([prevMonthEnd], -1))))],
each if [i] = 1 then Text.From([prevMonthEnd], "dd-MM-yyyy") else Text.From([prevMonthEnd], "dd-MM-yyyy")
),

// Create a list of columns to add
columnsToAdd = List.Transform(column_names, each [Name = _ & " Column", Type = Int64.Type]),

// Add the columns
databaseWithColumns = Table.AddColumn(Source, columnsToAdd),

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

@royces99 replace last 2 steps with the following

databaseWithColumns = 
   List.Accumulate(
      column_names, 
      Source, 
      (s, c) => Table.AddColumn(s, c & " Column", each null, Int64.Type)
   )

View solution in original post

2 REPLIES 2
wdx223_Daniel
Super User
Super User

databaseWithColumns = Table.SelectColumns(Source,Table.ColumnNames(Source)&List.Transform({1..numColumns},each Date.ToText(Date.EndOfMonth(Date.AddMonths(today,_-1)),"dd-MM-yyyy")),2)

AlienSx
Super User
Super User

@royces99 replace last 2 steps with the following

databaseWithColumns = 
   List.Accumulate(
      column_names, 
      Source, 
      (s, c) => Table.AddColumn(s, c & " Column", each null, Int64.Type)
   )

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