Hi @Lz._
Thank you so much for you answer! Unfortunately, I couldn't make it work.
I should have clarified that the data is already imported into PQ. I'm not working with a table in an worksheet and then importing it into PQ, but rather grabbing 5 excel files from a folder and importing them into PQ. Then transform the data and finally trying to add the extra column with the desired outcome.
So I tried your code in my original file from line 9 by using the "add custom column" in the GUI and pasting the code in the prompt.
This is what the code looks like so far in the advanced editor (Note: changed a part of it by incorporating the Text.StartsWith formula in your code whichI find very lean and much more efficient):
----------
let
Origen = Folder.Files("ORIGIN ROUTE"),
#"Archivos ocultos filtrados1" = Table.SelectRows(Origen, each [Attributes]?[Hidden]? <> true),
#"Invocar función personalizada1" = Table.AddColumn(#"Archivos ocultos filtrados1", "Transformar archivo", each #"Transformar archivo"([Content])),
#"Columnas con nombre cambiado1" = Table.RenameColumns(#"Invocar función personalizada1", {"Name", "Source.Name"}),
#"Otras columnas quitadas1" = Table.SelectColumns(#"Columnas con nombre cambiado1", {"Source.Name", "Transformar archivo"}),
#"Se expandió Transformar archivo" = Table.ExpandTableColumn(#"Otras columnas quitadas1", "Transformar archivo", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23", "Column24", "Column25", "Column26", "Column27", "Column28", "Column29", "Column30", "Column31", "Column32", "Column33", "Column34", "Column35", "Column36", "Column37", "Column38", "Column39", "Column40", "Column41", "Column42", "Column43"}, {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23", "Column24", "Column25", "Column26", "Column27", "Column28", "Column29", "Column30", "Column31", "Column32", "Column33", "Column34", "Column35", "Column36", "Column37", "Column38", "Column39", "Column40", "Column41", "Column42", "Column43"}),
#"Filas filtradas" = Table.SelectRows(#"Se expandió Transformar archivo", each [Column3] <> null and [Column3] <> ""),
#"Encabezados promovidos" = Table.PromoteHeaders(#"Filas filtradas", [PromoteAllScalars=true]),
#"Filas filtradas1" = Table.SelectRows(#"Encabezados promovidos", each ([Tipo de Documento] <> "Tipo de Documento")),
#"Columnas con nombre cambiado" = Table.RenameColumns(#"Filas filtradas1",{{"Tipo de movimiento", "Tipo de movimiento.1"}}),
#"Tipo cambiado" = Table.TransformColumnTypes(#"Columnas con nombre cambiado",{{"Vigencia Hasta", type date}, {"Tipo de Documento", type text}, {"Número de Documento", type text}, {"Código de Producto", Int64.Type}, {"Rama", type text}, {"Producto", type text}, {"Nro. Endoso", Int64.Type}, {"Usuario", type text}, {"Fecha Emisión", type date}, {"Vigencia Desde", type date}, {"Moneda Emisión", type text}, {"Cant. de Cuotas", Int64.Type}, {"Prima", Currency.Type}, {"Recargo Financiero", Currency.Type}, {"Otras Cargas", Currency.Type}, {"IVA", Currency.Type}, {"Prima Total", Currency.Type}, {"Suma Asegurada", Currency.Type}, {"Fronting", type text}, {"Tipo de Movimiento", type text}, {"Tipo de Endoso", type text}, {"Tomador", Int64.Type}, {"Nombre Tomador", type text}, {"Tipo de movimiento.1", type text}, {"Medio de transporte", type text}, {"Fecha de Operación", type date}, {"Tipo Operación", type text}, {"Actividad del Riesgo", type text}, {"Agente", Int64.Type}, {"Nombre Agente", type text}, {"Importe Comisión", Currency.Type}, {"Tipo Cambio", Currency.Type}, {"Total Prima Cedida", Currency.Type}, {"Total Suma Asegurada Cedida", Currency.Type}, {"Compañía", type text}, {"% de fronting fee", Percentage.Type}, {"% de comisión Reaseguro", Percentage.Type}, {"% Impuestos (Fronting/Reas)", Percentage.Type}, {"Monto de fronting fee", Currency.Type}, {"Monto de comisión Reaseguro", Currency.Type}, {"Monto Impuestos (Fronting/Reas)", Currency.Type}}),
#"Columna duplicada" = Table.DuplicateColumn(#"Tipo cambiado", "Código Rama", "Código Rama - Copia"),
#"Columnas combinadas" = Table.CombineColumns(Table.TransformColumnTypes(#"Columna duplicada", {{"Código Rama - Copia", type text}, {"Póliza", type text}}, "es-UY"),{"Código Rama - Copia", "Póliza"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Poliza"),
Vigencia = Table.AddColumn(#"Columnas combinadas", "Vigencia", each if Text.StartsWith([Tipo de Movimiento], "Cancelac") then "NO VIGENTE" else
if Duration.Days([Vigencia Hasta] - Date.From(DateTime.LocalNow())) >=0 then "VIGENTE" else "NO VIGENTE"),
#"Tipo cambiado1" = Table.TransformColumnTypes(Vigencia,{{"Vigencia", type text}})
in
#"Tipo cambiado1"
----------
Couldn't write on the sample file you shared either to show the expected outcome.