Forum Discussion
How to create columns using M language
- Anonymous6 years ago
Yes. Only thing you have to take care of the tables names and field names that we are passing to these functions. You can add the following codes before the "IN" line and change the #"Changed Type" in the 2nd line below to #"Sorted Rows" because that is the step name that you are referring to the table in your code. As I mentioned earlier, please correct the field names correctly. for example, I have used field names like [ReportDate],[actual_visibility_weeks] etc... If the names are different in your table, change them accordingly, but keep the square brackets.
AddedLastRefreshDate =Table.AddColumn( #"Changed Type", "Last Refresh Date", each [ReportDate] ), AddedReportingDate = Table.AddColumn( AddedLastRefreshDate, "Reporting Date", each if [ReportDate] = List.Max(Table.Column(AddedLastRefreshDate,"ReportDate")) then "CurrentWeek" else Date.ToText([ReportDate],"YYYY-MM-DD") ), AddedVisibilityGroup = Table.AddColumn( AddedReportingDate, "Visibility Group", each if [actual_visibility_weeks] >0 and [actual_visibility_weeks] <=5 then "<6 weeks visibility" else ">6 weeks visibility" )These are actually simple in-built functions.
Table.AddColumn(<tableName>,<NewColumnName>,each <calculation>)
IF <condition> then <statement> else <statement>
List.Max() returns the maximum from a list.
Table.Column() converts a table's column into a list which we can pass it on to List.Max()
Date.ToText() converts a date value to text.
Hello Anonymous
Thanks for your response, i have connected to sql db and created import query, still i will be able to include below code in m language?
Thank you
Regards,
Yes. Only thing you have to take care of the tables names and field names that we are passing to these functions. You can add the following codes before the "IN" line and change the #"Changed Type" in the 2nd line below to #"Sorted Rows" because that is the step name that you are referring to the table in your code. As I mentioned earlier, please correct the field names correctly. for example, I have used field names like [ReportDate],[actual_visibility_weeks] etc... If the names are different in your table, change them accordingly, but keep the square brackets.
AddedLastRefreshDate =Table.AddColumn(
#"Changed Type",
"Last Refresh Date", each [ReportDate]
),
AddedReportingDate =
Table.AddColumn(
AddedLastRefreshDate,
"Reporting Date",
each if [ReportDate] = List.Max(Table.Column(AddedLastRefreshDate,"ReportDate")) then "CurrentWeek" else Date.ToText([ReportDate],"YYYY-MM-DD")
),
AddedVisibilityGroup =
Table.AddColumn(
AddedReportingDate,
"Visibility Group",
each if [actual_visibility_weeks] >0 and [actual_visibility_weeks] <=5 then "<6 weeks visibility" else ">6 weeks visibility"
)
These are actually simple in-built functions.
Table.AddColumn(<tableName>,<NewColumnName>,each <calculation>)
IF <condition> then <statement> else <statement>
List.Max() returns the maximum from a list.
Table.Column() converts a table's column into a list which we can pass it on to List.Max()
Date.ToText() converts a date value to text.