Forum

Dynamic Column Name...
 
Notifications
Clear all

Dynamic Column Name encounter error

5 Posts
2 Users
0 Reactions
185 Views
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin

I am trying to make the first column dynamic,  i.e.  whenever I rename in the source,  it will not affect the Applied steps code,  however  seems like it doesn't work for Custom column,  or did I miss out anything in the M code

Thank you !

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Chg_Type = Table.TransformColumnTypes(Source,{{Table.ColumnNames(Source){0}, type date}}),
#"Added Custom" = Table.AddColumn(Chg_Type, "CY", each Text.End ( Text.From ( Date.Year (Table.ColumnNames(Source){0})) , 2 ))
in
#"Added Custom"

 
Posted : 19/11/2020 9:57 am
(@catalinb)
Posts: 1937
Member Admin
 

What's the purpose of the #"Added Custom" step?

 
Posted : 19/11/2020 1:17 pm
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin, 

This #"Added Custom"  is current year in yy format,  there will be next year and previous year,   this query ultimate goal is to final out Fascial year.

wanted to make the query dynamic so that whenever there is any change in the original source header it will not affect the code,  i.e.  don't need to amend the new col names

thank you !

 
Posted : 19/11/2020 9:43 pm
(@catalinb)
Posts: 1937
Member Admin
 

Table.ColumnNames(Source){0} will return the Name of the first column from the table Source. (Table.ColumnNames returns a list of column ... names)

If you want to refer to the first row from the DateColumn column, use:
Source[DateColumn]{0}

Or, if your parameter table is... dynamic and the headers will change (which by the way sounds weird, a parameter table should be static), you can refer to the first cell from the first column:

Table.ToColumns(Source){0}{0}

 
Posted : 20/11/2020 6:59 am
(@bluesky63)
Posts: 162
Estimable Member
Topic starter
 

Hi Catalin,

Attached is my complete codes

actually what I hope to achieve is whenever there is any changes in the source header,  for e.g.  rename from DateColumn to Start_Date

All the custom columns variables will not affected,   I had already taken care the dynamic for Chg_Type by putting Table.ColumnNames(Source){0} in a parameter header1,   is it possible to make those highlighted one dynamic , Table.ToColumns(Chg_Type){0}{0},  Table.ToColumns(Chg_Type){0}{1},  using loop or other methods,   if not everytime I got to change all the previous header name to the new one.

Apologise I may not explain clearly in my previous threads

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Header1 = Table.ColumnNames(Source){0},
    Chg_Type = Table.TransformColumnTypes(Source,{{Header1, type date}}),
    LastYr = Table.AddColumn(Chg_Type, "LY", each Text.From ( Date.Year ([DateColumn])-1)),
    CurrentYr = Table.AddColumn(LastYr, "CY", each Text.From ( Date.Year ([DateColumn]))),
    #"Changed Type" = Table.TransformColumnTypes(CurrentYr,{{"LY", type text}, {"CY", type text}}),
    FY = Table.AddColumn(#"Changed Type", "FY", each if Date.Month([DateColumn]) < Date.Month(#date(Date.Year ([DateColumn]), 4, 1)) and               Text.From(Date.Year([DateColumn])) = [CY] then "FY "&[LY] else "FY "& [CY])
in
    FY

 
Posted : 20/11/2020 10:09 am
Share: