Forum Discussion

harirao's avatar
harirao
Icon for Post Prodigy rankPost Prodigy
6 years ago
Solved

How to create columns using M language

Hi All, Can you please assist how to create columns in M language. Below are three columns created  Last Refresh Date = Query1[reportdate] ReportingDate = IF(Query1[reportdate]=MAX(Query1[report...
  • Anonymous's avatar
    Anonymous
    6 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.